This commit is contained in:
packtBhagyashree committed 2017-09-19 15:50:43 +05:30
1 parent 1cf55acd75
commit 96f3e58e99
51 files changed
+1120 -1053

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
Python Machine Learning - Code Examples
## Chapter 5: Compressing Data via Dimensionality Reduction
### Chapter Outline
- Unsupervised dimensionality reduction via principal component analysis
- The main steps behind principal component analysis
- Extracting the principal components step by step
- Total and explained variance
- Feature transformation
- Principal component analysis in scikit-learn
- Supervised data compression via linear discriminant analysis
- Principal component analysis versus linear discriminant analysis
- The inner workings of linear discriminant analysis
- Computing the scatter matrices
- Selecting linear discriminants for the new feature subspace
- Projecting samples onto the new feature space
- LDA via scikit-learn
- Using kernel principal component analysis for nonlinear mappings
- Kernel functions and the kernel trick
- Implementing a kernel principal component analysis in Python
- Example 1 – separating half-moon shapes
- Example 2 – separating concentric circles
- Projecting new data points
- Kernel principal component analysis in scikit-learn
- 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: [`ch05.ipynb`](ch05.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.
+1 -10
View File
@@ -2125,22 +2125,13 @@
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook ch05.ipynb to script\n",
"[NbConvertApp] Writing 27544 bytes to ch05.py\n"
"[NbConvertApp] Writing 27705 bytes to ch05.py\n"
]
}
],
"source": [
"! python ../.convert_notebook_to_script.py --input ch05.ipynb --output ch05.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
+25 -77
View File
@@ -1,6 +1,22 @@
# coding: utf-8
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from matplotlib.colors import ListedColormap
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from scipy.spatial.distance import pdist, squareform
from scipy import exp
from scipy.linalg import eigh
from sklearn.datasets import make_moons
from sklearn.datasets import make_circles
from sklearn.decomposition import KernelPCA
# *Python Machine Learning 2nd Edition* by [Sebastian Raschka](https://sebastianraschka.com), Packt Publishing Ltd. 2017
#
# Code Repository: https://github.com/rasbt/python-machine-learning-book-2nd-edition
@@ -13,7 +29,6 @@
# Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s).
# In[1]:
@@ -46,27 +61,22 @@
# - [Summary](#Summary)
# In[2]:
from IPython.display import Image
# # Unsupervised dimensionality reduction via principal component analysis
# ## The main steps behind principal component analysis
# In[3]:
# ## Extracting the principal components step-by-step
# In[4]:
import pandas as pd
df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/'
'machine-learning-databases/wine/wine.data',
@@ -87,14 +97,11 @@ df_wine.columns = ['Class label', 'Alcohol', 'Malic acid', 'Ash',
df_wine.head()
# <hr>
# Splitting the data into 70% training and 30% test subsets.
# In[5]:
from sklearn.model_selection import train_test_split
X, y = df_wine.iloc[:, 1:].values, df_wine.iloc[:, 0].values
@@ -105,10 +112,8 @@ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
# Standardizing the data.
# In[6]:
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
@@ -161,10 +166,8 @@ X_test_std = sc.transform(X_test)
# Eigendecomposition of the covariance matrix.
# In[7]:
import numpy as np
cov_mat = np.cov(X_train_std.T)
eigen_vals, eigen_vecs = np.linalg.eig(cov_mat)
@@ -181,7 +184,6 @@ print('\nEigenvalues \n%s' % eigen_vals)
# ## Total and explained variance
# In[8]:
tot = sum(eigen_vals)
@@ -189,10 +191,8 @@ var_exp = [(i / tot) for i in sorted(eigen_vals, reverse=True)]
cum_var_exp = np.cumsum(var_exp)
# In[9]:
import matplotlib.pyplot as plt
plt.bar(range(1, 14), var_exp, alpha=0.5, align='center',
@@ -210,7 +210,6 @@ plt.show()
# ## Feature transformation
# In[10]:
# Make a list of (eigenvalue, eigenvector) tuples
@@ -221,7 +220,6 @@ eigen_pairs = [(np.abs(eigen_vals[i]), eigen_vecs[:, i])
eigen_pairs.sort(key=lambda k: k[0], reverse=True)
# In[11]:
w = np.hstack((eigen_pairs[0][1][:, np.newaxis],
@@ -240,13 +238,11 @@ print('Matrix W:\n', w)
# then $-v$ is also an eigenvector that has the same eigenvalue, since
# $$\Sigma \cdot (-v) = -\Sigma v = -\lambda v = \lambda \cdot (-v).$$
# In[12]:
X_train_std[0].dot(w)
# In[13]:
X_train_pca = X_train_std.dot(w)
@@ -273,17 +269,14 @@ plt.show()
#
# The following four code cells has been added in addition to the content to the book, to illustrate how to replicate the results from our own PCA implementation in scikit-learn:
# In[14]:
from sklearn.decomposition import PCA
pca = PCA()
X_train_pca = pca.fit_transform(X_train_std)
pca.explained_variance_ratio_
# In[15]:
plt.bar(range(1, 14), pca.explained_variance_ratio_, alpha=0.5, align='center')
@@ -294,7 +287,6 @@ plt.xlabel('Principal components')
plt.show()
# In[16]:
pca = PCA(n_components=2)
@@ -302,7 +294,6 @@ X_train_pca = pca.fit_transform(X_train_std)
X_test_pca = pca.transform(X_test_std)
# In[17]:
plt.scatter(X_train_pca[:, 0], X_train_pca[:, 1])
@@ -311,10 +302,8 @@ plt.ylabel('PC 2')
plt.show()
# In[18]:
from matplotlib.colors import ListedColormap
def plot_decision_regions(X, y, classifier, resolution=0.02):
@@ -347,10 +336,8 @@ def plot_decision_regions(X, y, classifier, resolution=0.02):
# Training logistic regression classifier using the first 2 principal components.
# In[19]:
from sklearn.linear_model import LogisticRegression
pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train_std)
@@ -360,7 +347,6 @@ lr = LogisticRegression()
lr = lr.fit(X_train_pca, y_train)
# In[20]:
plot_decision_regions(X_train_pca, y_train, classifier=lr)
@@ -372,7 +358,6 @@ plt.tight_layout()
plt.show()
# In[21]:
plot_decision_regions(X_test_pca, y_test, classifier=lr)
@@ -384,7 +369,6 @@ plt.tight_layout()
plt.show()
# In[22]:
pca = PCA(n_components=None)
@@ -397,7 +381,6 @@ pca.explained_variance_ratio_
# ## Principal component analysis versus linear discriminant analysis
# In[4]:
@@ -409,7 +392,6 @@ pca.explained_variance_ratio_
# Calculate the mean vectors for each class:
# In[24]:
np.set_printoptions(precision=4)
@@ -422,7 +404,6 @@ for label in range(1, 4):
# Compute the within-class scatter matrix:
# In[25]:
d = 13 # number of features
@@ -439,14 +420,12 @@ print('Within-class scatter matrix: %sx%s' % (S_W.shape[0], S_W.shape[1]))
# Better: covariance matrix since classes are not equally distributed:
# In[26]:
print('Class label distribution: %s'
% np.bincount(y_train)[1:])
# In[27]:
d = 13 # number of features
@@ -460,7 +439,6 @@ print('Scaled within-class scatter matrix: %sx%s' % (S_W.shape[0],
# Compute the between-class scatter matrix:
# In[28]:
mean_overall = np.mean(X_train_std, axis=0)
@@ -480,7 +458,6 @@ print('Between-class scatter matrix: %sx%s' % (S_B.shape[0], S_B.shape[1]))
# Solve the generalized eigenvalue problem for the matrix $S_W^{-1}S_B$:
# In[29]:
eigen_vals, eigen_vecs = np.linalg.eig(np.linalg.inv(S_W).dot(S_B))
@@ -495,7 +472,6 @@ eigen_vals, eigen_vecs = np.linalg.eig(np.linalg.inv(S_W).dot(S_B))
# Sort eigenvectors in descending order of the eigenvalues:
# In[30]:
# Make a list of (eigenvalue, eigenvector) tuples
@@ -512,7 +488,6 @@ for eigen_val in eigen_pairs:
print(eigen_val[0])
# In[31]:
tot = sum(eigen_vals.real)
@@ -532,7 +507,6 @@ plt.tight_layout()
plt.show()
# In[32]:
w = np.hstack((eigen_pairs[0][1][:, np.newaxis].real,
@@ -543,7 +517,6 @@ print('Matrix W:\n', w)
# ## Projecting samples onto the new feature space
# In[33]:
X_train_lda = X_train_std.dot(w)
@@ -566,19 +539,15 @@ plt.show()
# ## LDA via scikit-learn
# In[34]:
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
lda = LDA(n_components=2)
X_train_lda = lda.fit_transform(X_train_std, y_train)
# In[35]:
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr = lr.fit(X_train_lda, y_train)
@@ -591,7 +560,6 @@ plt.tight_layout()
plt.show()
# In[36]:
X_test_lda = lda.transform(X_test_std)
@@ -608,7 +576,6 @@ plt.show()
# # Using kernel principal component analysis for nonlinear mappings
# In[5]:
@@ -616,13 +583,8 @@ plt.show()
# ## Implementing a kernel principal component analysis in Python
# In[38]:
from scipy.spatial.distance import pdist, squareform
from scipy import exp
from scipy.linalg import eigh
import numpy as np
def rbf_kernel_pca(X, gamma, n_components):
"""
@@ -674,11 +636,8 @@ def rbf_kernel_pca(X, gamma, n_components):
# ### Example 1: Separating half-moon shapes
# In[39]:
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
X, y = make_moons(n_samples=100, random_state=123)
@@ -690,10 +649,8 @@ plt.tight_layout()
plt.show()
# In[40]:
from sklearn.decomposition import PCA
scikit_pca = PCA(n_components=2)
X_spca = scikit_pca.fit_transform(X)
@@ -721,7 +678,6 @@ plt.tight_layout()
plt.show()
# In[41]:
X_kpca = rbf_kernel_pca(X, gamma=15, n_components=2)
@@ -751,10 +707,8 @@ plt.show()
# ### Example 2: Separating concentric circles
# In[42]:
from sklearn.datasets import make_circles
X, y = make_circles(n_samples=1000, random_state=123, noise=0.1, factor=0.2)
@@ -766,7 +720,6 @@ plt.tight_layout()
plt.show()
# In[43]:
scikit_pca = PCA(n_components=2)
@@ -795,7 +748,6 @@ plt.tight_layout()
plt.show()
# In[44]:
X_kpca = rbf_kernel_pca(X, gamma=15, n_components=2)
@@ -825,13 +777,8 @@ plt.show()
# ## Projecting new data points
# In[45]:
from scipy.spatial.distance import pdist, squareform
from scipy import exp
from scipy.linalg import eigh
import numpy as np
def rbf_kernel_pca(X, gamma, n_components):
"""
@@ -886,28 +833,24 @@ def rbf_kernel_pca(X, gamma, n_components):
return alphas, lambdas
# In[46]:
X, y = make_moons(n_samples=100, random_state=123)
alphas, lambdas = rbf_kernel_pca(X, gamma=15, n_components=1)
# In[47]:
x_new = X[25]
x_new
# In[48]:
x_proj = alphas[25] # original projection
x_proj
# In[49]:
def project_x(x_new, X, gamma, alphas, lambdas):
@@ -920,7 +863,6 @@ x_reproj = project_x(x_new, X, gamma=15, alphas=alphas, lambdas=lambdas)
x_reproj
# In[50]:
plt.scatter(alphas[y == 0, 0], np.zeros((50)),
@@ -941,10 +883,8 @@ plt.show()
# ## Kernel principal component analysis in scikit-learn
# In[51]:
from sklearn.decomposition import KernelPCA
X, y = make_moons(n_samples=100, random_state=123)
scikit_kpca = KernelPCA(n_components=2, kernel='rbf', gamma=15)
@@ -966,3 +906,11 @@ plt.show()
# # Summary
# ...
# ---
#
# Readers may ignore the next cell.