<jupyter_start><jupyter_text>> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.  8.7. Reducing the dimensionality of a data with a Principal Component Analysis 1. We import NumPy, matplotlib, and scikit-learn.<jupyter_code>import numpy as np
import sklearn
import sklearn.decomposition as dec
import sklearn.datasets as ds
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>2. The Iris flower dataset is available in the *datasets* module of scikit-learn.<jupyter_code>iris = ds.load_iris()
X = iris.data
y = iris.target
print(X.shape)<jupyter_output><empty_output><jupyter_text>3. Each row contains four parameters related to the morphology of the flower. Let's display the first two components in two dimensions. The color reflects the iris variety of the flower (the label, between 0 and 2).<jupyter_code>plt.figure(figsize=(6,3));
plt.scatter(X[:,0], X[:,1], c=y,
            s=30, cmap=plt.cm.rainbow);<jupyter_output><empty_output><jupyter_text>4. We now apply PCA on the dataset to get the transformed matrix. This operation can be done in a single line with scikit-learn: we instantiate a `PCA` model, and call the `fit_transform` method. This function computes the principal components first, and projects the data then.<jupyter_code>X_bis = dec.PCA().fit_transform(X)<jupyter_output><empty_output><jupyter_text>5. We now display the same dataset, but in a new coordinate system (or equivalently, a linearly transformed version of the initial dataset).<jupyter_code>plt.figure(figsize=(6,3));
plt.scatter(X_bis[:,0], X_bis[:,1], c=y,
            s=30, cmap=plt.cm.rainbow);<jupyter_output><empty_output><jupyter_text>Points belonging to the same classes are now grouped together, even though the `PCA` estimator dit *not* use the labels. The PCA was able to find a projection maximizing the variance, which corresponds here to a projection where the classes are well separated. 6. The `scikit.decomposition` module contains several variants of the classic `PCA` estimator: `ProbabilisticPCA`, `SparsePCA`, `RandomizedPCA`, `KernelPCA`... As an example, let's take a look at `KernelPCA`, a non-linear version of PCA.<jupyter_code>X_ter = dec.KernelPCA(kernel='rbf').fit_transform(X)
plt.figure(figsize=(6,3));
plt.scatter(X_ter[:,0], X_ter[:,1], c=y, s=30, cmap=plt.cm.rainbow);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10357286230429547, 'fraction_numerical': 0.012444801284624649, 'mean_word_length': 5.883977900552487, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '348485', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 827, 'n_tokens_neox': 787, 'n_words': 273, 'path': 'notebooks/chapter08_ml/07_pca.ipynb', 'repo_name': 'khanparwaz/PythonProjects', 'size': '2491'}