import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.neighbors import KNeighborsRegressor #KNN回归
from sklearn.linear_model import Ridge #岭回归
from sklearn.linear_model import LinearRegression #线性回归
from sklearn.ensemble import ExtraTreesRegressor #随机森林
import sklearn.datasets as datasets #导入内置数据
faces= datasets.fetch_olivetti_faces() #抓取人脸数据
data= faces.data #数据
images= faces.images #图片
print(data.shape, images.shape)
(400, 4096) (400, 64, 64)
#直接数据打印
plt.figure(figsize=(3, 2))
plt.imshow(images[23], cmap="gray")
<matplotlib.image.AxesImage at 0x7fce24b1dd30>
#间接数据打印
print(data[0].shape)
plt.figure(figsize=(3, 2))
plt.imshow(data[0].reshape(64, 64), cmap="gray")
(4096,)
<matplotlib.image.AxesImage at 0x7fce24aac2e8>
target= faces.target
target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39])
target < 30
array([True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False])
#400张脸,300个完整的人脸,100个残缺,
train= data[target < 30]
test= data[target >= 30]
X_train= train[:,:2048]
Y_train= train[:,2048:] #一半的人脸
face_upper= X_train[0].reshape(32, 64)
plt.figure(figsize= (3, 2))
plt.imshow(face_upper, cmap="gray")
<matplotlib.image.AxesImage at 0x7fce24a7c6a0>
X_test= test[:,:2048]
Y_test= test[:,2048:] #测试数据
face_upper= X_test[0].reshape(32, 64)
plt.figure(figsize= (3, 2))
plt.imshow(face_upper, cmap="gray")
<matplotlib.image.AxesImage at 0x7fce24a554a8>
face_upper= Y_test[0].reshape(32, 64)
plt.figure(figsize= (3, 2))
plt.imshow(face_upper, cmap="gray")
<matplotlib.image.AxesImage at 0x7fce249a1eb8>
Estimator= {"KNN": KNeighborsRegressor(),
"LinearRG": LinearRegression(),
"Ridge": Ridge(),
"ExtraTree": ExtraTreesRegressor()} #机器学习算法
y_predicts= dict() #预测之后的数据保存到一个字典
for key, estimator in Estimator.items():
estimator.fit(X_train, Y_train) #训练数据
y_new= estimator.predict(X_test) #预测数据
y_predicts[key]= y_new #结果保存字典
face_id= np.random.randint(0, 100, size= 5) #随机获取区间数据
face_id
array([77, 85, 13, 94, 54])
#图片2,2.5
#5个数据5行
#4个算法+真实,5列
plt.figure(figsize= (6*2,5*2.5))
for i in range(5):
if i:
axes= plt.subplot(5, 6, i*6+1)
else:
axes= plt.subplot(5, 6, i*6+1, title= "True Face")
axes.imshow(test[face_id[i]].reshape(64, 64), cmap= "gray")
if i:
axes= plt.subplot(5, 6, i*6+2)
else:
axes= plt.subplot(5, 6, i*6+2, title= "half Face")
axes.imshow(X_test[face_id[i]].reshape(32, 64), cmap= "gray")
plt.legend()
plt.show()
No handles with labels found to put in legend.
plt.figure(figsize= (6*2,5*2.5)) #设置画布
for i in range(5):
if i:
axes= plt.subplot(5, 6, i*6+1)
else:
axes= plt.subplot(5, 6, i*6+1, title= "True Face")
axes.imshow(test[face_id[i]].reshape(64, 64), cmap= "gray")
for j, key in enumerate(y_predicts): #转换序列
faceup= X_test[face_id[i]] #上半部分
facedown= y_predicts[key][face_id[i]] #下半部分
face= np.hstack((faceup, facedown)).reshape(64, 64) #拼接数据
if i:
axes= plt.subplot(5, 6, i*6+j+2)
else:
axes= plt.subplot(5, 6, i*6+j+2, title= "%s"% list(Estimator.keys())[j])
axes.imshow(face, cmap= "gray")
plt.legend()
plt.show()
No handles with labels found to put in legend.
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程