Skip to content

Commit

Permalink
ch12
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Sep 25, 2019
1 parent d0e7247 commit af706f1
Show file tree
Hide file tree
Showing 31 changed files with 2,364 additions and 92 deletions.
78 changes: 39 additions & 39 deletions ch10/ch10.ipynb

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions ch10/ch10.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,15 @@ def lin_regplot(X, y, model):
y_quad_fit = pr.predict(quadratic.fit_transform(X_fit))

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

plt.tight_layout()
plt.savefig('images/10_10.png', dpi=300)
#plt.savefig('images/10_11.png', dpi=300)
plt.show()


Expand Down Expand Up @@ -563,22 +563,22 @@ def lin_regplot(X, y, model):


# plot results
plt.scatter(X, y, label='training points', color='lightgray')
plt.scatter(X, y, label='Training points', color='lightgray')

plt.plot(X_fit, y_lin_fit,
label='linear (d=1), $R^2=%.2f$' % linear_r2,
label='Linear (d=1), $R^2=%.2f$' % linear_r2,
color='blue',
lw=2,
linestyle=':')

plt.plot(X_fit, y_quad_fit,
label='quadratic (d=2), $R^2=%.2f$' % quadratic_r2,
label='Quadratic (d=2), $R^2=%.2f$' % quadratic_r2,
color='red',
lw=2,
linestyle='-')

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

#plt.savefig('images/10_11.png', dpi=300)
#plt.savefig('images/10_12.png', dpi=300)
plt.show()


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

# plot results
plt.scatter(X_log, y_sqrt, label='training points', color='lightgray')
plt.scatter(X_log, y_sqrt, label='Training points', color='lightgray')

plt.plot(X_fit, y_lin_fit,
label='linear (d=1), $R^2=%.2f$' % linear_r2,
label='Linear (d=1), $R^2=%.2f$' % linear_r2,
color='blue',
lw=2)

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

plt.tight_layout()
#plt.savefig('images/10_12.png', dpi=300)
#plt.savefig('images/10_13.png', dpi=300)
plt.show()


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


Expand Down Expand Up @@ -691,15 +691,15 @@ def lin_regplot(X, y, model):
marker='o',
s=35,
alpha=0.9,
label='training data')
label='Training data')
plt.scatter(y_test_pred,
y_test_pred - y_test,
c='limegreen',
edgecolor='white',
marker='s',
s=35,
alpha=0.9,
label='test data')
label='Test data')

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

# plt.savefig('images/10_14.png', dpi=300)
#plt.savefig('images/10_15.png', dpi=300)
plt.show()


Expand Down
Binary file modified ch10/images/10_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch10/images/10_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch10/images/10_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch10/images/10_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch10/images/10_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch10/images/10_15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 23 additions & 23 deletions ch11/ch11.ipynb

Large diffs are not rendered by default.

36 changes: 22 additions & 14 deletions ch11/ch11.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@



# *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.*
# *The use of `watermark` is optional. You can install this Jupyter extension via*
#
# conda install watermark -c conda-forge
#
# or
#
# pip install watermark
#
# *For more information, please see: https://github.com/rasbt/watermark.*


# ### Overview
Expand Down Expand Up @@ -101,22 +109,22 @@
X[y_km == 0, 1],
s=50, c='lightgreen',
marker='s', edgecolor='black',
label='cluster 1')
label='Cluster 1')
plt.scatter(X[y_km == 1, 0],
X[y_km == 1, 1],
s=50, c='orange',
marker='o', edgecolor='black',
label='cluster 2')
label='Cluster 2')
plt.scatter(X[y_km == 2, 0],
X[y_km == 2, 1],
s=50, c='lightblue',
marker='v', edgecolor='black',
label='cluster 3')
label='Cluster 3')
plt.scatter(km.cluster_centers_[:, 0],
km.cluster_centers_[:, 1],
s=250, marker='*',
c='red', edgecolor='black',
label='centroids')
label='Centroids')
plt.legend(scatterpoints=1)
plt.grid()
plt.tight_layout()
Expand Down Expand Up @@ -219,17 +227,17 @@
c='lightgreen',
edgecolor='black',
marker='s',
label='cluster 1')
label='Cluster 1')
plt.scatter(X[y_km == 1, 0],
X[y_km == 1, 1],
s=50,
c='orange',
edgecolor='black',
marker='o',
label='cluster 2')
label='Cluster 2')

plt.scatter(km.cluster_centers_[:, 0], km.cluster_centers_[:, 1],
s=250, marker='*', c='red', label='centroids')
s=250, marker='*', c='red', label='Centroids')
plt.legend()
plt.grid()
plt.tight_layout()
Expand Down Expand Up @@ -330,7 +338,7 @@



# 3. correct approach: Input sample matrix
# 3. correct approach: Input matrix

row_clusters = linkage(df.values, method='complete', metric='euclidean')
pd.DataFrame(row_clusters,
Expand Down Expand Up @@ -452,15 +460,15 @@
y_ac = ac.fit_predict(X)
ax2.scatter(X[y_ac == 0, 0], X[y_ac == 0, 1], c='lightblue',
edgecolor='black',
marker='o', s=40, label='cluster 1')
marker='o', s=40, label='Cluster 1')
ax2.scatter(X[y_ac == 1, 0], X[y_ac == 1, 1], c='red',
edgecolor='black',
marker='s', s=40, label='cluster 2')
marker='s', s=40, label='Cluster 2')
ax2.set_title('Agglomerative clustering')

plt.legend()
plt.tight_layout()
# plt.savefig('images/11_15.png', dpi=300)
#plt.savefig('images/11_15.png', dpi=300)
plt.show()


Expand All @@ -474,11 +482,11 @@
plt.scatter(X[y_db == 0, 0], X[y_db == 0, 1],
c='lightblue', marker='o', s=40,
edgecolor='black',
label='cluster 1')
label='Cluster 1')
plt.scatter(X[y_db == 1, 0], X[y_db == 1, 1],
c='red', marker='s', s=40,
edgecolor='black',
label='cluster 2')
label='Cluster 2')
plt.legend()
plt.tight_layout()
#plt.savefig('images/11_16.png', dpi=300)
Expand Down
Binary file modified ch11/images/11_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch11/images/11_05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch11/images/11_15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch11/images/11_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions ch12/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Python Machine Learning - Code Examples


## Chapter 12: Implementing a Multilayer Artificial Neural
Network from Scratch

### Chapter Outline

- Modeling complex functions with arti cial neural networks
- Single-layer neural network recap
- Introducing the multilayer neural network architecture
- Activating a neural network via forward propagation
- Classifying handwritten digits
- Obtaining the MNIST dataset
- Implementing a multilayer perceptron
- Training an artificial neural network
- Computing the logistic cost function
- Developing your intuition for backpropagation
- Training neural networks via backpropagation
- About the convergence in neural networks
- A few last words about the neural network implementation
- Summary

### A note on using the code examples

The recommended way to interact with the code examples in this book is via Jupyter Notebook (the `.ipynb` files). Using Jupyter Notebook, you will be able to execute the code step by step and have all the resulting outputs (including plots and images) all in one convenient document.

![](../ch02/images/jupyter-example-1.png)



Setting up Jupyter Notebook is really easy: if you are using the Anaconda Python distribution, all you need to install jupyter notebook is to execute the following command in your terminal:

conda install jupyter notebook

Then you can launch jupyter notebook by executing

jupyter notebook

A window will open up in your browser, which you can then use to navigate to the target directory that contains the `.ipynb` file you wish to open.

**More installation and setup instructions can be found in the [README.md file of Chapter 1](../ch01/README.md)**.

**(Even if you decide not to install Jupyter Notebook, note that you can also view the notebook files on GitHub by simply clicking on them: [`ch12.ipynb`](ch12.ipynb))**

In addition to the code examples, I added a table of contents to each Jupyter notebook as well as section headers that are consistent with the content of the book. Also, I included the original images and figures in hope that these make it easier to navigate and work with the code interactively as you are reading the book.

![](../ch02/images/jupyter-example-2.png)


When I was creating these notebooks, I was hoping to make your reading (and coding) experience as convenient as possible! However, if you don't wish to use Jupyter Notebooks, I also converted these notebooks to regular Python script files (`.py` files) that can be viewed and edited in any plaintext editor.
Loading

0 comments on commit af706f1

Please sign in to comment.