<jupyter_start><jupyter_text><jupyter_code>%matplotlib inline
import numpy as np
from sklearn.decomposition import PCA
# X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2], [4, 3], [4, -1]])
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2], [4, 3], [0, 0]])
# X = np.array([[-1, 1], [-2, 2], [-3, 3], [1, 1], [2, 2], [3, 3], [4, 4]])
X
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.scatter(X[:, 0], X[:, 1])
plt.xlim(-4, 4)
plt.ylim(-4, 4)
pca = PCA(n_components=2)
pca.fit(X)
pca.explained_variance_
# sum is 1, first pc has a very high variance, i.e. is very good, second could be deleted
pca.explained_variance_ratio_
pcs = pca.components_
pcs
x_points = pca.transform([[-1, 0], [1, 0]])
x_points
y_points = pca.transform([[0, -1], [0, 1]])
y_points
plt.figure(figsize=(10,10))
plt.scatter(X[:, 0], X[:, 1])
plt.plot(x_points.transpose()[0], x_points.transpose()[1], 'go-')
plt.plot(y_points.transpose()[0], y_points.transpose()[1], 'mo-')
plt.xlim(-4, 4)
plt.ylim(-4, 4)
X
X_transformed = pca.transform(X)
X_transformed
plt.figure(figsize=(10,10))
plt.scatter(X_transformed[:, 0], X_transformed[:, 1])
plt.xlim(-4, 4)
plt.ylim(-4, 4)<jupyter_output><empty_output><jupyter_text>Reduction to 1<jupyter_code>pca = PCA(n_components=1)
pca.fit(X)
pca.explained_variance_
# sum is 1, first pc has a very high variance, i.e. is very good, second could be deleted
pca.explained_variance_ratio_
X_transformed = pca.transform(X)
X_transformed
plt.figure(figsize=(10,2))
plt.scatter(X_transformed, np.zeros(len(X_transformed)))
plt.xlim(-4, 4)
plt.ylim(-1, 1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22215422276621788, 'fraction_numerical': 0.06119951040391677, 'mean_word_length': 5.337209302325581, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '257968', 'license': 'MIT', 'n_tokens_mistral': 793, 'n_tokens_neox': 730, 'n_words': 173, 'path': 'notebooks/unsupervised/pca.ipynb', 'repo_name': 'kmunve/ml-workshop', 'size': '1634'}