请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  数理科学 帖子:3364437 新帖:3

机器学习-人脸预测

量化王者发表于:8 月 25 日 14:55回复(1)

导入需求库¶

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.

小小学习,见笑了。¶

 

全部回复

0/140

量化课程

    移动端课程