在pythonsklearn集成库中,我想使用一些boosting方法(比如Adaboost)来训练数据。由于我想知道最佳估计数,我计划每次用不同数量的估计量做一个cv。然而,它似乎用以下方式来做是多余的:
for n in [50,100,150,200,250,300]:
model = AdaBoostClassifier(DecisionTreeClassifier(max_depth=1),n_estimators=n)
cross_val_score(model,x,y,k=5)
因为在AdaBoost中,一旦我将分类器训练为估计器=50,当我继续训练估计器=100时,前50个分类器及其权重不变。在这种情况下,我想知道是否有办法直接与第51名学习成绩差的学生一起开始培训。在
您可以拟合所有300个估计器,然后使用
AdaBoostClassifier.staged_predict()
来跟踪错误率如何依赖于估计器的数量。但是,您必须自己执行交叉验证拆分;我不认为它与cross_val_score()不兼容。在例如
可以使用继承对
AdaBoostClassifier
进行“黑客”操作,它不需要重新训练估计器,并且与sklearn
中的许多交叉验证函数兼容(必须是不洗牌数据的交叉验证)。在如果您查看}的行为,就可以避免不需要重新训练估计器。在
sklearn.ensemble.weight_boosting.py
中的源代码,您会发现,如果正确地包装AdaBoostClassifier.fit()
和{交叉验证函数的问题是,它们使用
sklearn.base.clone()
克隆原始估计器,而函数sklearn.base.clone()
生成估计器参数的深层副本。深度复制特性使得估计器不可能在不同的交叉验证运行之间“记住”它的估计器(clone()
复制引用的内容,而不是引用本身)。唯一的方法(至少我能想到的唯一方法)是使用全局状态来跟踪运行之间的旧估计量。这里的问题是,您必须计算X特性的散列值,这可能很昂贵!在不管怎样,以下是对
AdaBoostClassifier
本身的破解:这里有一个例子,它允许您比较黑客的时间/精确度与原始的
^{pr2}$AdaBoostClassifier
。请注意,当我们添加估计器时,测试hack的时间会增加,但是训练不会。我发现hack比原来运行得快得多,但是我没有对大量的X样本进行散列。在相关问题 更多 >
编程相关推荐