本篇博客分享使用python进行一元线性回归
源数据:
代码实现:
import pandas as pd
from sklearn.linear_model import LinearRegression #线性回归
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_excel(r"C:\Users\Haley\Desktop\linear regression test.xlsx")
X = data[['X']]
Y = data['Y']
regr = LinearRegression()
regr.fit(X,Y)
print('系数矩阵:\n',regr.coef_)
print('线性回归模型:\n',regr)
# 使用模型预测
predicted = regr.predict(X)
plt.scatter(X,Y,marker='x')
plt.plot(X,predicted, color='b')
plt.show()