『sklearn学习』多种模型预测脸的下半部分的结果对比
2016-11-30 13:24
555 查看
# 预测脸的下半部分 import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import fetch_olivetti_faces from sklearn.utils.validation import check_random_state from sklearn.ensemble import ExtraTreesRegressor from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_model import LinearRegression from sklearn.linear_model import RidgeCV data = fetch_olivetti_faces() targets = data.target print data.images data = data.images.reshape((len(data.images), -1)) train = data[targets < 30] # data 和 target 的行数必须要相同 test = data[targets >= 30] n_faces = 5 rng = check_random_state(4) """ check_random_state 函数说明: 将种子转换为np.random.RandomState实例 如果seed为None,返回np.random使用的RandomState单例。 如果seed是一个int,返回一个新的RandomState实例种子。 如果seed已经是一个RandomState实例,则返回它。 否则引发ValueError。 提出修改建议 """ face_ids = rng.randint(test.shape[0], size=(n_faces, )) test = test[face_ids, :] n_pixels = data.shape[1] # 将图像分为上下部分 X_train = train[:, :np.ceil(0.5 * n_pixels)] y_train = train[:, np.floor(0.5 * n_pixels):] X_test = test[:, :np.ceil(0.5 * n_pixels)] y_test = test[:, np.floor(0.5 * n_pixels):] ESTIMATORS = { "Extra trees": ExtraTreesRegressor(n_estimators=10, max_features=32, random_state=0), "K-nn": KNeighborsRegressor(), "Linear regression": LinearRegression(), "Ridge": RidgeCV(), } y_test_predict = dict() for name, estimator in ESTIMATORS.items(): estimator.fit(X_train, y_train) y_test_predict[name] = estimator.predict(X_test) image_shape = (64, 64) n_cols = 1 + len(ESTIMATORS) plt.figure(figsize=(2. * n_cols, 2.26 * n_faces)) plt.suptitle("Face completion with multi-output estimators", size=16) for i in range(n_faces): true_face = np.hstack((X_test[i], y_test[i])) """ hstack 函数说明: 使多维数组变为一维数组,如: >>> c array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]]) >>> np.hstack(c) array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 或者使两个数组合并为一个数组(但是行数必须要相同),如: >>> np.hstack((a, c)) array([[1, 4, 7, 1, 2, 3], [2, 5, 8, 4, 5, 6], [3, 6, 9, 7, 8, 9]], dtype=int64) 其它类似的函数如下: stack:沿着新轴连接数组序列。 vstack:按照垂直(按行)顺序堆叠数组。 dstack:按照深度顺序堆叠数组(沿第三轴)。 concatenate:沿着现有轴连接数组序列。 hsplit:沿第二轴拆分数组 """ if i: sub = plt.subplot(n_faces, n_cols, i * n_cols + 1) else: 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)): completed_face = np.hstack((X_test[i], y_test_predict[est][i])) if i: sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j) else: 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()
相关文章推荐
- Day Four——分别使用Keras和Sklearn训练模型预测结果
- linux内核学习笔记:中断的下半部分
- 【面向对象课程项目:纸牌】Java实例学习(二):优秀源码与自己模型的对比
- 【Java深入学习系列】之CPU的分支预测(Branch Prediction)模型
- DL学习笔记【15】使用训练好的模型得到分类结果
- 【Java深入学习系列】之CPU的分支预测(Branch Prediction)模型
- OPENGL 变换_视图模型变换与部分透视变换_学习笔记
- 系统学习机器学习之随机场(二)--MRF,CRF及几种模型对比
- LIME:模型预测结果是否值得信任?
- R: 学习Gradient Boosting算法,提高预测模型准确率
- 【机器学习实践】人脸识别模型结果对比
- 评分模型的检验方法和标准通常有:K-S指标、交换曲线、AR值、Gini数等。例如,K-S指标是用来衡量验证结果是否优于期望值,具体标准为:如果K-S大于40%,模型具有较好的预测功能,发展的模型具有成功的应用价值。K-S值越大,表示评分模型能够将“好客户”、“坏客户”区分开来的程度越大。
- 【deep learning学习笔记】注释yusugomori的SDA代码 -- Sda.cpp -- 模型训练与预测
- 深入理解 Laravel Eloquent(三)——模型间关系(关联) 在本篇文章中,我将跟大家一起学习 Eloquent 中最复杂也是最难理解的部分——模型间关系。官方英文文档中叫 Relatio
- 认知的概率模型(ESSLLI教程) - 第四部分译文 - 条件推理的学习
- 【加入自己的部分内容】Linux设备驱动模型学习之基础篇--Kobject.txt翻译
- 【HEVC学习与研究】28、第一帧第一个宏块的SAO部分完整解析结果
- scikit-learn初探:多种回归方法建立预测电力输出模型(一)
- 【机器学习实践】人脸识别模型结果对比
- 评分模型的检验方法和标准通常有:K-S指标、交换曲线、AR值、Gini数等。例如,K-S指标是用来衡量验证结果是否优于期望值,具体标准为:如果K-S大于40%,模型具有较好的预测功能,发展的模型具有成功的应用价值。K-S值越大,表示评分模型能够将“好客户”、“坏客户”区分开来的程度越大。