使用多种回归方法自动补齐人脸
import numpy as np
import matplotlib.pyplot as plt
#构建方程
from sklearn.linear_model import LinearRegression,Ridge,Lasso
#
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn import datasets
from sklearn.model_selection import train_test_split
#data_home:保存的地址
faces=datasets.fetch_olivetti_faces()
# print(faces)
#获取数据
x=faces.data
images=faces.images
y=faces.target
print(x.shape,images.shape,y.shape)
plt.figure(figsize=(2,2))
#随即查看图片
# index=np.random.randint(0,400,size=1)[0]
# img=images[index]
# plt.imshow(img,cmap=plt.cm.gray)
# plt.show()
#将x分成上半张人脸和下半,并打印出来,水平方向一分为二
x_up=x[:,:2048]
x_down=x[:,2048:]
'''
index=np.random.randint(0,400,size=1)[0]
axes=plt.subplot(1,3,1)
img1=x_up[index].reshape(32,64)
axes.imshow(img1,cmap=plt.cm.gray)
axes=plt.subplot(1,3,2)
img2=x_down[index].reshape(32,64)
axes.imshow(img2,cmap=plt.cm.gray)
axes=plt.subplot(1,3,3)
img3=x[index].reshape(64,64)
axes.imshow(img3,cmap=plt.cm.gray)
plt.show()
'''
#根据上半张人脸(x)预测下班张人脸(y)
x=x_up.copy()
y=x_down.copy()
print(x.shape,y.shape)
#分割训练集和数据集
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=30,random_state=1)
estimators={}
estimators[0]=LinearRegression()
estimators[1]=Ridge(alpha=0.1)
estimators[2]=Lasso(alpha=1)
estimators[3]=KNeighborsRegressor(n_neighbors=5)
estimators[4]=DecisionTreeRegressor() #决策树做回归使用的criterion是mse
label=["linear","ridge","lasso","knn","tree"]
result={}
for key,model in estimators.items():
model.fit(x_train,y_train)
y_hat=model.predict(x_test) #预测的是下半张脸
result[label[key]]=y_hat
plt.figure(figsize=(14,20))
#结果可视化
for i in range(0,10):
#第一列,上半张人脸
axes=plt.subplot(10,7,i*7+1)
up_face=x_test[i].reshape(32,64) #上半张连
axes.imshow(u