-1

I would like to train a voiting classifer in SciKit-Learn with three different classifiers. I'm having issues with the final step, which is printing the final accuracy scores of the classifiers.

from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

import pandas as pd
import numpy as np 

log_clf=LogisticRegression()
rnd_clf=RandomForestClassifier()
svm_clf=SVC()

voting_clf=VotingClassifier(estimators=[('lr',log_clf),('rf',rnd_clf),('svc',svm_clf)],voting='hard')
voting_clf.fit(X_train, y_train)

I am getting errors when I run the following code:

for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    clf.fit(X_train, y_train)
    y_predict=clf.predict(X_test)
    print(clf._class_._name_,accuracy_score(y_test,y_pred))

When I run this chunk of code I get the following:

AttributeError: 'LogisticRegression' object has no attribute '_class_'

I am assuming that calling 'class'is a bit outdated, so I changed class to 'classes_':

for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    clf.fit(X_train, y_train)
    y_pred=clf.predict(X_test)
    print(clf.classes_._name_,accuracy_score(y_test,y_pred))

When I run this chunk of code I get the following:

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

When I remove 'name' and run the following code, I still get an error:

for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    clf.fit(X_train, y_train)
    y_pred=clf.predict(X_test)
    print(clf.classes_,accuracy_score(y_test,y_pred))

Error:

 NameError: name 'accuracy_score' is not defined

I'm not sure why accuracy_score is not defined seeing that imported the library

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
sos.cott
  • 435
  • 3
  • 17

1 Answers1

1

For the first error about class, you need to have two underscores here.

Change

print(clf._class_._name_,accuracy_score(y_test,y_pred))

to:

print(clf.__class__.__name__, accuracy_score(y_test,y_pred))

See this question for other ways to get the name of an object in python:

Now for the second error about 'accuracy_score' not defined, this happens when you have not imported the accuracy_score correctly. But I can see that in your code you have imported the accuracy_score. So are you sure that you are executing the line print(clf.__class__.__name__, accuracy_score(y_test,y_pred)) in the same file? or in any different file?

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132