机器学习算法(一)线性回归

本文介绍了机器学习中的回归概念,包括线性回归、多元回归和非线性回归方法。通过实例演示如何使用Python的sklearn库进行房屋面积和价格的数据分析,展示了回归模型的建立和预测过程。
摘要由CSDN通过智能技术生成

学习机器学习有一段时间了,为了让自己更好掌握机器学习算法,特做笔记。

回归是分析变量之间的相关关系。当然在计量经济里面,也是通过回归工具找因果关系,只是要排除各种伪因果。在机器学习里面,一般不会考虑因果。

回归可以解决:

1、找到经验公式

2、通过统计推断,判断公式的有效性

3、确定众多的影响因素的主次。

线性回归

一元回归:Y=a+b X

多元回归:Y=\beta _{0}+\beta _{1}X _{1}+\beta _{2}X _{2}+...+\beta _{n}X _{n}

非线性回归

Y=a+b e^{-rX}    渐近回归

Y=a+b_{1}X_{1}+b_{2}X^{2}   二次型

Y=z+\frac{b}{X}     双曲线型

看看下面简单数据:

房屋面积与房屋价格数据
编号 面积价格
11506450
22007450
32508450
43009450
535011450
640015450
760018450

假设房屋价格会受房屋面积影响。方程:Y=a+bX


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

x = np.array([150, 200, 250, 300, 350, 400, 600]).reshape(-1, 1)
y = np.array([6450,7450,8450,9450,11450,15450,18450]).reshape(-1, 1)


# 创建线性回归模型
model = LR()

# 拟合数据
model.fit(x, y)

# 预测新的数据点
x_new = np.array(700).reshape(1, -1)
y_pred = model.predict(x_new)


print('a:',model.intercept_)
print('b',model.coef_)
print(y_pred)

结果 :

a: [1771.80851064]
b [[28.77659574]]
[[21915.42553191]]

再画图

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

x = np.array([150, 200, 250, 300, 350, 400, 600]).reshape(-1, 1)
y = np.array([6450,7450,8450,9450,11450,15450,18450]).reshape(-1, 1)


# 创建线性回归模型
model = LR()

# 拟合数据
model.fit(x, y)

# 预测新的数据点
x_new = np.array(700).reshape(1, -1)
y_pred = model.predict(x_new)


plt.scatter(x,y)
plt.plot(x,model.predict(x),color='red')
plt.show()

待续

2024.1.2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bowen2006

你的鼓励是我的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值