<jupyter_start><jupyter_text>Principal component analysis  Variance, covariance, and covariance matrices<jupyter_code>import numpy as np

X = np.array([
    [2, 0, -1.4],
    [2.2, 0.2, -1.5],
    [2.4, 0.1, -1],
    [1.9, 0, -1.2]
])

print(np.cov(X).T)
import numpy as np
w, v = np.linalg.eig(np.array([[1, -2], [2, -3]]))
print(w)
print(v)<jupyter_output>[-0.99999998 -1.00000002]
[[ 0.70710678  0.70710678]
 [ 0.70710678  0.70710678]]<jupyter_text>Visualizing high-dimensional data with PCA<jupyter_code>import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris

data = load_iris()
y = data.target
X = data.data
pca = PCA(n_components=2)
reduced_X = pca.fit_transform(X)

red_x, red_y = [], []
blue_x, blue_y = [], []
green_x, green_y = [], []
for i in range(len(reduced_X)):
    if y[i] == 0:
        red_x.append(reduced_X[i][0])
        red_y.append(reduced_X[i][1])
    elif y[i] == 1:
        blue_x.append(reduced_X[i][0])
        blue_y.append(reduced_X[i][1])
    else:
        green_x.append(reduced_X[i][0])
        green_y.append(reduced_X[i][1])
plt.scatter(red_x, red_y, c='r', marker='x')
plt.scatter(blue_x, blue_y, c='b', marker='D')
plt.scatter(green_x, green_y, c='g', marker='.')
plt.show()<jupyter_output><empty_output><jupyter_text>Face recognition with PCA<jupyter_code>import os
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import cross_val_score
from sklearn.preprocessing import scale
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from PIL import Image

X = []
y = []

for dirpath, _, filenames in os.walk('att_faces'):
    for filename in filenames:
        if filename[-3:] == 'pgm':
            img = Image.open(os.path.join(dirpath, filename)).convert('L')
            arr = np.array(img).reshape(10304).astype('float32') / 255
            X.append(arr)
            y.append(dirpath)

X = scale(X)
X_train, X_test, y_train, y_test = train_test_split(X, y)
pca = PCA(n_components=150)
X_train_reduced = pca.fit_transform(X_train)
X_test_reduced = pca.transform(X_test)
print(X_train.shape)
print(X_train_reduced.shape)
classifier = LogisticRegression()
accuracies = cross_val_score(classifier, X_train_reduced, y_train)
print('Cross validation accuracy: %s' % np.mean(accuracies))
classifier.fit(X_train_reduced, y_train)
predictions = classifier.predict(X_test_reduced)
print(classification_report(y_test, predictions))<jupyter_output>(300, 10304)
(300, 150)
Cross validation accuracy: 0.791103073156
               precision    recall  f1-score   support

 att_faces\s1       0.50      1.00      0.67         1
att_faces\s10       1.00      1.00      1.00         3
att_faces\s11       1.00      1.00      1.00         2
att_faces\s12       1.00      1.00      1.00         3
att_faces\s13       1.00      1.00      1.00         2
att_faces\s15       1.00      0.60      0.75         5
att_faces\s16       0.50      1.00      0.67         1
att_faces\s17       1.00      1.00      1.00         3
att_faces\s18       1.00      1.00      1.00         3
att_faces\s19       1.00      1.00      1.00         2
 att_faces\s2       0.50      1.00      0.67         2
att_faces\s20       1.00      1.00      1.00         2
att_faces\s21       1.00      0.83      0.91         6
att_faces\s22       1.00      1.00      1.00         4
att_faces\s23       1.00      1.00      1.00         2
att_faces\s24       0.75      1.00      0.86         [...]
{'fraction_non_alphanumeric': 0.12444071588366891, 'fraction_numerical': 0.08892617449664429, 'mean_word_length': 2.506862745098039, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '69644', 'license': 'MIT', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1350, 'n_words': 285, 'path': 'Section 14/Section 14.ipynb', 'repo_name': 'robson-koji/Machine-Learning-with-Scikit-learn', 'size': '3576'}