Seaborn Heatmap

Seaborn Heatmap

In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
In [2]:
np.random.seed(0)
sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)
plt.show()
In [3]:
df = pd.DataFrame(np.random.randn(50).reshape(10,5))
df
Out[3]:
0 1 2 3 4
0 0.010500 1.785870 0.126912 0.401989 1.883151
1 -1.347759 -1.270485 0.969397 -1.173123 1.943621
2 -0.413619 -0.747455 1.922942 1.480515 1.867559
3 0.906045 -0.861226 1.910065 -0.268003 0.802456
4 0.947252 -0.155010 0.614079 0.922207 0.376426
5 -1.099401 0.298238 1.326386 -0.694568 -0.149635
6 -0.435154 1.849264 0.672295 0.407462 -0.769916
7 0.539249 -0.674333 0.031831 -0.635846 0.676433
8 0.576591 -0.208299 0.396007 -1.093062 -1.491258
9 0.439392 0.166673 0.635031 2.383145 0.944479
In [4]:
#corr() is used to find the pairwise correlation of all columns in the dataframe.
corr = df.corr()
corr
Out[4]:
0 1 2 3 4
0 1.000000 -0.055747 -0.228936 0.263067 -0.229857
1 -0.055747 1.000000 -0.420999 0.246921 -0.254467
2 -0.228936 -0.420999 1.000000 0.104807 0.222818
3 0.263067 0.246921 0.104807 1.000000 0.292405
4 -0.229857 -0.254467 0.222818 0.292405 1.000000
In [5]:
sns.heatmap(corr)
plt.show()
In [6]:
#set the lower and upper bound of the heatmap color bar. Here, we passed 0 value as lower bound and 1 as upper bound
ax1 = sns.heatmap(corr, cbar=0, linewidths=2, vmax=1, vmin=0, square=True, cmap='Blues')
plt.show()
In [7]:
sns.set()
flights = sns.load_dataset("flights")
flights
Out[7]:
year month passengers
0 1949 Jan 112
1 1949 Feb 118
2 1949 Mar 132
3 1949 Apr 129
4 1949 May 121
... ... ... ...
139 1960 Aug 606
140 1960 Sep 508
141 1960 Oct 461
142 1960 Nov 390
143 1960 Dec 432

144 rows × 3 columns

DataFrame.pivot(index=None, columns=None, values=None)

Parameters

    index: Column to use to make new frame’s index. If None, uses existing index.
    columns: Column to use to make new frame’s columns.
    values: Column(s) to use for populating new frame’s values. 
    If not specified, all remaining columns will be used and the result will have hierarchically indexed columns.

Returns
    reshaped DataFrame
In [8]:
#Reshape data (produce a “pivot” table) based on column values.
flights = flights.pivot("month", "year", "passengers")
flights
Out[8]:
year 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
month
Jan 112 115 145 171 196 204 242 284 315 340 360 417
Feb 118 126 150 180 196 188 233 277 301 318 342 391
Mar 132 141 178 193 236 235 267 317 356 362 406 419
Apr 129 135 163 181 235 227 269 313 348 348 396 461
May 121 125 172 183 229 234 270 318 355 363 420 472
Jun 135 149 178 218 243 264 315 374 422 435 472 535
Jul 148 170 199 230 264 302 364 413 465 491 548 622
Aug 148 170 199 242 272 293 347 405 467 505 559 606
Sep 136 158 184 209 237 259 312 355 404 404 463 508
Oct 119 133 162 191 211 229 274 306 347 359 407 461
Nov 104 114 146 172 180 203 237 271 305 310 362 390
Dec 118 140 166 194 201 229 278 306 336 337 405 432
In [9]:
flights.columns
Out[9]:
Int64Index([1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959,
            1960],
           dtype='int64', name='year')
In [10]:
ax = sns.heatmap(flights)
plt.title("Heatmap Flight Data")
plt.show()
In [11]:
ax = sns.heatmap(flights, cmap="coolwarm")
plt.title("Heatmap Flight Data")
plt.show()
In [12]:
plt.figure(figsize=(16,9))
ax = sns.heatmap(flights, cmap="coolwarm", annot = True)
plt.title("Heatmap Flight Data")
plt.show()

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing