邹博机器学习代码分析(1)-线性回归
生活随笔
收集整理的這篇文章主要介紹了
邹博机器学习代码分析(1)-线性回归
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.導入各種需要的包
import csv import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from pprint import pprint 2.讀入數據 path = 'Advertising.csv' data = pd.read_csv(path) # TV、Radio、Newspaper、Sales # x = data[['TV', 'Radio', 'Newspaper']] x = data[['TV', 'Radio']] y = data['Sales'] print x print y3.訓練模型
# 訓練和測試 分開 train占0.8 默認為75 和 25 x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, random_state=1) print type(x_test ) #DataFrame 類型 print x_train.shape, y_train.shape linreg = LinearRegression() # 調用線性回歸模型 model = linreg.fit(x_train, y_train) # 進行擬合 print model # 打印模型信息 # coef_ 對應Theta_1 ........;intercept_ 對應Theta_0 print linreg.coef_, linreg.intercept_ 輸出結果為:[ 0.04686997 ?0.1800065 ] 2.94751503603;表示Theta_0 = 2.94751503603 4.進行預測
# print y_test order = y_test.argsort(axis=0) #order 為 y_test的值按大小排序的索引號 原來的index不變 # print 'order:',order y_test = y_test.values[order] # 取出order中的索引號 作為y_test的排序方式 # print x_test x_test = x_test.values[order, :] # x_test 的第一列按 order排序 # print x_test y_hat = linreg.predict(x_test) # 對測試集進行預測
argsort 是按y_tset 的值從小到大排序, 返回每個值對應的index ,y_test自帶的index不變.
5.計算MES和R2mse = np.average((y_hat - np.array(y_test)) ** 2) # Mean Squared Error rmse = np.sqrt(mse) # Root Mean Squared Error print 'MSE = ', mse, print 'RMSE = ', rmse print 'R2 = ', linreg.score(x_train, y_train) print 'R2 = ', linreg.score(x_test, y_test) R^2 = 1-RSS/TSS 越大效果越好。1為最優值。用score計算R^2
6.畫圖
plt.figure(facecolor='w') t = np.arange(len(x_test)) plt.plot(t, y_test, 'r-', linewidth=2, label=u'真實數據') plt.plot(t, y_hat, 'g-', linewidth=2, label=u'預測數據') plt.legend(loc='upper right') plt.title(u'線性回歸預測銷量', fontsize=18) plt.grid(b=True) plt.show()
總結
以上是生活随笔為你收集整理的邹博机器学习代码分析(1)-线性回归的全部內容,希望文章能夠幫你解決所遇到的問題。