Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - How do fix : ValueError: X has 6 features, but DecisionTreeClassifier is expecting 4 features as input

I wrote an ML script with sklearn in Python that you get the specifications of a car and the address of its place of sale and it predicts the price of the car.

my_code:

from sklearn import preprocessing, tree

ls = [['Toyota RAV4', 'XLE FWD', '36,280 miles', 'Stafford, TX'], ['Toyota Camry', 'SE I4 Automatic', '36,233 miles', 'Norco, CA']]
x = []
y = ['$19,480','$17,399']

c = preprocessing.LabelEncoder()
clf = tree.DecisionTreeClassifier()
for k in ls:
    c.fit(k)
    b = list(c.classes_)
    n = c.transform(k)
    x.append(list(n))



clf.fit(x,y)



bmi = [list(c.fit_transform(input().split()))]

answer = clf.predict(bmi)
print(answer[0])


My input:

Toyota RAV4 XLE FWD 36,280 miles Stafford, TX

I encoded the strings well but I got an error:

ValueError: X has 6 features, but DecisionTreeClassifier is expecting 4 features as input.

How can I solve this problem?

Thanks a lot

question from:https://stackoverflow.com/questions/65858857/how-do-fix-valueerror-x-has-6-features-but-decisiontreeclassifier-is-expecti

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...