0
点赞
收藏
分享

微信扫一扫

最小二乘法&线性回归

 

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

data=pd.read_excel("data.xlsx")
features=data["X"].values.reshape(-1,1)
target=data["Y"].values.reshape(-1,1)

regression=LinearRegression()
model=regression.fit(features,target)
print(model.intercept_,model.coef_)

values=np.zeros(5)
for i in range(5):
values[i]=rd.randint(0,100)
result=model.predict(values.reshape(-1,1))
print(values,result)

plt.xlabel("X")
plt.xlabel("Y")
plt.scatter(values,result)
plt.show()

 


举报

相关推荐

0 条评论