2015-05-09 3 views
0

я следующий код для создания и обучения sklearn.ensemble.GradientBoostingClassifierрасчета потерь на 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()) 

Однако, когда я запускаю этот код, я получаю ошибку:

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_' 

Почему я не могу использовать ту же самую оценку (которая не None, я проверил), чтобы рассчитать вероятности класса во время прогона? есть ли способ выполнить то, что я хочу (т. е. проверить модель на данных валидации на каждой итерации процедуры подгонки)?

ответ

1

estimatorявляетсяself. Попробуйте

def __call__(self, i, locals) 
    proba = self.predict_proba(Xp2) 
Смежные вопросы