import numpy as np
data = np.array([[45,37,42,35], [38,31,26,28], [10,15,17,21]]).T # 5*3 matrix
print(data)
avg = np.average(data, axis=0)
#std = np.std(data, axis=0)
data = data - avg
#data = data/std
covariance = data.T.dot(data)/(data.shape[0]-1)
print(covariance)
# u is the eigenvector
u, s, v = np.linalg.svd(covariance)
print(u, s)
# v is the eigenvector
w, v = np.linalg.eig(covariance)
print(v, w)
# Reduced features m*k
Z = data.dot(v[:, :2])
print(Z)
ratio = np.sum(w[:2])/np.sum(w)
print(ratio)
X_app = Z.dot(v[:, :2].T)
X_app = X_app + avg
print(X_app)
data = np.array([[45,37,42,35], [38,31,26,28], [10,15,17,21]]).T # 5*3 matrix
print(data)
from sklearn.decomposition import PCA
pca = PCA()
pca.fit(data)
Z = pca.transform(data)
print(Z)
print(pca.components_) # principle components
print(pca.get_covariance()) # sigma
print(pca.explained_variance_) # eign values
print(pca.explained_variance_ratio_) # percentage of variance
X_app = pca.inverse_transform(Z)
print(X_app)
# choose the number of PC with 95% variance
cumsum = np.cumsum(pca.explained_variance_ratio_)
d = np.argmax(cumsum >= 0.95) + 1
# choose the ratio of variance to preserve
pca = PCA(n_components=0.95)
# search elbow
import matplotlib.pyplot as plt
fig, ax = plt.subplots();
ax.plot(range(len(pca.explained_variance_ratio_)), cumsum);
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
data_X, data_Y = mnist['data'], mnist['target']
from sklearn.decomposition import IncrementalPCA
n_batches = 100
pca = IncrementalPCA(n_components=154)
for X_batch in np.array_split(data_X, n_batches):
pca.partial_fit(X_batch)
X_reduced = pca.transform(data_X)
from sklearn.datasets import make_swiss_roll
X, t = make_swiss_roll(n_samples=1000, noise=0.2, random_state=42)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import proj3d
axes = [-11.5, 14, -2, 23, -12, 15]
fig = plt.figure(figsize=(6, 5))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=t, cmap=plt.cm.hot)
ax.view_init(10, -70)
ax.set_xlabel("$x_1$", fontsize=18)
ax.set_ylabel("$x_2$", fontsize=18)
ax.set_zlabel("$x_3$", fontsize=18)
from sklearn.decomposition import KernelPCA
pca = PCA(n_components=2) # default PCA, equals to linear PCA
X_default = pca.fit_transform(X)
rbf_pca = KernelPCA(n_components = 2, kernel='rbf', gamma=0.04) # rbf kernel
X_rbf = rbf_pca.fit_transform(X)
linear_pca = KernelPCA(n_components = 2, kernel= 'linear') # linear kernel
X_linear = linear_pca.fit_transform(X)
sig_pca = KernelPCA(n_components = 2, kernel='sigmoid', gamma=0.001) # sigmoid kernel
X_sig = sig_pca.fit_transform(X)
f, axs = plt.subplots(2, 2, figsize=(11, 8))
axs[0, 0].scatter(X_default[:, 0], X_default[:, 1], c=t, cmap=plt.cm.hot)
axs[0, 1].scatter(X_linear[:, 0], X_linear[:, 1], c=t, cmap=plt.cm.hot)
axs[1, 0].scatter(X_rbf[:, 0], X_rbf[:, 1], c=t, cmap=plt.cm.hot)
axs[1, 1].scatter(X_sig[:, 0], X_sig[:, 1], c=t, cmap=plt.cm.hot)
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
clf = Pipeline([
('kpca', KernelPCA(n_components=2)),
('log_reg', LogisticRegression()),
])
param_grid = [{
'kpca__gamma': np.linspace(0.03, 0.05, 10),
'kpca__kernel': ['rbf', 'sigmoid'],
}]
grid_search = GridSearchCV(clf, param_grid, cv=3)
grid_search.fit(X, y)
# Select with the lowest reconstruction error
rbf_pca = KernelPCA(n_components = 2, kernel='rbf', gamma=0.0433, fit_inverse_transform=True)
X_rbf = rbf_pca.fit_transform(X);
X_preimage = rbf_pca.inverse_transform(X_rbf)
# Original Plot
fig = plt.figure(figsize=(18, 6))
ax = fig.add_subplot(1, 3, 1, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=t, cmap=plt.cm.hot)
ax.view_init(10, -70)
# Reduced Plot
ax = fig.add_subplot(1, 3, 2)
ax.scatter(X_rbf[:, 0], X_rbf[:, 1], c=t, cmap=plt.cm.hot)
# Pre-image Plot
ax = fig.add_subplot(1, 3, 3, projection='3d')
ax.scatter(X_preimage[:, 0], X_preimage[:, 1], X_preimage[:, 2], c=t, cmap=plt.cm.hot)
from sklearn.metrics import mean_squared_error
mean_squared_error(X, X_preimage)
from sklearn.manifold import LocallyLinearEmbedding
lle = LocallyLinearEmbedding(n_components=2, n_neighbors=8)
X_reduced = lle.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
lle = LocallyLinearEmbedding(n_components=2, n_neighbors=8, method = 'modified')
X_reduced = lle.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
lle = LocallyLinearEmbedding(n_components=2, n_neighbors=8, method = 'hessian')
X_reduced = lle.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
lle = LocallyLinearEmbedding(n_components=2, n_neighbors=8, method = 'ltsa')
X_reduced = lle.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
from sklearn import random_projection
rp_gaussian = random_projection.GaussianRandomProjection(n_components=2)
X_reduced = rp_gaussian.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
rp_sparse = random_projection.SparseRandomProjection(n_components=2)
X_reduced = rp_sparse.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
# metric MDS
from sklearn.manifold import MDS
embedding = MDS(n_components=2)
X_reduced = embedding.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
# nonmetric MDS
from sklearn.manifold import MDS
embedding = MDS(n_components=2, metric=False)
X_reduced = embedding.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
from sklearn.manifold import Isomap
embedding = Isomap(n_components=2)
X_reduced = embedding.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
from sklearn.manifold import SpectralEmbedding
embedding = SpectralEmbedding(n_components=2)
X_reduced = embedding.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)
* Revealing the structure at many scales on a single map
* Revealing data that lie in multiple, different, manifolds or clusters
* Reducing the tendency to crowd points together at the center
* might be beneficial to visually disentangle a dataset that comprises several manifolds at once
* Note that the KL divergence is not convex, i.e. multiple restarts with different initializations will end up in local minima of the KL divergence
* t-SNE is computationally expensive
* The Barnes-Hut t-SNE method is limited to two or three dimensional embeddings
* Global structure is not explicitly preserved. This problem is mitigated by initializing points with PCA.
from sklearn.manifold import TSNE
embedding = TSNE(n_components=2, init='pca')
X_reduced = embedding.fit_transform(X)
fig, ax = plt.subplots()
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=t, cmap=plt.cm.hot)