LoginSignup
8
7

More than 5 years have passed since last update.

機械学習3ヶ月の初心者がKaggleHousingPriceに挑戦した(Top29%) 後半(アルゴリズム)

Posted at

前回の記事の続きです。
挑戦したKaggleチュートリアル:KaggleHousingPrice

この記事はアルゴリズムについて書いています。
今回使用したアルゴリズムはXGboostです。
Xgboostの他にLightGBMもありますが、パラメータの調整がうまくできず、いいスコアが出せませんでした。
(どなたかLightGBMのハイパーパラメータの調整教えてください><)

XGboostのハイパーパラメータの調整についてはこの記事が参考になりました。
Xgboostはパラメータがたくさんあるので、重要なパラメータを調整するだけでも時間がかかります。そこでRandomizedSearchCVというグリッドサーチを採用しました。グリッドサーチは総当たりで、一番良いパラメータの組み合わせを算出しますが、ランダムサーチは文字通りランダムに各パラメータが選択され、CV*iterの回数だけ実行されます。

HousingPrice.py
from scipy.stats import randint as sp_randint
from scipy.stats import uniform as sp_uniform
learning=[0.05,0.07,0.09,0.1,0.12,0.14,0.16,0.18,0.2]
r=[]
for i in range(6,10):
    r.append(i/10.0)
mod = xgb.XGBRegressor(reg_lambda=1,gamma=0.0)
params ={'learning_rate':sp_uniform(0.05,0.2),'n_estimators':list(range(10,150,10)),\
         'max_depth':list(range(3,11,1)),'min_child_weight':list(range(1,6,2))\
        ,'colsample_bytree':r,'subsample':r}
#繰り返し処理計算式=cv * n_iter
import sklearn
gscv =RandomizedSearchCV(mod, params, verbose=1,
                   cv=3,n_iter=500, scoring='neg_mean_squared_error',n_jobs=1)
gscv.fit(X_train1, y_train1)
gscv.best_params_

実行結果
learning_rate=0.15124835871864328,max_depth=3,min_child_weight=3,n_estimators=120,colsample_bytree=0.8,subsample=0.6

このまま提出してもよいのですが、スコアを少しでも上げるために、バギングと呼ばれるアンサンブルを使います。
バギングとはトレーニングセット数がN個の場合、ランダムにN個復元抽出(重複あり)して、機械学習のモデルに食わることです。
これをだいたい10~100回くらい繰り返して、その平均値を算出します。
バギングの利点は手元のデータセットとは若干異なるデータセットが手に入るので、データセットのノイズが少し軽減されます。
自分は100回繰り返しました。

HousingPrice.py
mod=xgb.XGBRegressor(reg_lambda=1,gamma=0.0,learning_rate= 0.15124835871864328,max_depth=3,min_child_weight=3,n_estimators=120,colsample_bytree=0.8,subsample=0.6)
y_pred_on_test=np.zeros(1459)
import random
for i in range(0,100):
    sampling=np.ones((1457,310))
    sampling_y=np.zeros(1457)
    for j in range(0,1457):
        element=np.random.randint(0,1457)
        sampling[[j]]=X[[element]]
        sampling_y[j]=y[element]
    mod.fit(sampling,sampling_y)
    y_prediction= mod.predict(test_df)
    y_pred_on_test+=y_prediction

y_pred_on_test=y_pred_on_test/100
output_df = pd.DataFrame(y_pred_on_test,columns=['SalePrice'])
output_df['Id']=ID
output_df=output_df.ix[:,['Id','SalePrice']]
output_df.to_csv('housing_ans.csv', index=False, encoding='utf-8')

この状態で提出し、スコアは0.12343でした。(2017/12/17の時点でTOP29%)
提出.JPG


複数の違う分類器を使ってアンサンブルをすればもっとスコアを挙げれそうなのですが、LightGBMのパラメーターの調整がうまくいかないので現時点ではこれ以上スコアをあげれなさそうです。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7