简单线性回归截距假设检验_线性回归模型及简单预测

•气温会随着海拔高度的升高而降低, 我们可以通过测量不同海拔高度的气温来预测海拔高度和气温的关系.

•我们假设海拔高度和气温的关系可以使用如下公式表达:

•y(气温) = a * x(海拔高度) + b

•理论上来讲, 确定以上公式 a 和 b的值只需在两个不同高度测试, 就可以算出来 a 和 b 的值了. 但是由于所有的设备都是有误差的, 而使用更多的高度测试的值可以使得预测的值更加准确.

•我们提供了在9个不同高度测量的气温值, 请你根据今天学习的线性回归方法预测 a 和 b 的值. 根据这个公式, 我们预测一下在8000米的海拔, 气温会是多少?

简单点的线性回归模型,不唠叨直接上代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

data = pd.read_csv('height.vs.temperature.csv')

# 读取数据,单行显示
x = data['height'].values.reshape(-1, 1)
y_ = data['temperature'].values.reshape(-1, 1)

# 线性拟合
reg = LinearRegression()
reg.fit(x, y_)
print('a = {:.5}'.format(reg.coef_[0][0]))  # coef_回归系数
print('b = {:.5}'.format(reg.intercept_[0]))  # intercept_截距
print("线性模型为: y_ = {:.5}x + {:.5} ".format(reg.coef_[0][0], reg.intercept_[0]))

# 可视化
predictions = reg.predict(x)
plt.figure(figsize=(8, 8))
plt.scatter(data['height'], data['temperature'], c='black')
plt.plot(data['height'], predictions, c='blue', linewidth=2)  # 蓝线
plt.xlabel("height")
plt.ylabel("temperature")
plt.show()

样本数据:

https://github.com/GreedyAIAcademy/Machine-Learning/blob/master/1introduction/exercise/height.vs.temperature.csv​github.com

预测:

#预测
predictions = reg.predict([[8000]])
print(predictions)
print('8000米高度的温度{:.5}'.format( predictions[0][0]) )

结果:

8df435be22aa2c31056750d98eb3ff5a.png
数据线性拟合直线

8000m预测结果:

49078a1fc864a8f130ee69ff95a72aae.png

###################出现的问题###################

f51c5b7deda3dad227011ab5aab67e38.png

问题原因:Python包中含有h5py

解决方法:升级h5py到2.9.0版本。#如果装了TensorFlow等,部分可能解决不了,升级或卸载后还是此时没办法的话,新建一个Python虚拟环境。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值