将 Random Forest 调整为欺诈检测
在本练习中,您将深入了解随机森林分类器的各项配置,我们会分配权重并调整森林中决策树的结构。您将手动定义权重,以略微抵消类别不平衡。在我们的示例中,欺诈样本约为 300,非欺诈样本约为 7,000。若将权重比设为 1:12,模型在训练时相当于得到约 1/3 欺诈与 2/3 非欺诈的比例,这样已经足够用于训练模型。
本练习的数据已拆分为训练集和测试集,您只需专注于定义模型。之后可以使用 get_model_results() 函数作为快捷方式。该函数会将模型拟合到训练数据上,进行预测,并获得与您在前面练习中类似的性能指标。
本练习是课程的一部分
Python 中的欺诈检测
练习说明
- 更改
weight选项,将非欺诈与欺诈的比率设为 1 比 12,并将划分准则设为 'entropy'。 - 将最大深度设置为 10。
- 将叶节点中的最小样本数设置为 10。
- 将模型中使用的树的数量设置为 20。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Change the model options
model = RandomForestClassifier(bootstrap=True, class_weight={0:____, 1:____}, criterion='____',
# Change depth of model
max_depth=____,
# Change the number of samples in leaf nodes
min_samples_leaf=____,
# Change the number of trees to use
n_estimators=____, n_jobs=-1, random_state=5)
# Run the function get_model_results
get_model_results(X_train, y_train, X_test, y_test, model)