Heatmap using seabron machine learing



#!/usr/bin/env python
# coding: utf-8

# # Heatmap using seabron

# In[1]:


import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


# In[2]:


data_2d = np.linspace(1,5 ,12).reshape(4,3)
data_2d


# In[3]:


sns.heatmap(data_2d)

heatmap show only numeric valuse
# In[4]:


data=pd.read_csv("who is responsible for global warming.csv")
data.head(2)


# In[5]:


data1=data.drop(["Country Code","Indicator Name","Indicator Code"],axis=1).set_index("Country Name")


# In[6]:


plt.figure(figsize=(16,9))
sns.heatmap(data1)

sns.heatmap(
    data,
    vmin=None,
    vmax=None,
    cmap=None,
    center=None,
    robust=False,
    annot=None,
    fmt='.2g',
    annot_kws=None,
    linewidths=0,
    linecolor='white',
    cbar=True,
    cbar_kws=None,
    cbar_ax=None,
    square=False,
    xticklabels='auto',
    yticklabels='auto',
    mask=None,
    ax=None,
    **kwargs,
)
Docstring:
Plot rectangular data as a color-encoded matrix.

This is an Axes-level function and will draw the heatmap into the
currently-active Axes if none is provided to the ``ax`` argument.  Part of
this Axes space will be taken and used to plot a colormap, unless ``cbar``
is False or a separate Axes is provided to ``cbar_ax``.

Parameters
----------
data : rectangular dataset
    2D dataset that can be coerced into an ndarray. If a Pandas DataFrame
    is provided, the index/column information will be used to label the
    columns and rows.
vmin, vmax : floats, optional
    Values to anchor the colormap, otherwise they are inferred from the
    data and other keyword arguments.
cmap : matplotlib colormap name or objector list of colors, optional
    The mapping from data values to color space. If not provided, the
    default will depend on whether ``center`` is set.
center : float, optional
    The value at which to center the colormap when plotting divergant data.
    Using this parameter will change the default ``cmap`` if none is
    specified.
robust : bool, optional
    If True and ``vmin`` or ``vmax`` are absent, the colormap range is
    computed with robust quantiles instead of the extreme values.
annot : bool or rectangular dataset, optional
    If True, write the data value in each cell. If an array-like with the
    same shape as ``data``, then use this to annotate the heatmap instead
    of the data. Note that DataFrames will match on position, not index.
fmt : string, optional
    String formatting code to use when adding annotations.
annot_kws : dict of key, value mappings, optional
    Keyword arguments for ``ax.text`` when ``annot`` is True.
linewidths : float, optional
    Width of the lines that will divide each cell.
linecolor : color, optional
    Color of the lines that will divide each cell.
cbar : boolean, optional
    Whether to draw a colorbar.
cbar_kws : dict of key, value mappings, optional
    Keyword arguments for `fig.colorbar`.
cbar_ax : matplotlib Axes, optional
    Axes in which to draw the colorbar, otherwise take space from the
    main Axes.
square : boolean, optional
    If Trueset the Axes aspect to "equal" so each cell will be
    square-shaped.
xticklabels, yticklabels : "auto"boollist-like, or int, optional
    If True, plot the column names of the dataframe. If False, don't plot
    the column names. If list-like, plot these alternate labels as the
    xticklabels. If an integer, use the column names but plot only every
    n label. If "auto"try to densely plot non-overlapping labels.
mask : boolean array or DataFrame, optional
    If passed, data will not be shown in cells where ``mask`` is True.
    Cells with missing values are automatically masked.
ax : matplotlib Axes, optional
    Axes in which to draw the plot, otherwise use the currently-active
    Axes.
kwargs : other keyword arguments
    All other keyword arguments are passed to
    :func:`matplotlib.axes.Axes.pcolormesh`.

Returns
-------
ax : matplotlib Axes
    Axes object with the heatmap.

See also
--------
clustermap : Plot a matrix using hierachical clustering to arrange the
             rows and columns.

Examples
--------

Plot a heatmap for a numpy array:

.. plot::
    :context: close-figs

    >>> import numpy as np; np.random.seed(0)
    >>> import seaborn as sns; sns.set()
    >>> uniform_data = np.random.rand(1012)
    >>> ax = sns.heatmap(uniform_data)

Change the limits of the colormap:

.. plot::
    :context: close-figs

    >>> ax = sns.heatmap(uniform_data, vmin=0vmax=1)

Plot a heatmap for data centered on 0 with a diverging colormap:

.. plot::
    :context: close-figs

    >>> normal_data = np.random.randn(1012)
    >>> ax = sns.heatmap(normal_data, center=0)

Plot a dataframe with meaningful row and column labels:

.. plot::
    :context: close-figs

    >>> flights = sns.load_dataset("flights")
    >>> flights = flights.pivot("month""year""passengers")
    >>> ax = sns.heatmap(flights)

Annotate each cell with the numeric value using integer formatting:

.. plot::
    :context: close-figs

    >>> ax = sns.heatmap(flights, annot=Truefmt="d")

Add lines between each cell:

.. plot::
    :context: close-figs

    >>> ax = sns.heatmap(flights, linewidths=.5)

Use a different colormap:

.. plot::
    :context: close-figs

    >>> ax = sns.heatmap(flights, cmap="YlGnBu")

Center the colormap at a specific value:

.. plot::
    :context: close-figs

    >>> ax = sns.heatmap(flights, center=flights.loc["January"1955])

Plot every other column label and don't plot row labels:

.. plot::
    :context: close-figs

    >>> data = np.random.randn(5020)
    >>> ax = sns.heatmap(data, xticklabels=2yticklabels=False)

Don't draw a colorbar:

.. plot::
    :context: close-figs

    >>> ax = sns.heatmap(flights, cbar=False)

Use different axes for the colorbar:

.. plot::
    :context: close-figs

    >>> grid_kws = {"height_ratios": (.9.05), "hspace".3}
    >>> f, (ax, cbar_ax) = plt.subplots(2gridspec_kw=grid_kws)
    >>> ax = sns.heatmap(flights, ax=ax,
    ...                  cbar_ax=cbar_ax,
    ...                  cbar_kws={"orientation""horizontal"})

Use a mask to plot only part of a matrix

.. plot::
    :context: close-figs

    >>> corr = np.corrcoef(np.random.randn(10200))
    >>> mask = np.zeros_like(corr)
    >>> mask[np.triu_indices_from(mask)] = True
    >>> with sns.axes_style("white"):
    ...     f, ax = plt.subplots(figsize=(75))
    ...     ax = sns.heatmap(corr, mask=mask, vmax=.3square=True)sns.heatmap(
    data,
    vmin=None,
    vmax=None,
    cmap=None,
    center=None,
    robust=False,
    annot=None,
    fmt='.2g',
    annot_kws=None,
    linewidths=0,
    linecolor='white',
    cbar=True,
    cbar_kws=None,
    cbar_ax=None,
    square=False,
    xticklabels='auto',
    yticklabels='auto',
    mask=None,
    ax=None,
    **kwargs,
)
# In[7]:


plt.figure(figsize=(16,9))
sns.heatmap(data1, vmin=0,vmax=20)


# In[8]:


"""
cmap valuse= supported values are 'Accent', 'Accent_r', 'Blues', 
'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 
'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 
'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 
'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2',
'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr',
'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy',
'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds',
'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r',
'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 
'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 
'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm',
'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 
'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 
'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r',
'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray',
'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'icefire', 'icefire_r', 'inferno', 'inferno_r', 
'jet', 'jet_r', 'magma', 'magma_r', 'mako', 'mako_r', 'nipy_spectral', 'nipy_spectral_r',
'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 
'rainbow_r', 'rocket', 'rocket_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer',
'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r',
'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 
'twilight_shifted_r', 'viridis', 'viridis_r', 'vlag', 'vlag_r', 'winter', 'winter_r'
"""
plt.figure(figsize=(16,9))

sns.heatmap(data1, cmap="coolwarm")


# In[9]:


plt.figure(figsize=(16,9))

sns.heatmap(data1, cmap="coolwarm",annot=True)


# In[10]:


data_2d


# In[11]:


arr_2d=np.array([[1,2,3],[4,5,6],[7,8,9],[1,5,9]])
arr_2d


# In[12]:


sns.heatmap(data_2d,annot=arr_2d,fmt="d")#  fmt----str="s", decimal="d"


# In[13]:


plt.figure(figsize=(16,9))
annot_kws1={
    "fontsize":15,
    "fontstyle":"italic",
   'color':'k',
    "alpha":0.9,
    "rotation":"vertical",
    "verticalalignment":"center",
    "backgroundcolor":"w"
}

sns.heatmap(data1, cmap="coolwarm",annot=True,annot_kws=annot_kws1)


# In[14]:


plt.figure(figsize=(17,10))

sns.heatmap(data1, cmap="coolwarm",annot=True,linewidths=5)


# In[15]:


plt.figure(figsize=(17,10))

sns.heatmap(data1, cmap="coolwarm",annot=True,linewidths=5,linecolor="k")


# In[16]:


plt.figure(figsize=(17,10))

sns.heatmap(data1, cmap="coolwarm",annot=True,linewidths=5,cbar=False)


# In[17]:


plt.figure(figsize=(17,10))

sns.heatmap(data1, cmap="coolwarm",annot=True,linewidths=5,xticklabels=False,yticklabels=False)


# In[18]:


plt.figure(figsize=(17,10))

sns.heatmap(data1, cmap="coolwarm",annot=True,linewidths=5,cbar_kws={
    #"orientation":"vertical",
    "orientation":"horizontal",
    "shrink":1,
    "extend":"min",#min,max,both
    "extendfrac":0.1,
    "ticks":np.arange(0,22),
    "drawedges":True,
})


# In[19]:


plt.figure(figsize=(17,10))

sns.heatmap(data1,annot=True,linewidths=5,cbar_kws={
    "orientation":"vertical",
#     "orientation":"horizontal",
    "shrink":1,
    "extend":"min"# min,max,both
    "extendfrac":0.1,
    "ticks":np.arange(0,22),
    "drawedges":True,
})


# In[20]:


plt.figure(figsize=(17,10))
#  yticklabels= # contry_lab=["a","b","c",............] ap de sakte ho lebale to name 
sns.heatmap(data1,annot=True,linewidths=5,xticklabels=np.arange(0,15),yticklabels=np.arange(0,15),cbar_kws={"extend":"min","drawedges":True})


# In[21]:


# # # plt.figure(figsize=(17,10))

# ax=sns.heatmap(data1, cmap="coolwarm",annot=True,linewidths=5)
# ax.set(title="vasim shaikh first heatmap ",
#       xlabel="co2 eemmistion ",
#       ylabel="Country name")
# sns.set(font_scale=3)


# # Correlation heatmap

# In[22]:


data1.corr()


# In[28]:


plt.figure(figsize=(16,9))
sns.heatmap(data1.corr(),annot=True,linewidth=3)


# In[29]:


plt.figure(figsize=(16,9))
ax=sns.heatmap(data1.corr(),annot=True,linewidth=3)
ax.tick_params(size=10,color="w",labelsize=10,labelcolor="w")
plt.title("heatmap using seaborn"fontsize=15)
plt.show()


# In[30]:


from sklearn.datasets import load_breast_cancer
canncer_dataset=load_breast_cancer()


# In[31]:


canncer_dataset


# In[42]:


canner_df = pd.DataFrame(np.c_[canncer_dataset["data"],canncer_dataset["target"]],
                       columns=np.append(canncer_dataset["feature_names"],["targget"]))
canner_df


# In[47]:


plt.figure(figsize=(30,30))
ax=sns.heatmap(canner_df.corr(),annot=True,linewidth=3)
ax.tick_params(size=10,color="w",labelsize=10,labelcolor="w")
plt.title("canner_data using seaborn"fontsize=15)
plt.show()



Post a Comment

0 Comments