forked from Visualize-ML/Book3_Elements-of-Mathematics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBk3_Ch21_1.py
More file actions
214 lines (161 loc) · 6.54 KB
/
Bk3_Ch21_1.py
File metadata and controls
214 lines (161 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
###############
# Authored by Weisheng Jiang
# Book 3 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk3_Ch21_1_A
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.datasets import load_iris
# Load the iris data
iris_sns = sns.load_dataset("iris")
#%% Scatter plot of x1 and x2
fig, ax = plt.subplots()
ax = sns.scatterplot(data=iris_sns, x="sepal_length", y="sepal_width")
ax.set_xlabel('Sepal length, $x_1$ (cm)')
ax.set_ylabel('Sepal width, $x_2$ (cm)')
ax.set_xticks(np.arange(4, 8 + 1, step=1))
ax.set_yticks(np.arange(1, 5 + 1, step=1))
ax.axis('scaled')
ax.grid(linestyle='--', linewidth=0.25, color=[0.7,0.7,0.7])
ax.set_xbound(lower = 4, upper = 8)
ax.set_ybound(lower = 1, upper = 5)
fig, ax = plt.subplots()
ax = sns.scatterplot(data=iris_sns, x="sepal_length",
y="sepal_width", hue = "species")
ax.set_xlabel('Sepal length, $x_1$ (cm)')
ax.set_ylabel('Sepal width, $x_2$ (cm)')
ax.set_xticks(np.arange(4, 8 + 1, step=1))
ax.set_yticks(np.arange(1, 5 + 1, step=1))
ax.axis('scaled')
ax.grid(linestyle='--', linewidth=0.25, color=[0.7,0.7,0.7])
ax.set_xbound(lower = 4, upper = 8)
ax.set_ybound(lower = 1, upper = 5)
#%% 3D scatter plot
x1=iris_sns['sepal_length']
x2=iris_sns['sepal_width']
x3=iris_sns['petal_length']
labels = iris_sns['species'].copy()
labels[labels == 'setosa'] =1
labels[labels == 'versicolor'] =2
labels[labels == 'virginica'] =3
rainbow = plt.get_cmap("rainbow")
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x1, x2, x3)
ax.set_xlabel('Sepal length, $x_1$ (cm)')
ax.set_ylabel('Sepal width, $x_2$ (cm)')
ax.set_zlabel('Petal length, $x_3$ (cm)')
plt.show()
ax.set_proj_type('ortho')
ax.view_init(azim=-135, elev=30)
fig = plt.figure()
ax = fig.gca(projection='3d')
scatter_h = ax.scatter(x1, x2, x3, c = labels, cmap=rainbow)
classes = ['Setosa', 'Versicolor', 'Virginica']
plt.legend(handles=scatter_h.legend_elements()[0], labels=classes)
ax.set_xlabel('Sepal length, $x_1$ (cm)')
ax.set_ylabel('Sepal width, $x_2$ (cm)')
ax.set_zlabel('Petal length, $x_3$ (cm)')
plt.show()
ax.set_proj_type('ortho')
ax.view_init(azim=-135, elev=30)
#%% pairwise plot
sns.pairplot(iris_sns)
sns.pairplot(iris_sns, hue = 'species')
# Bk3_Ch21_1_B
#%% add mean values to the histograms
fig, axes = plt.subplots(2,2)
mu_1 = iris_sns['sepal_length'].mean()
sns.histplot(data=iris_sns, x = 'sepal_length', binwidth = 0.2, ax = axes[0][0])
axes[0][0].set_xlim([0,8]); axes[0][0].set_ylim([0,40])
axes[0][0].vlines(x = mu_1,
ymin = 0, ymax = 40, color = 'r')
mu_2 = iris_sns['sepal_width'].mean()
sns.histplot(data=iris_sns, x = 'sepal_width', binwidth = 0.2, ax = axes[0][1])
axes[0][1].set_xlim([0,8]); axes[0][1].set_ylim([0,40])
axes[0][1].vlines(x = mu_2,
ymin = 0, ymax = 40, color = 'r')
mu_3 = iris_sns['petal_length'].mean()
sns.histplot(data=iris_sns, x = 'petal_length', binwidth = 0.2, ax = axes[1][0])
axes[1][0].set_xlim([0,8]); axes[1][0].set_ylim([0,40])
axes[1][0].vlines(x = mu_3,
ymin = 0, ymax = 40, color = 'r')
mu_4 = iris_sns['petal_width'].mean()
sns.histplot(data=iris_sns, x = 'petal_width', binwidth = 0.2, ax = axes[1][1])
axes[1][1].set_xlim([0,8]); axes[1][1].set_ylim([0,40])
axes[1][1].vlines(x = mu_4,
ymin = 0, ymax = 40, color = 'r')
# Bk3_Ch21_1_C
#%% add mean values and std bands to the histograms
num = 0
fig, axes = plt.subplots(2,2)
for i in [0,1]:
for j in [0,1]:
sns.histplot(data=iris_sns, x = iris_sns.columns[num],
binwidth = 0.2, ax = axes[i][j])
axes[i][j].set_xlim([0,8]); axes[0][0].set_ylim([0,40])
mu = iris_sns.iloc[:,num].mean()
std = iris_sns.iloc[:,num].std()
axes[i][j].axvline(x=mu,
color = 'r', linestyle = '--')
axes[i][j].axvline(x=mu - std,
color = 'r', linestyle = '--')
axes[i][j].axvline(x=mu + std,
color = 'r', linestyle = '--')
axes[i][j].axvline(x=mu - 2*std,
color = 'r', linestyle = '--')
axes[i][j].axvline(x=mu + 2*std,
color = 'r', linestyle = '--')
num = num + 1
# Bk3_Ch21_1_D
#%% covariance matrix
SIGMA = iris_sns.cov()
fig, axs = plt.subplots()
h = sns.heatmap(SIGMA,cmap='RdBu_r', linewidths=.05, annot = True)
h.set_aspect("equal")
h.set_title('$\Sigma$')
#%% compare covariance matrices, with class labels
f,(ax1,ax2,ax3) = plt.subplots(1,3,sharey=True)
g1 = sns.heatmap(iris_sns.loc[iris_sns['species'] == 'setosa'].cov(),
cmap="RdYlBu_r",
annot=True,cbar=False,ax=ax1,square=True,
vmax = 0.4, vmin = 0)
ax1.set_title('Y = 0, setosa')
g2 = sns.heatmap(iris_sns.loc[iris_sns['species'] == 'versicolor'].cov(),
cmap="RdYlBu_r",
annot=True,cbar=False,ax=ax2,square=True,
vmax = 0.4, vmin = 0)
ax2.set_title('Y = 1, versicolor')
g3 = sns.heatmap(iris_sns.loc[iris_sns['species'] == 'virginica'].cov(),
cmap="RdYlBu_r",
annot=True,cbar=False,ax=ax3,square=True,
vmax = 0.4, vmin = 0)
ax3.set_title('Y = 2, virginica')
# Bk3_Ch21_1_E
#%% correlation matrix
RHO = iris_sns.corr()
fig, axs = plt.subplots()
h = sns.heatmap(RHO,cmap='RdBu_r', linewidths=.05, annot = True)
h.set_aspect("equal")
h.set_title('$\u03A1$')
#%% compare correlation matrices, with class labels
f,(ax1,ax2,ax3) = plt.subplots(1,3,sharey=True)
g1 = sns.heatmap(iris_sns.loc[iris_sns['species'] == 'setosa'].corr(),
cmap="RdYlBu_r",
annot=True,cbar=False,ax=ax1,square=True,
vmax = 1, vmin = 0.15)
ax1.set_title('Y = 0, setosa')
g2 = sns.heatmap(iris_sns.loc[iris_sns['species'] == 'versicolor'].corr(),
cmap="RdYlBu_r",
annot=True,cbar=False,ax=ax2,square=True,
vmax = 1, vmin = 0.15)
ax2.set_title('Y = 1, versicolor')
g3 = sns.heatmap(iris_sns.loc[iris_sns['species'] == 'virginica'].corr(),
cmap="RdYlBu_r",
annot=True,cbar=False,ax=ax3,square=True,
vmax = 1, vmin = 0.15)
ax3.set_title('Y = 2, virginica')