开始使用免费开始使用

模型调优

在处理高度不平衡的欺诈数据时,一个简单的方式是,在定义 sklearn 模型时使用 class_weights 选项 来调整随机森林模型。不过,正如您将看到的,这是一种相对"钝"的手段,未必适用于您的特定场景。

在本练习中,您将基于上一个练习的 Random Forest 模型,探索 weight = "balanced_subsample" 模式。您已经把数据划分为训练集和测试集,即 X_trainX_testy_trainy_test 已就绪。评估指标函数也已导入。

本练习是课程的一部分

Python 中的欺诈检测

查看课程

练习说明

  • 将分类器的 class_weight 参数设为 balanced_subsample
  • 在训练集上拟合模型。
  • 基于 X_test 获取预测结果和预测概率。
  • 计算并查看 roc_auc_score、分类报告和混淆矩阵。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Define the model with balanced subsample
model = RandomForestClassifier(class_weight='____', random_state=5)

# Fit your training model to your training set
model.fit(____, ____)

# Obtain the predicted values and probabilities from the model 
predicted = ____.____(____)
probs = ____.____(____)

# Print the roc_auc_score, the classification report and confusion matrix
print(____(____, ____))
print(____(____, ____))
print(____(____, ____))
编辑并运行代码