pythonmachine-learningadaboostensemble-learningboosting

AttributeError: 'str' object has no attribute 'fit'


Hi I want to use a simple AdaBoostClassifier on the mushroom dataset which lools smth. like:

target  cap-shape  cap-surface  cap-color  bruises  odor  \
3059       0          2            3          2        1     5   
1953       0          5            0          3        1     5   
1246       0          2            2          3        0     5   
5373       1          5            2          8        1     2   
413        0          5            3          9        1     3   

...

using:

from sklearn.ensemble import AdaBoostClassifier
from sklearn.preprocessing import LabelEncoder
import pandas as pd

dataset = pd.read_csv('data\mushroom.csv',header=None)
dataset = dataset.sample(frac=1)
dataset.columns = ['target','cap-shape','cap-surface','cap-color','bruises','odor','gill-attachment','gill-spacing',
             'gill-size','gill-color','stalk-shape','stalk-root','stalk-surface-above-ring','stalk-surface-below-ring','stalk-color-above-ring',
             'stalk-color-below-ring','veil-type','veil-color','ring-number','ring-type','spore-print-color','population',
             'habitat']

for label in dataset.columns:
    dataset[label] = LabelEncoder().fit(dataset[label]).transform(dataset[label])


X = dataset.drop(['target'],axis=1)
Y = dataset['target']


AdaBoost = AdaBoostClassifier(base_estimator='DecisionTreeClassifier',n_estimators=400,learning_rate=0.01,algorithm='SAMME')

AdaBoost.fit(X,Y)

prediction = AdaBoost.score(Y)

print(prediction)

but this returns me:

---> 15 AdaBoost.fit(X,Y)

AttributeError: 'str' object has no attribute 'fit'


Solution

  • I have found the issue. As base_estimator I have set 'DecisionTreeClassifier'. THIS is a sting and has no fit() method. The AdaBoost IS NOT a string.

    from sklearn.ensemble import AdaBoostClassifier
    from sklearn.preprocessing import LabelEncoder
    
    for label in dataset.columns:
        dataset[label] = LabelEncoder().fit(dataset[label]).transform(dataset[label])
    
    X = dataset.drop(['target'],axis=1)
    Y = dataset['target']
    
    
    AdaBoost = AdaBoostClassifier(n_estimators=400,learning_rate=0.01,algorithm='SAMME')
    
    AdaBoost.fit(X,Y)
    
    prediction = AdaBoost.score(X,Y)
    
    print(prediction)
    

    0.9182668636139832