问题描述

根据人脸上半脸预测下班脸,数据集在

from sklearn.datasets import fetch_olivetti_faces

代码实现

一开始,我用了库里面的标准线性回归模型进行预测,结果发现效果不太好,以下为一开始的代码与效果

import matplotlib.pyplot as plt
import tensorflow as tf
tf = tf.compat.v1
tf.disable_v2_behavior()
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import r2_score
from sklearn.datasets import fetch_olivetti_faces
import numpy as np
from sklearn.linear_model import LinearRegressiondef process_feature(X):scaler=StandardScaler()X=scaler.fit_transform(X)scaler=MinMaxScaler(feature_range=(-1,1))X=scaler.fit_transform(X)return Xdata=fetch_olivetti_faces()
images=data.images
plt.imshow(images[0])
plt.show()data=images.reshape((len(data.images),-1))
n_pixels=data.shape[1]
n_samples=data.shape[0]
X_lode=data[:,:(n_pixels+1)//2]
y_lode=data[:,n_pixels//2:]X_train,X_test,y_train,y_test=train_test_split(X_lode,y_lode,test_size=0.2,random_state=0)
X_train=process_feature(X_train)
X_test=process_feature(X_test)model=LinearRegression()
reg=model.fit(X_train,y_train)
y_pred=model.predict(X_test)
coef=model.coef_
r2=r2_score(y_test,y_pred)print("coef{}".format(coef))
print("r2={}".format(r2))id=6
true_face=np.hstack((X_test[id],y_test[id]))
pred_face=np.hstack((X_test[id],y_pred[id]))
plt.figure()
image_shape=(64,64)
plt.figure(0)
plt.imshow(true_face.reshape(image_shape),interpolation="nearest")
plt.figure(1)
plt.imshow(pred_face.reshape(image_shape),interpolation="nearest")
plt.show()

下面的效果图还挺可怕的,尽量不要在晚上看

发现这个图是真的不大对,看着只有上面出问题了,想着是不是因为我做了标准化的原因,去掉标准化之后果然图片正确了起来

以下是正确运行代码

import matplotlib.pyplot as plt
import tensorflow as tf
tf = tf.compat.v1
tf.disable_v2_behavior()
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import r2_score
from sklearn.datasets import fetch_olivetti_faces
import numpy as np
from sklearn.linear_model import LinearRegressiondata=fetch_olivetti_faces()
images=data.images
plt.imshow(images[0])
plt.show()data=images.reshape((len(data.images),-1))
n_pixels=data.shape[1]
n_samples=data.shape[0]
X_lode=data[:,:(n_pixels+1)//2]
y_lode=data[:,n_pixels//2:]X_train,X_test,y_train,y_test=train_test_split(X_lode,y_lode,test_size=0.2,random_state=0)model=LinearRegression()
reg=model.fit(X_train,y_train)
y_pred=model.predict(X_test)
coef=model.coef_
r2=r2_score(y_test,y_pred)print("coef{}".format(coef))
print("r2={}".format(r2))id=6
true_face=np.hstack((X_test[id],y_test[id]))
pred_face=np.hstack((X_test[id],y_pred[id]))
plt.figure()
image_shape=(64,64)
plt.figure(0)
plt.imshow(true_face.reshape(image_shape),interpolation="nearest")
plt.figure(1)
plt.imshow(pred_face.reshape(image_shape),interpolation="nearest")
plt.show()

结果图为

线性回归——人脸下半脸预测问题相关推荐

  1. R语言使用lm函数拟合多元线性回归模型、假定预测变量没有交互作用(Multiple linear regression)

    R语言使用lm函数拟合多元线性回归模型.假定预测变量没有交互作用(Multiple linear regression) 目录

  2. 人工智能之基于多变量线性回归的房屋销售价格预测详细解决方案

    人工智能之基于多变量线性回归的房屋销售价格预测详细解决方案 一.目的 学习多变量线性回归模型与梯度下降算法设计机器学习程序,理解并掌握特征归一化,多变量的梯度下降以及正规方程方法,对房屋销售价格进行预 ...

  3. Python建立线性回归模型进行房价预测

    Python建立线性回归模型进行房价预测 前期准备 多因子房价预测 实战流程 1.数据加载 2.数据可视化 3.数据预处理 4.模型建立与训练 5.模型预测 6.模型评估 7.房价预测 数据与代码 前 ...

  4. 一元线性回归python示例——房价预测

    假设房价只有面积一个影响因素,根据下列数据集建立线性回归模型,并预测面积为700平方英尺的房子价格. No square_feet price 1 150 6450 2 200 7450 3 250 ...

  5. scikit-learn线性回归实践 - 波斯顿房价预测

    Educoder实训平台机器学习-线性回归:scikit-learn线性回归实践 - 波斯顿房价预测 (下方代码已成功通过平台测试) 文章目录 机器学习:波士顿房价数据集 任务描述 相关知识 Line ...

  6. 使用线性回归实现波士顿房价预测

    使用线性回归实现波士顿房价预测 本文不进行线性回归基础知识讲解,只提供三种方法对波士顿房价进行预测,这三种方法分别是: 1.使用正规方程的优化方法对波士顿房价进行预测 2.使用梯度下降的优化方法对波士 ...

  7. 《机器学习实战》8.2 线性回归基础篇之预测鲍鱼年龄

    <机器学习实战>8.2 线性回归基础篇之预测鲍鱼年龄 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多人工智能.机器学习干货 csdn:https://blo ...

  8. pytorch-用线性回归梯度下降模型-预测水果产量

    假设有如下数据集, 如何预测水果产量 假设水果产量与地区无关, 与只与温度, 降雨量, 湿度有关, 若用线性模型预测, 可以通过线性回归的方法, 预测产量.加入线性无关, 通过蓝色方框参数可以求解W1 ...

  9. R语言使用lm函数拟合多元线性回归模型、假定预测变量之间有交互作用、R语言使用effects包的effect函数查看交互作用对于回归模型预测响应变量的影响

    R语言使用lm函数拟合多元线性回归模型.假定预测变量之间有交互作用.R语言使用effects包的effect函数查看交互作用对于回归模型预测响应变量的影响 目录

最新文章

  1. CentOS 6.5 安装 Mysql 5.7.* (tar.gz)
  2. 腾讯云linux服务器怎么使用图形化界面_自己搭建一个自动签到和远程下载的服务器...
  3. 【机器学习】27 个机器学习、数学、Python 速查表
  4. iOS之性能优化·列表异步绘制
  5. HDFS的Shell客户端操作
  6. List集合的remove一个对象的方法
  7. 互联网大佬学历背景大揭秘,看看是你的老乡还是校友
  8. Python数据可视化案例二:动态更新数据
  9. Redis数据类型详解(五种)
  10. 电商平台环境下的图像分析在线服务产品——电商图像分析
  11. 从零开始实现一个颜色选择器(原生JavaScript实现)
  12. ERP原理及应用教程-第三章
  13. springboot基于微信小程序的在线考试系统
  14. Keil(MDK)下用仿真器烧程序的同时烧写附加数据到SPI FLASH
  15. Python pyecharts地理数据可视化 绘制地理图表
  16. 海银资本:在大数据创业企业中掘金
  17. 【Python数学建模】SEIR传染病模型模型延伸-SEIDR模型(一),加入疫苗接种、政府管控、病毒变异等因素的影响
  18. Matlab中meshgrid的用法
  19. 线上bug检测工具 android,Android 测试中对于偶现且难以重现的 bug 的处理
  20. RISC-V 实现整数运算指令(Part 1)

热门文章

  1. ROS参数服务器(参数使用详细介绍)
  2. 6 海康视觉平台VisionMaster 上手系列:常用工具(三)
  3. 全球及中国水果罐头行业竞争形势及产销需求分析报告2022-2027年
  4. kafka学习知识点总结(三)
  5. leetcode.887 鸡蛋掉落(Hard)
  6. 腾讯安全发布《2021年全球DDoS威胁报告》:DDoS威胁成犯罪团伙首选勒索手段
  7. Java-高级技术(一)
  8. Visual Studio.NET 2005下载
  9. [实体关系抽取|顶刊论文]UniRel:Unified Representation and Interaction for Joint Relational Triple Extraction
  10. ubuntu(linux) 下 svn 使用kdiff3 merge主干