I have ensembled 3 algorithms as below,

estimators = []
model1 = MultinomialNB().fit(X,y)
estimators.append(('Naive Bayes', model1))
model2 = LinearSVC(random_state=0).fit(X,y)
estimators.append(('SVM', model2))
model3 = RandomForestClassifier(bootstrap=True).fit(X,y)
estimators.append(('Random Forest', model3))

# create the ensemble model
ensemble = VotingClassifier(estimators)
results = model_selection.cross_val_score(ensemble, X, y)

while passing new sentence to test the ensembling I have used,

predicted = results.predict(X_new_tfidf)

But I am getting error:

AttributeError: 'numpy.ndarray' object has no attribute 'predict'

How can I fix/debug this?

  • $\begingroup$ results is the value of cross validation score which is a numpy array. You should call the predict using your model instance not the numpy instance. $\endgroup$ – Media Feb 7 '18 at 14:04

Your line results = model_selection.cross_val_score(ensemble, X, y) just returns an array of scores for each run of the cross validation, so the error is telling you that it's just an array of numbers, not a model itself and has no defined method for fitting things.

To use predict, you need to call it on a model object, which I'm assuming you're going for the VotingClassifier. Your ensemble variable has a fit method, so you want to call

predicted = ensemble.predict(X_new_tfidf)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.