线性回归是简单的机器学习,提供了一个说明学习算法如何工作的例子 import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt x = [1,3,2,1,3] x = np.reshape(x,newshape=(5,1)) y = [14,24,18,17,27] y = np.reshape(y,newshape=(5,1)) # 调用模型 lr = LinearRegression() # 训练模型 lr.fit(x,y) # 计算R平方 print(lr.score(x,y)) # 计算y_hat y_hat = lr.predict(x) # 打印出图 plt.scatter(x,y) plt.plot(x, y_hat) plt.show()