使用线性回归模型在比萨训练样本上进行拟合

来源:互联网 发布:苹果系统装windows 编辑:程序博客网 时间:2024/04/28 21:45

训练数据和测试数据

代码:

x_train = [[6], [8], [10], [14], [18]]y_train = [[7], [9], [13], [17.5], [18]]from sklearn.linear_model import LinearRegressionregressor = LinearRegression()regressor.fit(x_train, y_train)import numpy as npxx = np.linspace(0, 26, 100)#生成了1行100列的一个矩阵#print(xx)xx = xx.reshape(xx.shape[0], 1)#将1行100列的矩阵转化成100行1列的矩阵形式#print(xx)yy = regressor.predict(xx)import matplotlib.pyplot as pltplt.scatter(x_train, y_train)plt1, = plt.plot(xx, yy, label="Degree=1")plt.axis([0, 25, 0, 25])plt.xlabel('Diameter of Pizza')plt.ylabel('Price of Pizza')plt.legend(handles=[plt1])plt.show()print('The R-squared value of Linear Regressor performing on the training data is', regressor.score(x_train, y_train))

运行结果如下:

The R-squared value of Linear Regressor performing on the training data is 0.910001596424
效果图如下:



阅读全文
0 0
原创粉丝点击