2017-05-23 13 views
0

私は機械学習とPythonには新しいです。今私はターゲットのバイナリ結果を予測するためにランダムなフォレストを適用しようとしています。私のデータには、24の予測変数(1000件の観測値)があり、そのうちの1つはカテゴリ(性別)であり、他のすべては数値です。数値の中には、ユーロ(非常に歪められている)と金額(atmからのトランザクション数)の2つのタイプの値があります。私は大きなスケールの特徴を変えて、帰属をしました。最後に、相関と共線性を確認し、それに基づいていくつかの機能を削除しました(結果として私は24の機能を持っていました)。今はRFを実装すると、トレーニングセットで完璧です。また、テストセットに適用しても、非常に低いリコール値が得られます。これをどのように修正すればよいですか?ランダムフォレストを正しく適用する方法は?

def classification_model(model, data, predictors, outcome): 
    # Fit the model: 
    model.fit(data[predictors], data[outcome]) 

    # Make predictions on training set: 
    predictions = model.predict(data[predictors]) 

    # Print accuracy 
    accuracy = metrics.accuracy_score(predictions, data[outcome]) 
    print("Accuracy : %s" % "{0:.3%}".format(accuracy)) 

    # Perform k-fold cross-validation with 5 folds 
    kf = KFold(data.shape[0], n_folds=5) 
    error = [] 
    for train, test in kf: 
     # Filter training data 
     train_predictors = (data[predictors].iloc[train, :]) 

     # The target we're using to train the algorithm. 
     train_target = data[outcome].iloc[train] 

     # Training the algorithm using the predictors and target. 
     model.fit(train_predictors, train_target) 

     # Record error from each cross-validation run 
     error.append(model.score(data[predictors].iloc[test, :], data[outcome].iloc[test])) 

    print("Cross-Validation Score : %s" % "{0:.3%}".format(np.mean(error))) 

    # Fit the model again so that it can be refered outside the function: 
    model.fit(data[predictors], data[outcome]) 



outcome_var = 'Sold' 
model = RandomForestClassifier(n_estimators=20) 
predictor_var = train.drop('Sold', axis=1).columns.values 
classification_model(model,train,predictor_var,outcome_var) 

#Create a series with feature importances: 
featimp = pd.Series(model.feature_importances_, index=predictor_var).sort_values(ascending=False) 
print(featimp) 

outcome_var = 'Sold' 
model = RandomForestClassifier(n_estimators=20, max_depth=20, oob_score = True) 
predictor_var = ['fet1','fet2','fet3','fet4'] 
classification_model(model,train,predictor_var,outcome_var) 

答えて

0

ランダムフォレストではオーバーフィットするのが非常に簡単です。これを解決するには、使用する最適なパラメータを知るために、より厳密にパラメータ検索を行う必要があります。 [ここ](http://scikit-learn.org/stable/auto_examples/model_selection/randomized_search.html )はこれを行う方法に関するリンクです:(scikitの文書から)。

これはオーバーフィットであり、モデルで動作する最適なパラメータを検索する必要があります。このリンクは、ハイパーパラメータ推定のためのグリッドとランダム化検索の実装を提供します。 そして、このMIT Artificial Intelligence講義を通して深い理論オリエンテーションを得ることも楽しいでしょう:https://www.youtube.com/watch?v=UHBmv7qCey4&t=318s

希望すると便利です。

+0

投稿とリンクをありがとう。確かに、私はGridSearchCVを試して、最良のパラメータが使用されたときに比較的良い結果を観察しました。私の主な問題は特にリコール値でした。それも良くなった。しかし結局はまだそれは過ちです。私は検索を続けます。 –

+1

ランダム検索を試しましたか? – user29120

+0

あなたが言及するまで私は知らなかった!私は見てみましょう。 –

関連する問題