python,不同的模型预测人脸

通过python调用不同的模型去预测人脸

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 17 09:26:18 2020

@author: guangjie2333
"""

import matplotlib.pyplot as plt
import numpy as np

from sklearn.utils.validation import check_random_state
from sklearn.datasets import fetch_olivetti_faces

from sklearn.ensemble import ExtraTreesRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression
from sklearn.neural_network import MLPRegressor

from sklearn.ensemble import RandomForestRegressor

data, targets = fetch_olivetti_faces(return_X_y = True)


train = data[targets < 30]
test = data[targets >= 30]

#test on face data
n_faces = 5;
rng = check_random_state(4);
face_ids = rng.randint(test.shape[0],size = (n_faces,));
test = test[face_ids,:];


n_pixels = data.shape[1]

x_train = train[:,:(n_pixels + 1)//2]
y_train = train[:,n_pixels//2:];

x_test = test[:,:(n_pixels + 1)//2]
y_test = test[:,n_pixels//2:]



# Fit estimators
ESTIMATORS = {
    "Extra trees": ExtraTreesRegressor(n_estimators=10, max_features=32,
                                       random_state=0),
    "K-nn": KNeighborsRegressor(),
    "Linear regression": LinearRegression(),
    "MLP": MLPRegressor(),
    "Random Tree": RandomForestRegressor(n_estimators=10,max_features=5, max_depth=5,min_samples_split=2)
}

y_test_predict = dict()
for name, estimator in ESTIMATORS.items():
    estimator.fit(x_train, y_train)
    y_test_predict[name] = estimator.predict(x_test)


#plot face   
image_shape = (64, 64)
 
n_cols = 1 + len(ESTIMATORS)
plt.figure(figsize=(10, 10))
plt.suptitle("Face completion with multi-output estimators", size=30)
 
for i in range(n_faces):
    true_face = np.hstack((x_test[i], y_test[i]))
   #画真脸
    if i:
        sub = plt.subplot(n_faces, n_cols, i * n_cols + 1)
    else:
        #第0行加标题
        sub = plt.subplot(n_faces, n_cols, i * n_cols + 1,
                          title="true faces")
 
    sub.axis("off")
    sub.imshow(true_face.reshape(image_shape),#照片大小
               cmap=plt.cm.gray,#灰度图
               interpolation="nearest")#内差值方式
 
    
    #画预测脸
    for j, est in enumerate(sorted(ESTIMATORS)): #j代表元素下标, est代表元素
        completed_face = np.hstack((x_test[i], y_test_predict[est][i]))
 
        if i: #j用来控制图像的位置
            sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j)
 
        else: #第0行加标题
            sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j,
                              title=est)
 
        sub.axis("off")
        sub.imshow(completed_face.reshape(image_shape),
                    cmap=plt.cm.gray,
                    interpolation="nearest")
 
plt.show()


讨论RandomForestRegressor模型n_estimators, max_depth,max_features对结果的影响

n_estimators取10,20,30,40
max_depth取 3,6,9,12
max_features取 5,8,11,14

n_estimators = 10,max_depth = 3,max_features = 5

在这里插入图片描述

n_estimators = 20,max_depth = 3,max_features = 5

在这里插入图片描述

n_estimators = 30,max_depth = 3,max_features = 5

在这里插入图片描述

n_estimators = 40,max_depth = 3,max_features = 5

在这里插入图片描述

n_estimators = 10,max_depth = 6,max_features = 5

在这里插入图片描述

n_estimators = 10,max_depth = 9,max_features = 5

在这里插入图片描述

n_estimators = 10,max_depth = 12,max_features = 5

在这里插入图片描述

n_estimators = 10,max_depth = 3,max_features = 8

在这里插入图片描述

n_estimators = 10,max_depth = 3,max_features = 11

在这里插入图片描述

n_estimators = 10,max_depth = 3,max_features = 14

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鄢广杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值