学习机器学习有一段时间了,为了让自己更好掌握机器学习算法,特做笔记。
回归是分析变量之间的相关关系。当然在计量经济里面,也是通过回归工具找因果关系,只是要排除各种伪因果。在机器学习里面,一般不会考虑因果。
回归可以解决:
1、找到经验公式
2、通过统计推断,判断公式的有效性
3、确定众多的影响因素的主次。
线性回归
一元回归:
多元回归:
非线性回归
渐近回归
二次型
双曲线型
看看下面简单数据:
编号 | 面积 | 价格 |
1 | 150 | 6450 |
2 | 200 | 7450 |
3 | 250 | 8450 |
4 | 300 | 9450 |
5 | 350 | 11450 |
6 | 400 | 15450 |
7 | 600 | 18450 |
假设房屋价格会受房屋面积影响。方程:
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