import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

rng=np.random.RandomState(1)
X=np.dot(rng.rand(2,2),rng.randn(2,200)).T
plt.scatter(X[:,0],X[:,1])
plt.axis('equal')
plt.show()

pca=PCA(n_components=2)
pca.fit(X)

def draw_vector(v0,v1,ax=None):
    ax=ax or plt.gca()
    arrow=dict(arrowstyle='->',linewidth=2,shrinkA=0,shrinkB=0)
    ax.annotate('',v0,v1,arrowprops=arrow)

plt.scatter(X[:,0],X[:,1])
for length,vector in zip(pca.explained_variance_,pca.components_):
    v = vector * 3 * np.sqrt(length)
    draw_vector(pca.mean_,pca.mean_ + v)
plt.axis('equal')
plt.show()

pca=PCA(n_components=1)
pca.fit(X)

X_pca=pca.transform(X)
X_new=pca.inverse_transform(X_pca)
plt.scatter(X[:,0],X[:,1])
plt.scatter(X_new[:,0],X_new[:,1],alpha=0.8)
plt.axis('equal')
plt.show()