• Skip to main content
  • Skip to primary sidebar

学習記録

ランダムフォレスト

2018年1月16日 by 河副 太智 Leave a Comment

ランダムフォレスト

1
2
3
4
5
6
7
8
9
10
11
12
13
train_X = df.drop('Survived', axis=1) train_y = df.Survived (
train_X, test_X ,train_y, test_y
) = train_test_split(train_X, train_y, test_size = 0.3, random_state = 666)
 
 
#ランダムフォレスト
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=0)
clf = clf.fit(train_X, train_y)
pred = clf.predict(test_X)
fpr, tpr, thresholds = roc_curve(test_y, pred, pos_label=1)
auc(fpr, tpr)
accuracy_score(pred, test_y)

  • criterion : 分割基準。gini or entropyを選択。(デフォルトでジニ係数)
  • max_depth : 木の深さ。木が深くなるほど過学習し易いので、適当なしきい値を設定してあげる。
  • max_features:最適な分割をする際の特徴量の数
  • min_samples_split:分岐する際のサンプル数
  • random_state:ランダムseedの設定。seedを設定しないと、毎回モデル結果が変わるので注意。

公式ドキュメント
sklearn.tree.DecisionTreeClassifier — scikit-learn 0.19.1 documentation

 

 

 

どの要素が強く影響を与えているかを確認

 

1
2
3
4
5
6
7
8
9
10
11
12
13
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
 
 
features = train_X.columns
importances = clf.feature_importances_
indices = np.argsort(importances)
 
plt.figure(figsize=(6,6))
plt.barh(range(len(indices)), importances[indices], color='b', align='center')
plt.yticks(range(len(indices)), features[indices])
plt.show()

 

 

このエントリーをはてなブックマークに追加

Tweet
[`yahoo` not found]
このエントリーを Google ブックマーク に追加
LinkedIn にシェア
LINEで送る


Filed Under: 分析手法

Reader Interactions

コメントを残す コメントをキャンセル

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in