web-dev-qa-db-ja.com

python sklearnを使用したランダムフォレストモデルのインクリメンタルトレーニング

以下のコードを使用して、ランダムフォレストモデルを保存しています。トレーニング済みモデルを保存するためにcPickleを使用しています。新しいデータが表示されたら、モデルを段階的にトレーニングできますか?現在、列車セットには約2年のデータがあります。さらに2年間トレーニングして、(一種の)既存の保存済みモデルに追加する方法はありますか。

rf =  RandomForestRegressor(n_estimators=100)
print ("Trying to fit the Random Forest model --> ")
if os.path.exists('rf.pkl'):
    print ("Trained model already pickled -- >")
    with open('rf.pkl', 'rb') as f:
        rf = cPickle.load(f)
else:
    df_x_train = x_train[col_feature]
    rf.fit(df_x_train,y_train)
    print ("Training for the model done ")
    with open('rf.pkl', 'wb') as f:
        cPickle.dump(rf, f)
df_x_test = x_test[col_feature]
pred = rf.predict(df_x_test)

編集1:4年間のデータでモデルを一度にトレーニングするための計算能力がありません。

7
ForeverLearner

モデルで「warm_start」パラメーターをTrueに設定できます。これにより、fitcallを使用した以前の学習での学習の保持が保証されます。

'warm_start'を設定した後、同じモデルが2回段階的に学習します(train_X [:1]、train_X [1:2])。

forest_model = RandomForestRegressor(warm_start=True)
forest_model.fit(train_X[:1],train_y[:1])
pred_y = forest_model.predict(val_X[:1])
mae = mean_absolute_error(pred_y,val_y[:1])
print("mae      :",mae)
print('pred_y :',pred_y)
forest_model.fit(train_X[1:2],train_y[1:2])
pred_y = forest_model.predict(val_X[1:2])
mae = mean_absolute_error(pred_y,val_y[1:2])
print("mae      :",mae)
print('pred_y :',pred_y)

mae:1290000.0 pred_y:[1630000。] mae:925000.0 pred_y:[1630000。]

最後に学習した値のみを使用してモデル化します(train_X [1:2])

forest_model = RandomForestRegressor()
forest_model.fit(train_X[1:2],train_y[1:2])
pred_y = forest_model.predict(val_X[1:2])
mae = mean_absolute_error(pred_y,val_y[1:2])
print("mae      :",mae)
print('pred_y :',pred_y)

mae:515000.0 pred_y:[1220000。]

http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html

10
M. Gopal