学習と予測 機械学習

y_target = fnData['default.payment.next.month']
x_explanatory = fnData
x_explanatory = x_explanatory.drop('default.payment.next.month', axis=1)

x_train,x_test,y_train, y_test = train_test_split(x_explanatory, y_target, stratify = y_target, random_state = 1)

ランダムフォレストのbaseモデルで学習

rf_model = RandomForestClassifier(random_state=0)
rf_model.fit(x_train,y_train)
RandomForestClassifier(random_state=0)
rf_model.score(x_test,y_test)
0.796
rf_pred = rf_model.predict(x_test)
f1_score(y_test, rf_pred)
0.33766233766233766
fpr, tpr, thresholds = roc_curve(y_test, rf_pred)
fpr, tpr, thresholds
(array([0.        , 0.05102041, 1.        ]),
 array([0.        , 0.24074074, 1.        ]),
 array([2, 1, 0], dtype=int64))
auc = roc_auc_score(y_test, rf_pred)
auc
0.5948601662887377
plt.plot(fpr, tpr, label='ROC curve (area = %.2f)'%auc)
plt.legend()
plt.title('ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.grid(True)

XGBのbaseモデルで学習と予測

xgb_model = xgb.XGBClassifier(random_state=0)
xgb_model.fit(x_train,y_train)
xgb_model.score(x_test,y_test)
0.796
xgb_pred = xgb_model.predict(x_test)
f1_score(y_test, xgb_pred)
0.42696629213483145
fpr, tpr, thresholds = roc_curve(y_test, xgb_pred)
fpr, tpr, thresholds
(array([0.        , 0.08163265, 1.        ]),
 array([0.        , 0.35185185, 1.        ]),
 array([2, 1, 0]))
auc = roc_auc_score(y_test, xgb_pred)
auc

0.6351

plt.plot(fpr, gpr, label='ROC curve(area = % , 2f)' %auc)
plt.legend()
ple.title('ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.grid(True)

コメント

タイトルとURLをコピーしました