Python-回帰(線形回帰、RFE、LASSOおよび嶺回帰+K折り返し交差検証)

8953 ワード

1.通常の線形回帰:出力モデルの真値と予測値の平均二乗差をできるだけ小さくする(すなわち最小二乗推定法)が、過度なフィッティング(すなわち低偏差)に陥りやすく、後続の回帰方法では正規化法を持ってデータを削減する.
2.通常の線形回帰+RFE:RFEはrecursive feature elimination回帰特徴除去であり、回帰特徴除去中にno_のみを保持させるFeaturesの最も重要な特徴は、過度なフィッティングを避けることができるが、RFEはいくつかの変数を捨てることができ、もともと次のいくつかの方法で変数に重みを与えることができなかった.
3.L 2縮小回帰-嶺回帰:正規化されたブロックはL 2パターンを採用し、alphaが大きいほど縮小幅が大きい.嶺回帰はLASSOの予測能力より優れているが,LASSOは動的選択を完了できる.
4.L 1縮小回帰-LASSO:Least absolute shrinkage and selection operator最小絶対値縮小と選択動作、LASSOはより疎な結果に偏り、1つの結果の大部分の係数が0に圧縮されると、係数と呼ばれ、LASSOの大部分の係数が0になり、関連する変数に対しては、1つだけ保持することを選択する.
RFE:
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 05 19:52:39 2018

@author: Alvin AI
"""

from sklearn.datasets import load_boston
from sklearn.cross_validationi import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from itertools import combinations
from sklearn.feature_selection import RFE

#    
def get_data():
    data = load_boston()
    x = data['data']
    y = data['target']
    return x,y

#    
#       (RFE-recursive feature elimination)   no_features       
def build_model(x,y,no_features):
    model = LinearRegression(normalize=True,fit_intercept=True)
    rfe_model = RFE(estimator=model,n_features_to_select=no_features)
    rfe_model.fit(x,y)
    return rfe_model

#    
def view_model(model):
    print "
model coefficients" print "===================
" #coef_ ,intercept_ for i,coef in enumerate(model.coef_): print "\t coefficient %d %model"%(i+1,coef) print "
\tintercept %0.3f"%(model.intercept_) # def model_worth(true_y,predicted_y): print "\t mean squared error = %0.2f"%(mean_squared_error(true_y,predicted_y)) return mean_squared_error(true_y,predicted_y) # def plot_residual(y,predicted_y): plt.cla() plt.xlabel('predicted y') plt.ylabel('residual') plt.title('residual plot') plt.figure1(1) diff = y - predicted_y plt.plot(predicted_y,diff,'go') plt.show() if __name__=="__main__": x,y = get_data() # x_train,x_test_all,y_train,y_test_all = train_test_split(x,y,\ test_size=0.3,random_state=9) x_dev,x_test,y_dev,y_test = train_test_split(x_test_all,y_test_all,\ test_size=0.3,random_state=9) # poly_features = PolynomialFeatures(interaction_only=True)# x1 x2 ,x1^2 x_train_poly = poly_features.fit_transform(x_train) x_dev_poly = poly_features.fit_transform(x_dev) choosen_model = build_model(x_train_poly,y_train,20) predicted_y = choosen_model.predict(x_train_poly) mse = model_worth(y_train,predicted_y) x_test_poly = poly_features.fit_transform(x_test) predicted_y = choosen_model.predict(x_test_poly) model_worth(y_test,predicted_y)

LASSO:
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 09 09:08:51 2018

@author: Alvin AI
"""

from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso, LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
import matplotlib.pyplot as plt
import numpy as np

#    
def get_data():
    data = load_boston()
    x = data['data']
    y = data['target']
    return x,y

#    
def build_models(x,y):
    alpha_range = np.linspace(0,0.5,200)
    model = Lasso(normalize=True)#      ,      
    coeffiecients = []
    #   alpha     
    for alpha in alpha_range:
        model.set_params(alpha=alpha)
        model.fit(x,y)
        coeffiecients.append(model.coef_)#        
        #print coeffiecients #   200*13
    #            alpha 
    #     RMSE    alpha 
    coeff_path(alpha_range,coeffiecients)
    #     
    #view_model(model)
    
#       
def view_model(model):
    print "
model coeffiecients" print "======================" for i,coef in enumerate(model.coef_): print "\t coefficient %d %0.3f" % (i+1,coef) print "
\t intercept %0.3f" % (model.intercept_) # def model_worth(true_y,predicted_y): print "\t mean squared error = %0.2f
" % \ (mean_squared_error(true_y,predicted_y)) # alpha def coeff_path(alpha_range,coeffiecients): plt.close('all') plt.cla() plt.figure(1) plt.xlabel("Alpha Values") plt.ylabel("coeffiecient weights for different alpha values") plt.plot(alpha_range,coeffiecients) plt.axis('tight')# x、y plt.show() # , def get_coef(x,y,alpha): model = Lasso(normalize=True,alpha=alpha) model.fit(x,y) coefs = model.coef_ indices = [i for i,coef in enumerate(coefs) if abs(coef) > 0.0] return indices # if __name__ == "__main__": x,y = get_data() # alpha , build_models(x,y) print "
predicting using all the variables
" full_model = LinearRegression(normalize=True) full_model.fit(x,y) predicted_y = full_model.predict(x) model_worth(y,predicted_y) print "
models at different alpha values
" alpa_values = [0.22,0.08,0.01] for alpha in alpa_values: indices = get_coef(x,y,alpha) print "\t alpha = %0.2f number of variables selected = %d\ " % (alpha,len(indices))# print "\t attributes include ", indices# x_new = x[:,indices] model = LinearRegression(normalize=True) model.fit(x_new,y) predicted_y = model.predict(x_new) model_worth(y,predicted_y)

嶺回帰+クロス検証反復器:データが少ない場合に訓練セットをK部に分割し、モデルをk-1部データ上で馴染ませ、残りをテストとして使用することでdevセットを単独で分割する必要がなくなり、この方法はK折クロス検証法とも呼ばれる.
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 09 14:30:10 2018

@author: Alvin AI
"""

from sklearn.datasets import load_boston
from sklearn.cross_validation import KFold,train_test_split
from sklearn.linear_model import Ridge
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
import numpy as np

#    
def get_data():
    data = load_boston()
    x = data['data']
    y = data['target']
    return x,y

#    
def build_model(x,y):
    kfold = KFold(y.shape[0],5)#K              ,5    (         )
    model = Ridge(normalize=True)#             
    alpha_range = np.linspace(0.0015,0.0017,30)#  alpha   
    grid_param = {"alpha":alpha_range}
    #GridSearchCV                    
    #cv             
    grid = GridSearchCV(estimator=model,param_grid=grid_param,cv=kfold,\
                        scoring='mean_squared_error')
    grid.fit(x,y)
    display_param_results(grid.grid_scores_)#         
    print grid.best_params_#           
    #               
    return grid.best_estimator_
     
    
#         
def view_model(model):
    #print "
estimated alpha = %0.3f" % model.alpha_# alpha print "
model coeffiecients" print "======================
" for i,coef in enumerate(model.coef_): print "\t coefficent %d %0.3f" % (i+1,coef) print "
\t intercept %0.3f" % (model.intercept_) # def model_worth(true_y,predicted_y): print "\t Mean squared error = %0.2f" % (mean_squared_error(true_y,predicted_y)) return mean_squared_error(true_y,predicted_y) # def display_param_results(param_results): fold = 1 for param_result in param_results: print "fold %d mean squared error %0.2f" % (fold,abs(param_result[1]\ )),param_result[0] fold+=1 if __name__ == "__main__": x,y = get_data() # x_train,x_test,y_train,y_test= train_test_split(x,y,test_size=0.3,\ random_state=9) # poly_features = PolynomialFeatures(interaction_only=True) x_train_poly = poly_features.fit_transform(x_train) x_test_poly = poly_features.fit_transform(x_test) choosen_model = build_model(x_train_poly,y_train) predicted_y = choosen_model.predict(x_train_poly) model_worth(y_train,predicted_y) view_model(choosen_model) predicted_y = choosen_model.predict(x_test_poly) model_worth(y_test,predicted_y)