Ajay Jaglan
Ajay Jaglan

Reputation: 11

Fitting VotingClassifier

I'm learning scikit-learn. I've been trying to optimise a classifier for a particular task where performance is judges by log loss. I'm trying to check if a VotingClassifier() can improve my performance using my two best performing classifiers.

Here's my code:

estimators = [RandomForestClassifier(random_state=0,n_estimators=500,bootstrap=False,criterion='entropy',
                                             max_depth=None,max_features='auto'),
                      LogisticRegression(solver='lbfgs',C=opt_c)]
        vc = VotingClassifier(estimators=estimators,voting='soft',weights=[1,1])
        vc.fit(X_train_std,y_train.as_matrix())
        vcp = vc.predict_proba(X_valid_std)
        print('Score: {}\tLog Loss: {}'.format(vc.score(X_valid_std,y_valid),log_loss(y_valid,vcp)))

However, when I try to run this code, I get the following error:

    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-123-8cd305ba77b8> in <module>()
----> 1 vc.fit(X_train_std,y_train.as_matrix())

/afs/-omited-/miniconda2/envs/iaml/lib/python2.7/site-packages/sklearn/ensemble/voting_classifier.pyc in fit(self, X, y, sample_weight)
    170                     raise ValueError('Underlying estimator \'%s\' does not'
    171                                      ' support sample weights.' % name)
--> 172         names, clfs = zip(*self.estimators)
    173         self._validate_names(names)
    174 

/afs/-ommited-/miniconda2/envs/iaml/lib/python2.7/site-packages/sklearn/ensemble/base.pyc in __iter__(self)
    145     def __iter__(self):
    146         """Returns iterator over estimators in the ensemble."""
--> 147         return iter(self.estimators_)
    148 
    149 

AttributeError: 'RandomForestClassifier' object has no attribute 'estimators_'

Can someone explkain why this is happening?

Upvotes: 0

Views: 1160

Answers (1)

Ajay Jaglan
Ajay Jaglan

Reputation: 11

I figured out my mistake, The estimators attribute takes a list of tuples.

Upvotes: 1

Related Questions