输出用于训练BaggingClassifier中每个base_estimator的实例子集

我正在使用决策树桩和BaggingClassifier来对一些数据进行分类:

def fit_ensemble(attributes,class_val,n_estimators):    # 最大深度为1
    decisionStump = DecisionTreeClassifier(criterion = 'entropy', max_depth = 1)
    ensemble = BaggingClassifier(base_estimator = decisionStump, n_estimators = n_estimators, verbose = 3)
    return ensemble.fit(attributes,class_val)
def predict_all(fitted_classifier, instances):
    for i, instance in enumerate(instances):
        instances[i] = fitted_classifier.predict([instances[i]])
    return list(itertools.chain(*instances))
def main(filename, n_estimators):
    df_ = read_csv(filename)
    col_names = df_.columns.values.tolist()
    attributes = col_names[0:-1] ## 0..n-1
    class_val = col_names[-1] ## n
    fitted = fit_ensemble(df_[attributes].values, df_[class_val].values, n_estimators)
    fitted_classifiers = fitted.estimators_ # 获取三个决策树桩。
    compared_ = DataFrame(index = range(0,len(df_.index)), columns = range(0,n_estimators + 1))
    compared_ = compared_.fillna(0)
    compared_.ix[:,n_estimators] = df_[class_val].values
    for i, fitted_classifier in enumerate(fitted_classifiers):
        compared_.ix[:,i] =  predict_all(fitted_classifier,df_[attributes].values)

我想检查用于训练每个决策树桩的随机子集。我查看了集成和决策树类的文档,但没有找到任何属性或方法可以显示训练子集。这是一项徒劳的任务吗?还是在树训练时,有什么方法可以输出训练子集?

我对pandas很陌生,但我来自R的背景。我的代码肯定没有优化,尽管我可以保证我的任务的数据集非常小。谢谢你的帮助。


回答:

看起来我已经自己回答了这个问题。DecisionTreeClassifier的estimators_samples_属性正是我想要的。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注