Python 2.7 为什么我在Python中得到以下AttributeError?

Python 2.7 为什么我在Python中得到以下AttributeError?,python-2.7,scikit-learn,Python 2.7,Scikit Learn,我使用的是sklearn的GradientBoosting回归方法。所以在用2000个估计器拟合之后,我想给它添加更多的估计器。由于重新运行整个拟合过程花费的时间太长,因此我使用了set_params()方法。请注意,这是一个多目标问题,也就是说,我有3个目标需要满足。所以我使用下面的代码来添加更多的估计器 '''parameters: models (list of length 3 in our case ) train_X, train_y [n_sampl

我使用的是sklearn的GradientBoosting回归方法。所以在用2000个估计器拟合之后,我想给它添加更多的估计器。由于重新运行整个拟合过程花费的时间太长,因此我使用了set_params()方法。请注意,这是一个多目标问题,也就是说,我有3个目标需要满足。所以我使用下面的代码来添加更多的估计器

'''parameters: models (list  of length 3 in our case )
               train_X, train_y [n_samples x 3], test
               n_estimators : previous + 500 (default) [additional estimators]
               warm_start : True (default)
'''

def addMoreEstimators(train_X, train_y, test, models, n_estimators = 500, warm_start=True):
    params = {'n_estimators':n_estimators, 'warm_start':warm_start}

    gbm_pred= pd.DataFrame()

    for (i,stars),clf in zip(enumerate(['*','**','***']), models):
        clf.set_params(**params)
        %time clf.fit(train_X.todense(),train_y[stars])
        %time gbm_pred[stars] = clf.predict(test.todense())

    gbm_pred = gbm_pred.as_matrix()    
    gbm_dict ={'model': gbm, 'prediction': gbm_pred}

    return gbm_dict
注意:
models
参数是针对3个目标的3个已安装型号的列表

当我第一次使用2500(最初我有2000个估计器)运行它时,它运行得很好,并给了我一个输出

当我使用3000个估计器运行同一个函数时,我得到了一个
AttributeError
(请参阅下面的错误回溯)。这里的模型包含3个已安装的模型。下面是错误的回溯:(有点长)

注意在上面的代码中,我删除了一些打印语句以减少混乱


这是我正在使用的两个函数,除此之外没有其他功能(除了用于分割数据的代码)。

看这行:似乎clf.estimators包含一个int,其中应该包含DecisionTreeRegressor实例。在GradientBoostingClassifier()中,我看不到任何地方会发生这种情况,但可能存在错误。或者,您是否正在做任何会覆盖估计器数组中的条目的事情?谢谢@RodrigoQueiro的回复。不,我不会做任何事情来覆盖估计器数组或任何其他内部内容,除非它是在我不知情的情况下发生的。如果任何人不能重现问题,他们将很难帮助你。您应该尝试找出重现问题的最简单的代码+数据组合,或者开始钻研sklearn的代码,找出将int放入估计器的原因。这可能是sklearn中的一个bug…您至少需要发布生成“模型”的代码,但最好发布一个最小的示例。谢谢。我将很快编辑我的帖子,并输入生成模型的代码。我还将尝试查看在随机生成的集合上是否出现相同的错误。
AttributeError                            Traceback (most recent call last)
<ipython-input-104-9418ada3b36f> in <module>()
      7                                                                 test = val_X_tfidf[:,shortened_col_index],
      8                                                                 models = models,
----> 9                                                                 n_estimators = 3000)
     10 
     11 reduced_features_gbm_pred_3000_2_lr_1_msp_2 = reduced_features_gbm_model_3000_2_lr_1_msp_2['prediction']

<ipython-input-103-e15a4fb70b50> in addMoreEstimators(train_X, train_y, test, models, n_estimators, warm_start)
     15         
     16         clf.set_params(**params)
---> 17         get_ipython().magic(u'time clf.fit(train_X.todense(),train_y[stars])')
     18         print 'starting prediction'
     19 

//anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2305         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2306         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2307         return self.run_line_magic(magic_name, magic_arg_s)
   2308 
   2309     #-------------------------------------------------------------------------

//anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2226                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2227             with self.builtin_trap:
-> 2228                 result = fn(*args,**kwargs)
   2229             return result
   2230 

//anaconda/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)

//anaconda/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

//anaconda/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)
   1160         if mode=='eval':
   1161             st = clock2()
-> 1162             out = eval(code, glob, local_ns)
   1163             end = clock2()
   1164         else:

<timed eval> in <module>()

//anaconda/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyc in fit(self, X, y, sample_weight, monitor)
    973                                     self.estimators_.shape[0]))
    974             begin_at_stage = self.estimators_.shape[0]
--> 975             y_pred = self._decision_function(X)
    976             self._resize_state()
    977 

//anaconda/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyc in _decision_function(self, X)
   1080         # not doing input validation.
   1081         score = self._init_decision_function(X)
-> 1082         predict_stages(self.estimators_, X, self.learning_rate, score)
   1083         return score
   1084 

sklearn/ensemble/_gradient_boosting.pyx in sklearn.ensemble._gradient_boosting.predict_stages (sklearn/ensemble/_gradient_boosting.c:2502)()

AttributeError: 'int' object has no attribute 'tree_'
from sklearn import ensemble
def updated_runGBM(train_X, train_y, test, 
           n_estimators =100, 
           max_depth = 1, 
           min_samples_split=1,
           learning_rate=0.01, 
           loss= 'ls',
           warm_start=True):
    '''train_X : n_samples x m_features
       train_y : n_samples x k_targets (multiple targets allowed)
       test    : n_samples x m_features
       warm_start : True (originally the default is False, but I want to add trees)
    '''
    params = {'n_estimators': n_estimators, 'max_depth': max_depth, 'min_samples_split': min_samples_split,
              'learning_rate': learning_rate, 'loss': loss,'warm_start':warm_start}
    gbm1 = ensemble.GradientBoostingRegressor(**params)
    gbm2 = ensemble.GradientBoostingRegressor(**params)
    gbm3 = ensemble.GradientBoostingRegressor(**params)

    gbm = [gbm1,gbm2,gbm3]

    gbm_pred= pd.DataFrame()
    for (i,stars),clf in zip(enumerate(['*','**','***']), gbm):
        %time clf.fit(train_X.todense(),train_y[stars])
        %time gbm_pred[stars] = clf.predict(test.todense())

    gbm_pred = gbm_pred.as_matrix()
    gbm_pred = np.clip(gbm_pred,0,np.inf)       
    gbm_dict ={'model': gbm, 'prediction': gbm_pred}       

    return gbm_dict