Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

python - Using Perceptron sklearn.ensemble.AdaBoostClassifier() gives an error

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:

  1. What is the solution with the possibility of using scikit-learn library?

  2. Is there any way to handle such an exception? For example, if it gives error, execute it again until desired results?

  3. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problematic part could be solved by try-catch block. For example,

#The problematic part solution
AdaBoost_accuracy = 0

while AdaBoost_accuracy == 0:
    try:
        classifier = AdaBoostClassifier(base_estimator=Perceptron(), n_estimators=15, algorithm='SAMME')
        classifier = classifier.fit(x_train, y_train)
        y_pred = classifier.predict(x_test)
        AdaBoost_accuracy = metrics.accuracy_score(y_test, y_pred)
    except:
        print("Let me reclassify AdaBoost again")

print("Accuracy of AdaBoost:", AdaBoost_accuracy)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...