機器學習—線性回歸
阿新 • • 發佈:2017-10-05
com str mode imp repr 線性模型 images mage 訓練集
一、普通的線性模型
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn import metrics %matplotlib inline
data = pd.read_csv(‘Advertising.csv‘,index_col=0)#第一列為index data.head()
#切分訓練集和測試集 x = data.values[:,:3] y = data.values[:,3] x_train,x_test,y_train,y_test = train_test_split(x,y,train_size=0.7,random_state=0) #標準化處理 sc = StandardScaler() x_train_std = sc.fit_transform(x_train) x_test_std = sc.transform(x_test) #訓練模型 linreg = LinearRegression() linreg.fit(x_train_std,y_train) y_pred= linreg.predict(x_test_std) #檢驗模型結果 mse = np.average((y_pred-y_test)**2) metrics.mean_squared_error(y_pred,y_test) #這個也是均方誤差 r2 = metrics.r2_score(y_test,y_pred) #R2值,註意參數,前面的是實際值,後面的是預測值 mse,r2 #計算R2 def calculater2(y_pred,y_test): RSS = ((y_pred-y_test)**2).sum() TSS = (((y_test-np.average(y_test))**2)).sum()return 1-(RSS/TSS) calculater2(y_pred,y_test) #畫圖 fig = plt.figure(figsize=(10,6)) plt.plot(y_test) plt.plot(y_pred)
二、加入正則化的模型
Ridge回歸
from sklearn.linear_model import RidgeCV,LassoCV #用這個自帶交叉驗證參數 from sklearn.model_selection import GridSearchCV #如果使用RidgeCV就不用GridSearchCV這個API了 #使用RidgeCV來建立參數 alpha = np.logspace(-3,2,10) #生成超參數,10的-3次方到10的2次方的等差數列 ridge = RidgeCV(alpha,cv=5) ridge.fit(x_train_std,y_train) ridge.alpha_ #輸出超參數的值 #使用Ridge配合GridSearchCV來做 from sklearn.linear_model import Ridge,Lasso ridge_model = GridSearchCV(Ridge(),param_grid={‘alpha‘:alpha},cv=5) ridge_model.fit(x_train_std,y_train) ridge_model.best_params_ #驗證模型效果 y_pred_ridge = ridge.predict(x_test_std) mse_ridge = metrics.mean_squared_error(y_test,y_pred_ridge) r2_ridge = metrics.r2_score(y_test,y_pred_ridge) mse_ridge,r2_ridge
Lasso回歸
#建立模型 lasso = LassoCV(alphas=alpha,cv=5) lasso.fit(x_train_std,y_train) lasso.alpha_ #驗證模型效果 y_pred_lasso = lasso.predict(x_test_std) mse_lasso = metrics.mean_squared_error(y_test,y_pred_lasso) r2_lasso = metrics.r2_score(y_test,y_pred_lasso) mse_lasso,r2_lasso
機器學習—線性回歸