機器學習-建立線性迴歸器
阿新 • • 發佈:2019-02-15
#開啟檔案,獲取資料 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)