/ Python And R Data science skills: 90 Matrix Plots Heat Maps Public

Sunday 18 February 2018

90 Matrix Plots Heat Maps Public

https://vlrtraining.com/courses/python-data-science-beginner-tutorial 90 Matrix Plots Heat Maps Public

Matrix Plots

Matrix plots allow you to plot data as color-encoded matrices and can also be used to indicate clusters within the data (later in the machine learning section we will learn how to formally cluster data).

Let's begin by exploring seaborn's heatmap and clutermap:

In [1]:
import seaborn as sns
%matplotlib inline
flights = sns.load_dataset('flights')
tips = sns.load_dataset('tips')
In [46]:
flights.head(2)
Out[46]:
year month passengers
0 1949 January 112
1 1949 February 118
In [4]:
tips.head(2)
Out[4]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
In [45]:
tips.corr()
#flights.corr()
Out[45]:
total_bill tip size
total_bill 1.000000 0.675734 0.598315
tip 0.675734 1.000000 0.489299
size 0.598315 0.489299 1.000000
In [9]:
tc=tips.corr()
In [47]:
sns.heatmap(tc)
Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0xc49dfd0>
In [48]:
sns.heatmap(tc,annot=True)
Out[48]:
<matplotlib.axes._subplots.AxesSubplot at 0xc827898>
In [50]:
sns.heatmap(tc,annot=True,cmap='coolwarm')
#tips.head(2)
Out[50]:
<matplotlib.axes._subplots.AxesSubplot at 0xe175550>
In [27]:
tips.pivot_table(values='tip',index='day',columns='sex')
Out[27]:
sex Male Female
day
Thur 2.980333 2.575625
Fri 2.693000 2.781111
Sat 3.083898 2.801786
Sun 3.220345 3.367222
In [29]:
tips.pivot_table(values='tip',index='day',columns='smoker')
Out[29]:
smoker Yes No
day
Thur 3.030000 2.673778
Fri 2.714000 2.812500
Sat 2.875476 3.102889
Sun 3.516842 3.167895
In [30]:
tips.pivot_table(values='tip',index='day',columns='size')
Out[30]:
size 1 2 3 4 5 6
day
Thur 1.83 2.442500 2.692500 4.218000 5.000000 5.3
Fri 1.92 2.644375 3.000000 4.730000 NaN NaN
Sat 1.00 2.517547 3.797778 4.123846 3.000000 NaN
Sun NaN 2.816923 3.120667 4.087778 4.046667 5.0
In [52]:
flights.head()
Out[52]:
year month passengers
0 1949 January 112
1 1949 February 118
2 1949 March 132
3 1949 April 129
4 1949 May 121
In [53]:
flights.pivot_table(values='passengers',index='month',columns='year')
Out[53]:
year 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
month
January 112 115 145 171 196 204 242 284 315 340 360 417
February 118 126 150 180 196 188 233 277 301 318 342 391
March 132 141 178 193 236 235 267 317 356 362 406 419
April 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
June 135 149 178 218 243 264 315 374 422 435 472 535
July 148 170 199 230 264 302 364 413 465 491 548 622
August 148 170 199 242 272 293 347 405 467 505 559 606
September 136 158 184 209 237 259 312 355 404 404 463 508
October 119 133 162 191 211 229 274 306 347 359 407 461
November 104 114 146 172 180 203 237 271 305 310 362 390
December 118 140 166 194 201 229 278 306 336 337 405 432
In [54]:
fpt = flights.pivot_table(values='passengers',index='month',columns='year')
sns.heatmap(fpt)
Out[54]:
<matplotlib.axes._subplots.AxesSubplot at 0xe272860>
In [55]:
sns.heatmap(fpt,cmap='magma')
Out[55]:
<matplotlib.axes._subplots.AxesSubplot at 0xe2c0780>
In [58]:
sns.heatmap(fpt,cmap='magma',linecolor='w',linewidths=1)
Out[58]:
<matplotlib.axes._subplots.AxesSubplot at 0xe51c550>

No comments:

Post a Comment