标签:RandomForestClassifier max train estimator 随机 test import sklearn 森林
随机森林:就是字面意思,在一个地方,有多个树一起组成的森林。也就是由多棵决策树来组成对于问题的判断。
树林的形成:
对于每一个树木,他的训练集和特征要不一样才可以发挥数的特性,否则就是将一颗决策树多次复制。因此,对于训练集的选择,使用bootstrap的抽样方法,就是不放回随机抽样。特征的选取也采用这种方法
API:
from sklearn.ensemble import RandomForestClassifier
RandomForestClassifier(n_estimators=100, criterion='gini', max_depth=None, bootstrap, min_samples_split=2)
# n_estimator 树木的个数
# criteria 默认gini,可选
# max_depth 树的最大深度
# max_features ‘auto’ 和‘sqrt’效果一样,开方决定个数
# ‘log2’ 将特征总数取log2得到
# ‘None’ 所有特征都使用
from math import log2
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
data = pd.read_csv('DS/wine/winequality-red.csv')
feature = data.iloc[:, :11]
target = data.iloc[:, 11:]
target = target.values.ravel()
x_train, x_test, y_train, y_test = train_test_split(
feature, target, random_state=0, test_size=0.25)
estimator = RandomForestClassifier()
dir = {'n_estimators': [120],
'max_depth': [14],
'max_features': [None, 'log2', 'auto']}
estimator = GridSearchCV(estimator, param_grid=dir, cv=8)
estimator.fit(x_train, y_train)
print(estimator.score(x_test, y_test))
print(estimator.best_estimator_)
0.7
RandomForestClassifier(max_depth=14, n_estimators=120)
可见,随机森林的预测效果相比于单颗树的预测效果还是较好的
标签:RandomForestClassifier,max,train,estimator,随机,test,import,sklearn,森林 来源: https://blog.csdn.net/weixin_62077732/article/details/122520835
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。