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