AttributeError:' str'对象没有属性' fit'

时间:2018-05-30 13:35:44

标签: python machine-learning adaboost ensemble-learning boosting

您好我想在蘑菇数据集上使用一个简单的AdaBoostClassifier来填充smth。像:

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   

...

使用:

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)

但这会让我回头:

  

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

     

属性错误:' str'对象没有属性' fit'

2 个答案:

答案 0 :(得分:1)

我找到了这个问题。作为base_estimator,我设置了'DecisionTreeClassifier'。这是一种刺痛,没有fit()方法。 AdaBoost不是字符串。

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

答案 1 :(得分:1)

参考我在2Obe的回答中的评论,我找到了指定参数的正确方法-

RotorView

它应该是构造函数而不是字符串