Pythonでのハイパーパラメータチューニング
Alex Scriven
Data Scientist
このコースを学ぶ理由
内部の仕組みに驚くかもしれません。
このデータセットはクレジットカード延滞に関するものです。
台湾の一部消費者の金融履歴に関する変数を含みます。30,000人、属性24です。
目的変数は、ローンが延滞したかどうかです。
前処理済みで、概念説明のために小規模サンプルを使う場合があります。
追加情報はこちら:
https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients
パラメータとは?
単純なロジスティック回帰モデル:
log_reg_clf = LogisticRegression() log_reg_clf.fit(X_train, y_train)print(log_reg_clf.coef_)
array([[-2.88651273e-06, -8.23168511e-03, 7.50857018e-04,
3.94375060e-04, 3.79423562e-04, 4.34612046e-04,
4.37561467e-04, 4.12107102e-04, -6.41089138e-06,
-4.39364494e-06, cont... ]])
係数を整形:
# 元の変数名を取得 original_variables = list(X_train.columns)# 名前と係数を結合 zipped_together = list(zip(original_variables, log_reg_clf.coef_[0])) coefs = [list(x) for x in zipped_together]# 列名付きのDataFrameにする coefs = pd.DataFrame(coefs, columns=["Variable", "Coefficient"])
上位3つの係数を並べ替えて表示します
coefs.sort_values(by=["Coefficient"], axis=0, inplace=True, ascending=False)
print(coefs.head(3))

パラメータを見つけるには:
パラメータは「Parameters」ではなく「Attributes」欄にあります!
木ベースのアルゴリズムは?
ランダムフォレストには係数はなく、ノードの分岐(どの特徴量をどの値で分割するか)があります。
# 簡単なランダムフォレスト推定器 rf_clf = RandomForestClassifier(max_depth=2) rf_clf.fit(X_train, y_train)# フォレストから1本の木を取り出す chosen_tree = rf_clf.estimators_[7]
簡潔にするため、決定木の最終生成物(画像)のみを示します。使用パッケージ(graphviz と pydotplus)は各自でお試しください。

左側の上から2番目のノードの詳細を取得できます:
# 分割に使った列を取得 split_column = chosen_tree.tree_.feature[1] split_column_name = X_train.columns[split_column]# 分割値を取得 split_value = chosen_tree.tree_.threshold[1]print("This node split on feature {}, at a value of {}" .format(split_column_name, split_value))
「このノードは特徴量 PAY_0 を値 1.5 で分割しました」
Pythonでのハイパーパラメータチューニング