Skip to content

Commit af706f1

Browse files
committed
ch12
1 parent d0e7247 commit af706f1

31 files changed

Lines changed: 2364 additions & 92 deletions

ch10/ch10.ipynb

Lines changed: 39 additions & 39 deletions
Large diffs are not rendered by default.

ch10/ch10.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,15 @@ def lin_regplot(X, y, model):
502502
y_quad_fit = pr.predict(quadratic.fit_transform(X_fit))
503503

504504
# plot results
505-
plt.scatter(X, y, label='training points')
506-
plt.plot(X_fit, y_lin_fit, label='linear fit', linestyle='--')
507-
plt.plot(X_fit, y_quad_fit, label='quadratic fit')
505+
plt.scatter(X, y, label='Training points')
506+
plt.plot(X_fit, y_lin_fit, label='Linear fit', linestyle='--')
507+
plt.plot(X_fit, y_quad_fit, label='Quadratic fit')
508508
plt.xlabel('Explanatory variable')
509509
plt.ylabel('Predicted or known target values')
510510
plt.legend(loc='upper left')
511511

512512
plt.tight_layout()
513-
plt.savefig('images/10_10.png', dpi=300)
513+
#plt.savefig('images/10_11.png', dpi=300)
514514
plt.show()
515515

516516

@@ -563,22 +563,22 @@ def lin_regplot(X, y, model):
563563

564564

565565
# plot results
566-
plt.scatter(X, y, label='training points', color='lightgray')
566+
plt.scatter(X, y, label='Training points', color='lightgray')
567567

568568
plt.plot(X_fit, y_lin_fit,
569-
label='linear (d=1), $R^2=%.2f$' % linear_r2,
569+
label='Linear (d=1), $R^2=%.2f$' % linear_r2,
570570
color='blue',
571571
lw=2,
572572
linestyle=':')
573573

574574
plt.plot(X_fit, y_quad_fit,
575-
label='quadratic (d=2), $R^2=%.2f$' % quadratic_r2,
575+
label='Quadratic (d=2), $R^2=%.2f$' % quadratic_r2,
576576
color='red',
577577
lw=2,
578578
linestyle='-')
579579

580580
plt.plot(X_fit, y_cubic_fit,
581-
label='cubic (d=3), $R^2=%.2f$' % cubic_r2,
581+
label='Cubic (d=3), $R^2=%.2f$' % cubic_r2,
582582
color='green',
583583
lw=2,
584584
linestyle='--')
@@ -587,7 +587,7 @@ def lin_regplot(X, y, model):
587587
plt.ylabel('Price in $1000s [MEDV]')
588588
plt.legend(loc='upper right')
589589

590-
#plt.savefig('images/10_11.png', dpi=300)
590+
#plt.savefig('images/10_12.png', dpi=300)
591591
plt.show()
592592

593593

@@ -610,10 +610,10 @@ def lin_regplot(X, y, model):
610610
linear_r2 = r2_score(y_sqrt, regr.predict(X_log))
611611

612612
# plot results
613-
plt.scatter(X_log, y_sqrt, label='training points', color='lightgray')
613+
plt.scatter(X_log, y_sqrt, label='Training points', color='lightgray')
614614

615615
plt.plot(X_fit, y_lin_fit,
616-
label='linear (d=1), $R^2=%.2f$' % linear_r2,
616+
label='Linear (d=1), $R^2=%.2f$' % linear_r2,
617617
color='blue',
618618
lw=2)
619619

@@ -622,7 +622,7 @@ def lin_regplot(X, y, model):
622622
plt.legend(loc='lower left')
623623

624624
plt.tight_layout()
625-
#plt.savefig('images/10_12.png', dpi=300)
625+
#plt.savefig('images/10_13.png', dpi=300)
626626
plt.show()
627627

628628

@@ -647,7 +647,7 @@ def lin_regplot(X, y, model):
647647
lin_regplot(X[sort_idx], y[sort_idx], tree)
648648
plt.xlabel('% lower status of the population [LSTAT]')
649649
plt.ylabel('Price in $1000s [MEDV]')
650-
#plt.savefig('images/10_13.png', dpi=300)
650+
#plt.savefig('images/10_14.png', dpi=300)
651651
plt.show()
652652

653653

@@ -691,15 +691,15 @@ def lin_regplot(X, y, model):
691691
marker='o',
692692
s=35,
693693
alpha=0.9,
694-
label='training data')
694+
label='Training data')
695695
plt.scatter(y_test_pred,
696696
y_test_pred - y_test,
697697
c='limegreen',
698698
edgecolor='white',
699699
marker='s',
700700
s=35,
701701
alpha=0.9,
702-
label='test data')
702+
label='Test data')
703703

704704
plt.xlabel('Predicted values')
705705
plt.ylabel('Residuals')
@@ -708,7 +708,7 @@ def lin_regplot(X, y, model):
708708
plt.xlim([-10, 50])
709709
plt.tight_layout()
710710

711-
# plt.savefig('images/10_14.png', dpi=300)
711+
#plt.savefig('images/10_15.png', dpi=300)
712712
plt.show()
713713

714714

ch10/images/10_10.png

930 Bytes
Loading

ch10/images/10_11.png

-64.2 KB
Loading

ch10/images/10_12.png

45.8 KB
Loading

ch10/images/10_13.png

-34.9 KB
Loading

ch10/images/10_14.png

48.7 KB
Loading

ch10/images/10_15.png

-1.19 MB
Loading

ch11/ch11.ipynb

Lines changed: 23 additions & 23 deletions
Large diffs are not rendered by default.

ch11/ch11.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@
3232

3333

3434

35-
# *The use of `watermark` is optional. You can install this IPython extension via "`pip install watermark`". For more information, please see: https://github.com/rasbt/watermark.*
35+
# *The use of `watermark` is optional. You can install this Jupyter extension via*
36+
#
37+
# conda install watermark -c conda-forge
38+
#
39+
# or
40+
#
41+
# pip install watermark
42+
#
43+
# *For more information, please see: https://github.com/rasbt/watermark.*
3644

3745

3846
# ### Overview
@@ -101,22 +109,22 @@
101109
X[y_km == 0, 1],
102110
s=50, c='lightgreen',
103111
marker='s', edgecolor='black',
104-
label='cluster 1')
112+
label='Cluster 1')
105113
plt.scatter(X[y_km == 1, 0],
106114
X[y_km == 1, 1],
107115
s=50, c='orange',
108116
marker='o', edgecolor='black',
109-
label='cluster 2')
117+
label='Cluster 2')
110118
plt.scatter(X[y_km == 2, 0],
111119
X[y_km == 2, 1],
112120
s=50, c='lightblue',
113121
marker='v', edgecolor='black',
114-
label='cluster 3')
122+
label='Cluster 3')
115123
plt.scatter(km.cluster_centers_[:, 0],
116124
km.cluster_centers_[:, 1],
117125
s=250, marker='*',
118126
c='red', edgecolor='black',
119-
label='centroids')
127+
label='Centroids')
120128
plt.legend(scatterpoints=1)
121129
plt.grid()
122130
plt.tight_layout()
@@ -219,17 +227,17 @@
219227
c='lightgreen',
220228
edgecolor='black',
221229
marker='s',
222-
label='cluster 1')
230+
label='Cluster 1')
223231
plt.scatter(X[y_km == 1, 0],
224232
X[y_km == 1, 1],
225233
s=50,
226234
c='orange',
227235
edgecolor='black',
228236
marker='o',
229-
label='cluster 2')
237+
label='Cluster 2')
230238

231239
plt.scatter(km.cluster_centers_[:, 0], km.cluster_centers_[:, 1],
232-
s=250, marker='*', c='red', label='centroids')
240+
s=250, marker='*', c='red', label='Centroids')
233241
plt.legend()
234242
plt.grid()
235243
plt.tight_layout()
@@ -330,7 +338,7 @@
330338

331339

332340

333-
# 3. correct approach: Input sample matrix
341+
# 3. correct approach: Input matrix
334342

335343
row_clusters = linkage(df.values, method='complete', metric='euclidean')
336344
pd.DataFrame(row_clusters,
@@ -452,15 +460,15 @@
452460
y_ac = ac.fit_predict(X)
453461
ax2.scatter(X[y_ac == 0, 0], X[y_ac == 0, 1], c='lightblue',
454462
edgecolor='black',
455-
marker='o', s=40, label='cluster 1')
463+
marker='o', s=40, label='Cluster 1')
456464
ax2.scatter(X[y_ac == 1, 0], X[y_ac == 1, 1], c='red',
457465
edgecolor='black',
458-
marker='s', s=40, label='cluster 2')
466+
marker='s', s=40, label='Cluster 2')
459467
ax2.set_title('Agglomerative clustering')
460468

461469
plt.legend()
462470
plt.tight_layout()
463-
# plt.savefig('images/11_15.png', dpi=300)
471+
#plt.savefig('images/11_15.png', dpi=300)
464472
plt.show()
465473

466474

@@ -474,11 +482,11 @@
474482
plt.scatter(X[y_db == 0, 0], X[y_db == 0, 1],
475483
c='lightblue', marker='o', s=40,
476484
edgecolor='black',
477-
label='cluster 1')
485+
label='Cluster 1')
478486
plt.scatter(X[y_db == 1, 0], X[y_db == 1, 1],
479487
c='red', marker='s', s=40,
480488
edgecolor='black',
481-
label='cluster 2')
489+
label='Cluster 2')
482490
plt.legend()
483491
plt.tight_layout()
484492
#plt.savefig('images/11_16.png', dpi=300)

0 commit comments

Comments
 (0)