Least Squares & Linear Regression

refer to

Programming implementation https://b23.tv/aUuSF7

Derivation process https://blog.csdn.net/weixin_38278993/article/details/100556051

 

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()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325447606&siteId=291194637