机器学习-创建线性回归器

 
 
#打开文件,获取数据
import sys
import numpy as np
from sklearn.linear_model import LinearRegression
import sklearn.metrics as sm
import matplotlib.pyplot as plt
X = []
y = []
f = open('E:\MachineLearning\CodeBook\data_singlevar.txt', 'r')#打开文件
lines = f.readlines()     #一次性按行把所有数据读取出来
for line in lines:       #逐行读取文件
    #print(line)          #打印一行数据
    xt, yt = [float(i) for i in line.split(',')]#逗号分隔字段,并将字段转化为浮点数
    X.append(xt)
    y.append(yt)
#划分数据为训练集与验证集
num_training = int(0.8*len(X))
num_test = len(X) - num_training
#训练数据,80%的数据是训练数据
X_train = np.array(X[:num_training]).reshape(num_training, 1)
y_train = np.array(y[:num_training])
#测试数据,20%的数据是测试数据
X_test = np.array(X[num_training:]).reshape(num_test, 1)
y_test = np.array(y[num_training:])
#训练模型
linear_regressor = LinearRegression()
linear_regressor.fit(X_train, y_train)
y_predict = linear_regressor.predict(X_train)
y_test_predict = linear_regressor.predict(X_test)
print('The score of LinearRegressor is:',linear_regressor.score(X_test, y_test))
#绘图
plt.figure()
plt.scatter(X_train, y_train, color = 'green')
plt.plot(X_train, y_predict, color = 'black', linewidth = 4)
plt.title('Traning Data')
plt.show()

plt.scatter(X_test, y_test, color = 'green')
plt.plot(X_test, y_test_predict, color = 'black', linewidth = 4)
plt.title('Test Data')
plt.show()

#计算回归准确性
print('Mean absolute error = ', round(sm.mean_absolute_error(y_test, y_test_predict)), 2)
print('Mean squared error = ', round(sm.mean_squared_error(y_test, y_test_predict)), 2)
print('Median absolute error = ', round(sm.median_absolute_error(y_test, y_test_predict)), 2)


猜你喜欢

转载自blog.csdn.net/u012967763/article/details/79204261