繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

机器学习-人脸预测

作者/量化王者 2019-08-25 14:55 0 来源: FX168财经网人物频道

导入需求库¶

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.

小小学习,见笑了。¶

 
分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 李冉晴

    人气2096文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 张迎妤

    人气1752文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 刘钥钥1

    人气1944文章3119粉丝34

    专业从事现货黄金、现货白银模似实盘操作分析指导

  • 梁孟梵

    人气1960文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 金帝财神

    人气4520文章8329粉丝117

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...

  • 金泰铬J

    人气2160文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 指导老师

    人气1752文章4423粉丝52

    暂无个人简介信息

  • 金算盘

    人气2520文章7761粉丝124

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 张亦巧

    人气2024文章4145粉丝45

    暂无个人简介信息