人脸自动补全

本博客通过使用sklearn库中的Olivetti人脸数据集,对比了KNN、线性回归、岭回归和Lasso回归四种算法在预测人脸下半部分特征上的性能。实验结果显示,KNN在准确率和预测时间上表现最佳。
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces()
# faces.images
faces.data.shape
(400, 4096)
faces.images.shape
(400, 64, 64)
# 总共40张脸   每张脸10张图片
faces.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])
plt.imshow(faces.data[129].reshape(64,64),cmap='gray')
<matplotlib.image.AxesImage at 0x226821a3b00>

在这里插入图片描述

X_train = []  # 每个人前8张照片的上半边脸数据
y_train = []  # 每个人前8张照片的下半边脸数据
X_test = []  # 每个人后2张照片的上半边脸数据
y_test = []  # 每个人后2张照片的下半边脸数据

data = faces.data # 4096 
# display(data)
target = faces.target 

for i in range(40):
    for j in range(10):
        index = 10*i+j
        face = data[index]
        up_face = face[:2048]
        bottom_face = face[2048:]
        if j < 8:
            X_train.append(up_face)
            y_train.append(bottom_face)
        else:
            X_test.append(up_face)
            y_test.append(bottom_face)
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression,Ridge,Lasso

knn = KNeighborsRegressor()
linear = LinearRegression()
ridge = Ridge(alpha=10)
lasso = Lasso()

print('knn fit time is')
%time knn.fit(X_train,y_train)

print('linear fit time is')
%time linear.fit(X_train,y_train)

print('ridge fit time is')
%time ridge.fit(X_train,y_train)

print('lasso fit time is')
%time lasso.fit(X_train,y_train)
knn fit time is
Wall time: 61 ms
linear fit time is
Wall time: 1.01 s
ridge fit time is
Wall time: 266 ms
lasso fit time is
Wall time: 24.5 s





Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
   normalize=False, positive=False, precompute=False, random_state=None,
   selection='cyclic', tol=0.0001, warm_start=False)
# 从预测时间上看,岭回归效果最好,Lasso其次,最慢的就是knn
# 从训练时间上看,knn最快,岭回归其次,最慢的就是Lasso
# 准确率最高的是knn  缩减系数Lasso要由于岭回归

%timeit knn_y_ = knn.predict(X_test)
133 ms ± 7.25 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%time line_y_ = linear.predict(X_test)
Wall time: 36 ms
# ridge.fit(X_train,y_train)
%timeit ridge_y_ = ridge.predict(X_test)
68.7 ms ± 9.71 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit lasso_y_ = lasso.predict(X_test)
64.4 ms ± 7.88 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
knn_y_ = knn.predict(X_test)
lasso_y_ = lasso.predict(X_test)
# 使用r2_score来查看算法评分
from sklearn.metrics import r2_score
print('knn r2 score is %f' % r2_score(y_test,knn_y_))
print('linear r2 score is %f' % r2_score(y_test,line_y_))
print('ridge r2 score is %f' % r2_score(y_test,ridge_y_))
print('lasso r2 score is %f' % r2_score(y_test,lasso_y_))
knn r2 score is 0.539090
linear r2 score is 0.207012
ridge r2 score is 0.565036
lasso r2 score is -0.029539
print('ridge r2 score is %f' % r2_score(y_test,ridge_y_))
ridge r2 score is 0.565036
plt.subplot?
# 以图形的方式来查看各种模型的效果哪个最好
# 随机抽出5张人脸(从测试数据里抽取),绘制出5列,第一列为真实人脸数据,后四列为预测人脸数据
results = {'KNN':knn_y_,'Linear':line_y_,'Ridge':ridge_y_,'Lasso':lasso_y_}


plt.figure(figsize=(16,16))
for i in range(5):
    axes = plt.subplot(5,5,i*5+1)
    true_up_face = X_train[i].reshape(32,64)
    true_bottom_face = y_train[i].reshape(32,64)
    true_face = np.concatenate((true_up_face,true_bottom_face),axis=0)
    plt.imshow(true_face,cmap='gray')
    axes.set_title('True')
    j=2
    for key,value in results.items(): 
        axes = plt.subplot(5,5,i*5+j)
        j += 1
        axes.set_title(key)
        pre_bottom_face = value[i].reshape(32,64)
        pre_face = np.concatenate((true_up_face,pre_bottom_face),axis=0)
        plt.imshow(pre_face,cmap='gray')

在这里插入图片描述

results = {'KNN':knn_y_,'Linear':line_y_,'Rige':ridge_y_,'Lasso':lasso_y_}
for key,value in results.items():
    print(key,value)
KNN [[0.43223137 0.4471074  0.5371901  ... 0.19421487 0.20743802 0.20413224]
 [0.59421486 0.6231405  0.6586777  ... 0.22727272 0.2338843  0.23719008]
 [0.6404959  0.6801653  0.7        ... 0.14214876 0.13801654 0.12396693]
 ...
 [0.3272727  0.3578512  0.38099173 ... 0.26528925 0.25785127 0.23966941]
 [0.35123968 0.3289256  0.3371901  ... 0.49008265 0.4867769  0.49008265]
 [0.43884295 0.5008265  0.54380167 ... 0.3859504  0.35619834 0.36694214]]
Linear [[0.40870795 0.45331472 0.55208606 ... 0.25845155 0.22177044 0.19537821]
 [0.46658427 0.51696014 0.58401066 ... 0.4853812  0.3403119  0.40044165]
 [0.7306797  0.7508561  0.7530964  ... 0.05081594 0.05193704 0.0270783 ]
 ...
 [0.16949676 0.18547818 0.30312285 ... 0.29799515 0.18631995 0.16171998]
 [0.31773785 0.29735655 0.3136961  ... 0.70708776 0.7010121  0.6402804 ]
 [0.47083372 0.45964605 0.4611654  ... 0.23720905 0.29146925 0.3613352 ]]
Rige [[0.44925849 0.48035248 0.53267095 ... 0.19611137 0.17849501 0.17461001]
 [0.51226013 0.52081839 0.54458618 ... 0.35944359 0.34695463 0.36239677]
 [0.68595123 0.71005346 0.76813295 ... 0.23668685 0.24897193 0.23032277]
 ...
 [0.23678788 0.29149605 0.35663582 ... 0.27713162 0.27068595 0.2696447 ]
 [0.28967593 0.30964534 0.315537   ... 0.60672389 0.5761973  0.54874882]
 [0.44050149 0.45080497 0.45805986 ... 0.32140219 0.31479703 0.3767172 ]]
Lasso [[0.51624484 0.54340134 0.57360537 ... 0.31840134 0.31004649 0.30617252]
 [0.51624484 0.54340134 0.57360537 ... 0.31840134 0.31004649 0.30617252]
 [0.51624484 0.54340134 0.57360537 ... 0.31840134 0.31004649 0.30617252]
 ...
 [0.51624484 0.54340134 0.57360537 ... 0.31840134 0.31004649 0.30617252]
 [0.51624484 0.54340134 0.57360537 ... 0.31840134 0.31004649 0.30617252]
 [0.51624484 0.54340134 0.57360537 ... 0.31840134 0.31004649 0.30617252]]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值