美文网首页
深度学习笔记 - 102 - 回归模型

深度学习笔记 - 102 - 回归模型

作者: Kimichen7764 | 来源:发表于2017-06-30 13:27 被阅读0次
image.png

配置环境

# 选择 Python 2 版本
conda create -n python2 python=2

# 安装 pandas, matplotlib, scikit-learn
conda install pandas matplotlib scikit-learn

Linear Regression:根据动物的大脑重量来预测对应体重

import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt

# Read data
dataframe = pd.read_fwf('brain_body.txt')
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]

# Train model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)

# Visualize results
plt.scatter(x_values, y_values)
plt.plot(x_values, body_reg.predict(x_values))
plt.show()

Breaking it Down

  1. Machine Learing - Defining the outcome & letting our algorithm learn the steps to get there

  2. 3 Learning styles - supervised, unsupervised & reinforcement learning

  3. Linear Regression models relationship between independent & dependent variables via line of best fit

线性回归练习:各国男性人口的 BMI 与该国人口平均寿命的回归

其中 "Country" 列记录出生国家,"Life expectancy" 列记录该国平均寿命,"BMI" 列记录该国男性 BMI 数据。你将使用 BMI 数据来预测平均寿命。

# Import statements
import pandas as pd
from sklearn.linear_model import LinearRegression

# Load the data
bmi_life_data = pd.read_csv("bmi_and_life_expectancy.csv")

# Make and fit the linear regression model

bmi_life_model = LinearRegression()
bmi_life_model.fit(bmi_life_data[['BMI']], bmi_life_data[['Life expectancy']])

# Mak a prediction using the model
laos_life_exp = bmi_life_model.predict(21.07931)

线性回归注意事项

最适用于线性数据
线性回归会根据训练数据生成直线模型。如果训练数据包含非线性关系,你需要选择:调整数据(进行数据转换)、增加特征数量(参考下节内容)或改用其他模型。

image.png

容易受到异常值影响
线性回归的目标是求取对训练数据而言的 “最优拟合” 直线。如果数据集中存在不符合总体规律的异常值,最终结果将会存在不小偏差。

image.png

多元线性回归

使用到波士顿房价数据集。数据集中包含 506 栋房屋的 13 个特征与房价中值(单位为 1000 美元)。你将根据这 13 个特征拟合模型,并预测房价。

from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston

# Load the data from the the boston house-prices dataset 
boston_data = load_boston()
x = boston_data['data']
y = boston_data['target']

# Make and fit the linear regression model
model = LinearRegression()
model.fit(x, y)

# Make a prediction using the model
sample_house = [[2.29690000e-01, 0.00000000e+00, 1.05900000e+01, 0.00000000e+00, 4.89000000e-01,
                6.32600000e+00, 5.25000000e+01, 4.35490000e+00, 4.00000000e+00, 2.77000000e+02,
                1.86000000e+01, 3.94870000e+02, 1.09700000e+01]]

prediction = model.predict(sample_house)

相关文章

网友评论

      本文标题:深度学习笔记 - 102 - 回归模型

      本文链接:https://www.haomeiwen.com/subject/oqmbcxtx.html