标签:转换 mooc int shape near import mac info cat
一:样本
代码:
import matplotlib.pyplot as plt import numpy as np from sklearn import linear_model datasets_X = [] #尺寸 datasets_Y = [] #价格 fr = open(‘D:\python_source\Machine_study\mooc_data\回归/prices.txt‘, ‘r‘) lines = fr.readlines() for line in lines: items = line.strip().split(‘,‘) #转换为列表,以 , 分隔 datasets_X.append(int(items[0])) datasets_Y.append(int(items[1])) length = len(datasets_X) datasets_X = np.array(datasets_X).reshape([length, 1])#二维列表 #print(datasets_X) datasets_Y = np.array(datasets_Y) minX = min(datasets_X) maxX = max(datasets_X) X = np.arange(minX, maxX).reshape([-1, 1]) #以最大值和最小值为范围建立等差数列 2维列表 792-4399 #print(X) linear = linear_model.LinearRegression() linear.fit(datasets_X, datasets_Y) #拟合回归数据 print(‘Coefficients:‘, linear.coef_)#查看系数 print(‘intercept:‘, linear.intercept_)#查看截距 #可视化 plt.scatter(datasets_X, datasets_Y, color = ‘red‘) plt.plot(X, linear.predict(X), color = ‘blue‘) plt.xlabel(‘Area‘) plt.ylabel(‘Price‘) plt.show()
效果图片:
标签:转换 mooc int shape near import mac info cat
原文地址:https://www.cnblogs.com/ymzm204/p/11300137.html