基本算法

公众号文章:最小二乘法

代码实现

from sklearn import datasets # datasets
from sklearn import linear_model
import numpy as np
import matplotlib.pyplot as plt

diabetes = datasets.load_diabetes()
diabetes_x = diabetes.data[:,np.newaxis,2]

x_train = diabetes_x[:40]
x_test = diabetes_x[-40:]
y_train = diabetes.target[:40]
y_test = diabetes.target[-40:]


reg = linear_model.LinearRegression()
reg.fit(x_train,y_train)
y_predict = reg.predict(x_test)
plt.scatter(x_test, y_test, color="black")
plt.plot(x_test, y_predict, color="blue", linewidth=3)
plt.show()

评论已关闭。

X