<jupyter_start><jupyter_text>Unsupervised Transformations<jupyter_code>from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import numpy as np
np.set_printoptions(suppress=True)


digits = load_digits()
X, y = digits.data, digits.target
X_train, X_test, y_train, y_test = train_test_split(X, y)<jupyter_output><empty_output><jupyter_text>Removing mean and scaling variance===================================<jupyter_code>from sklearn.preprocessing import StandardScaler<jupyter_output><empty_output><jupyter_text>1) Instantiate the model<jupyter_code>scaler = StandardScaler()<jupyter_output><empty_output><jupyter_text>2) Fit using only the data.<jupyter_code>scaler.fit(X_train)<jupyter_output><empty_output><jupyter_text>3) `transform` the data (not `predict`).<jupyter_code>X_train_scaled = scaler.transform(X_train)
X_train.shape
X_train_scaled.shape
X_train.mean(axis=0)<jupyter_output><empty_output><jupyter_text>The transformed version of the data has the mean removed:<jupyter_code>X_train_scaled.mean(axis=0)
X_train_scaled.std(axis=0)
X_test_transformed = scaler.transform(X_test)
X_test_transformed.mean(axis=0)<jupyter_output><empty_output><jupyter_text>Principal Component Analysis============================= 0) Import the model<jupyter_code>from sklearn.decomposition import PCA<jupyter_output><empty_output><jupyter_text>1) Instantiate the model<jupyter_code>pca = PCA(n_components=2)<jupyter_output><empty_output><jupyter_text>2) Fit to training data<jupyter_code>pca.fit(X)<jupyter_output><empty_output><jupyter_text>3) Transform to lower-dimensional representation<jupyter_code>print(X.shape)
X_pca = pca.transform(X)
X_pca.shape<jupyter_output><empty_output><jupyter_text>Visualize----------<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["figure.dpi"] = 200

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=plt.cm.tab10(y))<jupyter_output><empty_output><jupyter_text>Manifold Learning==================<jupyter_code>from sklearn.manifold import Isomap
isomap = Isomap()
X_isomap = isomap.fit_transform(X)
plt.figure()
plt.scatter(X_isomap[:, 0], X_isomap[:, 1], c=plt.cm.tab10(y))<jupyter_output><empty_output><jupyter_text>ExercisesVisualize the digits dataset using the TSNE algorithm from the sklearn.manifold module (it runs for a couple of seconds).<jupyter_code># %load solutions/digits_tsne.py<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup