from sklearn.linear_model import SGDRegressor
from sklearn import datasets
from sklearn.model_selection import train_test_split#将数据分为测试集和训练集
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np
x=np.empty((100,2))
x[:,0]=np.random.uniform(0,100,100)
x[:,1]=0.75*x[:,0]+3+np.random.normal(0,10,size=100)
pca=PCA(n_components=1)
pca.fit(x)
print(pca.components_)
x_reducation=pca.transform(x)
x_restore=pca.inverse_transform(x_reducation)
plt.scatter(x[:,0],x[:,1],color="black",alpha=0.5)
plt.scatter(x_reducation,np.zeros((100,1)),color="red",alpha=0.5)
plt.scatter(x_restore[:,0],x_restore[:,1],color="y",alpha=0.5)
plt.show()