栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

结合scikit学习中的随机森林模型

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

结合scikit学习中的随机森林模型

我相信可以通过修改RandomForestClassifier对象的

estimators_
n_estimators
属性来实现。森林中的每棵树都存储为DecisionTreeClassifier对象,这些树的列表存储在
estimators_
属性中。为了确保不存在不连续性,更改中的估计量也很有意义
n_estimators

这种方法的优点是,您可以跨多台机器并行构建一堆小森林并将其组合。

这是使用虹膜数据集的示例:

from sklearn.ensemble import RandomForestClassifierfrom sklearn.cross_validation import train_test_splitfrom sklearn.datasets import load_irisdef generate_rf(X_train, y_train, X_test, y_test):    rf = RandomForestClassifier(n_estimators=5, min_samples_leaf=3)    rf.fit(X_train, y_train)    print "rf score ", rf.score(X_test, y_test)    return rfdef combine_rfs(rf_a, rf_b):    rf_a.estimators_ += rf_b.estimators_    rf_a.n_estimators = len(rf_a.estimators_)    return rf_airis = load_iris()X, y = iris.data[:, [0,1,2]], iris.targetX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.33)# in the line below, we create 10 random forest classifier modelsrfs = [generate_rf(X_train, y_train, X_test, y_test) for i in xrange(10)]# in this step below, we combine the list of random forest models into one giant modelrf_combined = reduce(combine_rfs, rfs)# the combined model scores better than *most* of the component modelsprint "rf combined score", rf_combined.score(X_test, y_test)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号