在运行期间计算python中GradientBoostingClassifier的损失
16lz
2021-01-22
I have the following code for creating and training a sklearn.ensemble.GradientBoostingClassifier
我有以下用于创建和训练sklearn.ensemble.GradientBoostingClassifier的代码
class myMonitor:
def __call__(self, i, estimator, locals):
proba = estimator.predict_proba(Xp2)
myloss = calculateMyLoss(proba, yp2) # calculateMyLoss is defined
# further on
print("Calculated MYLOSS: ",myloss)
return False
... #some more code
model = GradientBoostingClassifier(verbose=2, learning_rate = learningRate, n_estimators=numberOfIterations, max_depth=maxDepth, subsample = theSubsample, min_samples_leaf = minLeafSamples, max_features=maxFeatures)
model.fit(Xp1, yIntegersp1, monitor = myMonitor())
However, when I run this code I get the error:
但是,当我运行此代码时,我收到错误:
model.fit(Xp1, yIntegersp1, monitor = myMonitor())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.py", line 980, in fit
begin_at_stage, monitor)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.py", line 1058, in _fit_stages
early_stopping = monitor(i, self, locals())
File "OTTOSolverGBM.py", line 44, in __call__
proba = estimator.predict_proba(Xp2)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.py", line 1376, in predict_proba
score = self.decision_function(X)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.py", line 1102, in decision_function
score = self._decision_function(X)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.py", line 1082, in _decision_function
predict_stages(self.estimators_, X, self.learning_rate, score)
File "sklearn/ensemble/_gradient_boosting.pyx", line 115, in sklearn.ensemble._gradient_boosting.predict_stages (sklearn/ensemble/_gradient_boosting.c:2502)
AttributeError: 'NoneType' object has no attribute 'tree_'
Why can't I use the same estimator (which is not None
, I checked) to calculate the class probabilities during the run? is there a way to accomplish what I want (i.e. check the model on validation data at every iteration of the fitting procedure)?
为什么我不能使用相同的估算器(我检查的不是None)来计算运行期间的类概率?有没有办法实现我想要的(即在拟合程序的每次迭代中检查验证数据的模型)?
2 个解决方案
#1
Your estimator
is self
. Try
你的估算是自我的。尝试
def __call__(self, i, locals)
proba = self.predict_proba(Xp2)
更多相关文章
- 计算机视觉相关代码片段(Python)
- 120行python代码解锁10000分微信跳一跳
- XGBoost中参数调优的完整指南(含Python-3.X代码)
- 求助:Python是否可以用一行代码来同时给变量赋值并打印变量的值
- 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)
- 怎样写贪吃蛇小游戏?用100行python代码轻松解决!
- 建模分析之机器学习算法(附python&R代码)
- 读取python中的unicode文件,该文件以与python源代码相同的方式声
- 自动完成在VS代码和Python中的自动化对象