In [1]:
import warnings
warnings.filterwarnings('ignore')
In [2]:
%matplotlib inline
In [3]:
import numpy as np
from sklearn.decomposition import PCA
In [4]:
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]])
In [5]:
X
Out[5]:
In [6]:
import matplotlib.pyplot as plt
In [7]:
plt.figure(figsize=(10,10))
plt.scatter(X[:, 0], X[:, 1])
# plt.savefig('original.png')
Out[7]:
In [8]:
pca = PCA(n_components=2)
pca.fit(X)
Out[8]:
In [9]:
pca.explained_variance_
Out[9]:
In [10]:
# sum is 1, first pc has a very high variance, i.e. is very good, second could be deleted
pca.explained_variance_ratio_
Out[10]:
In [11]:
X_transformed = pca.transform(X)
X_transformed
Out[11]:
In [12]:
plt.figure(figsize=(10,10))
plt.scatter(X_transformed[:, 0], X_transformed[:, 1])
# plt.savefig('reduced.png')
Out[12]:
In [13]:
pca = PCA(n_components=1)
pca.fit(X)
Out[13]:
In [14]:
pca.explained_variance_
Out[14]:
In [15]:
# sum is 1, first pc has a very high variance, i.e. is very good, second could be deleted
pca.explained_variance_ratio_
Out[15]:
In [16]:
X_transformed = pca.transform(X)
X_transformed
Out[16]:
In [17]:
plt.figure(figsize=(10,10))
plt.plot(X_transformed)
# plt.savefig('reduced.png')
Out[17]:
In [ ]: