質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

0回答

570閲覧

【初心者】ランダムフォレストを使ったら訓練データの結果とテストデータの結果が全く別ものになった

yuyu4100

総合スコア43

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

0グッド

0クリップ

投稿2023/01/25 01:28

編集2023/01/25 01:29

前提

サンプルデータを使って’churm’を予測するモデルを作るためにランダムフォレストを使いました。
test_score train_score
RandomForest 0.019183 0.857964
そうすると上記のような訓練データとテストデータが全く違う結果がでました。
欠損値は平均値で埋めました。

実現したいこと

この2つの結果を同じくらいに寄せるにはどうすればよいのでしょうか??

該当のソースコード

python
X = df2.drop('churn',axis=1)
y = df2['churn'] # 目的変数

トレーニングデータ,テストデータの分割

X_train, X_valid, y_train, y_valid = train_test_split(X, y,test_size=0.2, random_state=0)

from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor

ランダムフォレストの設定

models = {
'RandomForest': RandomForestRegressor(random_state=0),
}

モデル構築

scores = {}
for model_name, model in models.items():
model.fit(X_train, y_train)
scores[(model_name, 'train_score')] = model.score(X_train, y_train)
scores[(model_name, 'test_score')] = model.score(X_valid, y_valid)

結果を表示

pd.Series(scores).unstack()

補足情報(FW/ツールのバージョンなど)

データの情報です
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100000 entries, 0 to 99999
Data columns (total 50 columns):

Column Non-Null Count Dtype


0 rev_Mean 99643 non-null float64
1 mou_Mean 99643 non-null float64
2 totmrc_Mean 99643 non-null float64
3 da_Mean 99643 non-null float64
4 ovrmou_Mean 99643 non-null float64
5 ovrrev_Mean 99643 non-null float64
6 vceovr_Mean 99643 non-null float64
7 datovr_Mean 99643 non-null float64
8 roam_Mean 99643 non-null float64
9 change_mou 99109 non-null float64
10 change_rev 99109 non-null float64
11 drop_vce_Mean 100000 non-null float64
12 drop_dat_Mean 100000 non-null float64
13 blck_vce_Mean 100000 non-null float64
14 blck_dat_Mean 100000 non-null float64
15 unan_vce_Mean 100000 non-null float64
16 unan_dat_Mean 100000 non-null float64
17 plcd_vce_Mean 100000 non-null float64
18 plcd_dat_Mean 100000 non-null float64
19 recv_vce_Mean 100000 non-null float64
20 recv_sms_Mean 100000 non-null float64
21 comp_vce_Mean 100000 non-null float64
22 comp_dat_Mean 100000 non-null float64
23 custcare_Mean 100000 non-null float64
24 ccrndmou_Mean 100000 non-null float64
25 cc_mou_Mean 100000 non-null float64
26 inonemin_Mean 100000 non-null float64
27 threeway_Mean 100000 non-null float64
28 mou_cvce_Mean 100000 non-null float64
29 mou_cdat_Mean 100000 non-null float64
30 mou_rvce_Mean 100000 non-null float64
31 owylis_vce_Mean 100000 non-null float64
32 mouowylisv_Mean 100000 non-null float64
33 iwylis_vce_Mean 100000 non-null float64
34 mouiwylisv_Mean 100000 non-null float64
35 peak_vce_Mean 100000 non-null float64
36 peak_dat_Mean 100000 non-null float64
37 mou_peav_Mean 100000 non-null float64
38 mou_pead_Mean 100000 non-null float64
39 opk_vce_Mean 100000 non-null float64
40 opk_dat_Mean 100000 non-null float64
41 mou_opkv_Mean 100000 non-null float64
42 mou_opkd_Mean 100000 non-null float64
43 drop_blk_Mean 100000 non-null float64
44 attempt_Mean 100000 non-null float64
45 complete_Mean 100000 non-null float64
46 callfwdv_Mean 100000 non-null float64
47 callwait_Mean 100000 non-null float64
48 churn 100000 non-null int64
49 months 100000 non-null int64
dtypes: float64(48), int64(2)
memory usage: 38.1 MB
None

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

PondVillege

2023/01/25 02:42 編集

CrossValidationを利用したハイパーパラメータチューニングが良さそうですね https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html ライブラリを参照してわかる通り,モデルの性能を決めるパラメータは多岐に渡ります. 例えば,生成する弱学習機の個数を決めるn_estimatorsはデフォルトで100になっていたりと,解きたい問題に対して高性能すぎる可能性が否定できません.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問