RandomForest随机森林模型训练

第一次用随机森林做分类,使用sklearn中的库,直接进行模型训练。下面写出了随机森林中的一些参数,仅供参考。(适合像我这样的菜鸟参考学习)

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
# train_test_split 划分训练集 测试集
# cross_val_score 交叉验证调节某个参数
# GridSearchCV 超参调节 调节多个参数
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
# roc_auc_score分数返回roc曲线下的面积
from sklearn.metrics import roc_auc_score,auc,roc_curve

data = pd.read_csv('./data/new.csv')
# print(data.head())

# 查看1和0的数量
# print(data.flag.value_counts())

# 把标记赋值给y
y = data.flag
# print(y)
# 去掉标签列flag 赋值给x
x = data.drop('flag',axis=1)
# print(x)
# y为标签 x为数据
# test_size表示测试集数据所占比例,random_state表示若要重复试验保证每次随机的实验结果是一样的
xtrain, xtest, ytrain, ytest = train_test_split(x, y,test_size=0.2, random_state=5 )

# RandomForestClassifier参数
# n_estimators 森林中树的数量,即基评估器的数量
# max_depth= 树的最大深度 默认为none 这样建树时,会使每一个叶节点只有一个类别,或是达到min_samples_split。
# min_samples_split:根据属性划分节点时,每个划分最少的样本数
# min_samples_leaf:叶子节点最少的样本数。
# criterion = 使用信息熵entropy或者基尼系数gini.默认是基尼系数
rfc = RandomForestClassifier(n_estimators=220,min_samples_leaf=10,min_samples_split=30,max_depth=8,random_state=10,
                             criterion='gini',class_weight=None)
rfc.fit(xtrain,ytrain)

# 导入测试集 rfc的接口score计算的是模型准确率accuracy
# result = rfc.score(xtest,ytest)
# print(result)

# 查看所有的决策树
# print(rfc.estimators_)

# 查看结果的类别
# print(rfc.classes_)
# 查看类别数量
# print(rfc.n_classes_)

# 查看预测结果标签值
# print(rfc.predict(xtest))

# 标签是1的可能性 出来的结果左边的标签值为0的概率 右边是标签值为1的概率
# print(rfc.predict_proba(xtest)[:,:])
# 只取标签为1的概率
# print(rfc.predict_proba(xtest)[:,1])

# roc分数的计算
# sc = roc_auc_score(ytest,rfc.predict_proba(xtest)[:,1])
# print(sc)

# 各个feature的重要性
# print(rfc.feature_importances_)

# sklearn.model_selection.cross_val_score(estimator= ,X= , y= ,scoring= ,cv= ,n_jobs=, verbose= )
# estimator:估计方法对象(分类器)
# X: 数据特征(Features)
# y: 数据标签
# soring:调用方法(包括accuracy和mean_squared_error等等)
# cv: 几折交叉验证


# 结果统计 计算P,R,F1,accuracy
p = precision_score(list(ytest),rfc.predict(xtest),average='weighted')
r = recall_score(list(ytest),rfc.predict(xtest),average='weighted')
f1 = f1_score(list(ytest),rfc.predict(xtest),average='weighted')

print(p)    # 0.9038104235553457
print(r)    # 0.903448275862069
print(f1)   # 0.9034574607673332





#  调整参数

# 查看训练集
# print(xtrain.shape)

# 调整森林中树的数量

# param_test1 = {'n_estimators': range(20,500,10)}
# estimator输入分类器(分类器的参数)
# param_grid= 需要调参的超参名
# scoring = 每次评估使用的分数
# cv = 每次进行几折交叉验证
# gsearch1 = GridSearchCV(estimator=RandomForestClassifier(min_samples_split=50,
#                                                          min_samples_leaf=20,
#                                                          max_depth=8,random_state=10),
#                         param_grid=param_test1,
#                         scoring='roc_auc',
#                         cv=3)
# gsearch1.fit(xtrain,ytrain)
# # 输出最好的参数和最好的分数
# print(gsearch1.best_params_,gsearch1.best_score_)
# 输出结果 {'n_estimators': 220} 0.8620994691397194

# 调整最小样本数 和 最小叶节点的样本数
# param_test2 = {'min_samples_split':range(20,200,10),'min_samples_leaf':range(10,200,10)}
# gsearch2 = GridSearchCV(estimator=RandomForestClassifier(n_estimators=220,
#                                                          max_depth=8,random_state=10),
#                         param_grid=param_test2,
#                         scoring='roc_auc',
#                         cv=3)
# gsearch2.fit(xtrain,ytrain)
# 输出最好的参数和最好的分数
# print(gsearch2.best_params_,gsearch2.best_score_)
# 输出结果{'min_samples_leaf': 10, 'min_samples_split': 30} 0.8675084144386757


# 调整最大深度
# param_test3 = {'max_depth':range(2, 50, 2)}
# gsearch3 = GridSearchCV(estimator=RandomForestClassifier(n_estimators=220,
#                                                          min_samples_leaf = 10,
#                                                          min_samples_split = 30,
#                                                          random_state=10),
#                         param_grid=param_test3,
#                         scoring='roc_auc',
#                         cv=3)
# gsearch3.fit(xtrain, ytrain)
# 输出最好的参数和最好的分数
# print(gsearch3.best_params_, gsearch3.best_score_)
# 输出结果{'max_depth': 8} 0.8675084144386757

# scor = roc_auc_score(ytest, gsearch3.best_estimator_.predict_proba(xtest)[:, 1])
# print(scor)
# 0.964570943075616

# print(gsearch3.best_estimator_)

# 调整分类方法 和样本平衡
# param_test4 = {'criterion':['gini','entropy'], 'class_weight':[None,'balanced']}
# gsearch4 = GridSearchCV(estimator=RandomForestClassifier(n_estimators=220,
#                                                          min_samples_leaf=10,
#                                                          min_samples_split=30,
#                                                          max_depth=8,
#                                                          random_state=10),
#                         param_grid=param_test4,
#                         scoring='roc_auc',
#                         cv=3)
# gsearch4.fit(xtrain,ytrain)
# print(gsearch4.best_params_, gsearch4.best_score_)
# {'class_weight': None, 'criterion': 'entropy'} 0.8722979957299399

# scor = roc_auc_score(ytest, gsearch4.best_estimator_.predict_proba(xtest)[:, 1])
# print(scor)
# 0.9615972812234495
  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
随机森林是一种集成学习算法,它由多个决策树组成。随机森林通过随机化特征选择和样本选择的方式,构建多个决策树模型,并将它们集成为一个强分类器。 在随机森林算法中,首先从原始数据集中有放回地进行有放回抽样,形成多个训练子集。对于每个训练子集,随机森林使用CART(分类与回归树)算法建立一个决策树。建立决策树的过程中,采用随机化特征选择,即在每次划分节点时,仅考虑子集的一部分特征。这种随机化特征选择可以让每个决策树都对数据集有所差异,增加随机性,避免模型过拟合。 当所有决策树建立完成后,随机森林的分类器通过投票或者求平均值的方式来进行集成预测。对于分类问题,多数投票法是常用的集成方式。对于回归问题,可以将各个决策树的预测结果求平均值来得到最终结果。 随机森林具有以下优点:首先,它能够处理高维度的数据集,并且对缺失数据和异常数据具有较好的鲁棒性;其次,它能够自动进行特征选择,通过不同决策树之间的差异性,可以评估各个特征的重要性;此外,随机森林还可以进行并行计算,提高了训练速度。 总之,随机森林算法通过构建多个决策树进行集成学习,利用随机化特征选择和样本选择的方式,能够处理高维度数据、高效地进行特征选择,并且对于分类和回归问题都有良好的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值