Even if I try various things with the code below, I still get an error.You can also specify arguments, but I don't know where and which arguments to pass.
Please give me some advice.
source code
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import acuity_score
from sklearn.utils import all_estimators
import warnings
warnings.filterwarnings('ignore')
# loading iris data
iris_data=pd.read_csv("iris.csv", encoding="utf-8")
# separate iris data into labels and input data
y = iris_data.loc [:, "Name" ]
x=iris_data.loc[:, ["SepalLength", "SepalWidth", "PetalLength", "PetalWidth"]]
# separate for learning and testing
warnings.filterwarnings('ignore')
x_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2,train_size=0.8,shuffle=True)
# Obtain all classifier algorithms ---(*1)
warnings.filterwarnings('ignore')
allAlgorithms=all_estimators(type_filter="classifier")
for (name, algorithm) in all Algorithms:
# Create objects for each algorithm ---(*2)
clf = algorithm()
# Learn and evaluate ----(*3)
clf.fit(x_train,y_train)
y_pred=clf.predict(x_test)
print(name, "correct answer rate=", accuracy_score(y_test,y_pred))
Run Results
AdaBoostClassifier Answer Rate = 0.933333333333333333
BaggingClassifier Correct Rate = 0.93333333333333333333
BernoulliNB correct answer rate = 0.3
CalibratedClassifierCV correct answer rate = 0.9
CategoryNB correct answer rate = 0.966666666666666667
Traceback (most recent call last):
File"/Users/fujiokamasaya/PycharmProjects/project/test2.1.py", line 27, in<module>
clf = algorithm()
TypeError: __init__() missing 1 required positional argument: 'base_estimator'
Process completed with exit code 1
Either the program in the referenced book did not consider updating the library, or the action taken against this part was omitted from the book.
When I tried scikit-learn 0.22.1, all_estimators(type_filter="classifier")
was output in the following order:
This error is probably due to an attempt to run _init__()
for ClassifierChain
(clf=algorithm()
line) is always the initialized option./p>
As for the solution, it seems a little unreasonable to try to categorize various classifiers by looping them all at once. As a temporary solution, you can add an if statement to avoid the classifier whenever something goes wrong.For example, simply skip or do something else, as shown below:
for (name, algorithm) in allAlgorithms:
if name == 'ClassifierChain':
continue
# Create objects for each algorithm ---(*2)
clf = algorithm()
# ...
© 2024 OneMinuteCode. All rights reserved.