I have the problem of using perceptrons for AdaBoost classifier.
The training and testing data from here
should be turned to 0 and 1 in the last column ("Poker Hand"), (it is from 1 to 9 inclusively originally), then both Decision Tree Classifier and AdaBoost Classifier with the total of 15 weak perceptron classifiers should be implemented in the data. I try to use scikit-learn libraries, but while my Decision Tree Classifier provides good results, AdaBoost Classifier throws error:
ValueError: BaseClassifier in AdaBoostClassifier ensemble is worse than random, ensemble can not be fit.
Here, the crucial parts of the code.
import pandas as pd
from sklearn.ensemble import AdaBoostClassifier
from sklearn.linear_model import Perceptron
from sklearn import metrics
if __name__ == "__main__":
data_train = pd.read_csv("poker-hand-testing.data",header=None)
data_test = pd.read_csv("poker-hand-training-true.data",header=None)
for value in range(0, len(data_train)):
if data_train[10][value] != 0:
data_train[10][value] = 1
for value in range(0, len(data_test)):
if data_test[10][value] != 0:
data_test[10][value] = 1
col=['Suit of card #1','Rank of card #1',
'Suit of card #2','Rank of card #2',
'Suit of card #3','Rank of card #3',
'Suit of card #4','Rank of card #4',
'Suit of card #5','Rank of card #5',
'Poker Hand']
data_train.columns=col
data_test.columns=col
y_train=data_train['Poker Hand']
y_test=data_test['Poker Hand']
x_train=data_train.drop('Poker Hand',axis=1)
x_test=data_test.drop('Poker Hand',axis=1)
#The problematic part
classifier = AdaBoostClassifier(base_estimator=Perceptron(), n_estimators=15, algorithm='SAMME')
classifier = classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test)
print("Accuracy of AdaBoost:", metrics.accuracy_score(y_test, y_pred))
The strange thing is that this error occurs only once per 9-10 times when I don't change values to binary ones, while binary values almost always gives an error. Also, changing Perceptron()
to SGDClassifier(loss="perceptron", eta0=1, learning_rate="constant", penalty=None)
also throws such errors.
My questions are:
What is the solution with the possibility of using scikit-learn library?
Is there any way to handle such an exception? For example, if it gives error, execute it again until desired results?
Are there any other alternatives where I can use both Decision Tree and AdaBoost with Perceptron, if it is couldn't be solved in scikit-learn library?
question from:
https://stackoverflow.com/questions/65856432/using-perceptron-sklearn-ensemble-adaboostclassifier-gives-an-error 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…