0

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

  • My question were corrected – Mobin Ghanbari Jan 23 '21 at 12:21
  • 2
    `split()` splits a string where there is a space. So while you intend "Toyota RAV4" to be one thing, it's two. Just use a delimeter that does't appear in your string and separate the entries with that, i.e., 'Toyota RAV4;XLE FWD;36,200 miles; Stafford, TX' and then `input().split(';')`. – Reti43 Jan 23 '21 at 13:11
  • Hello, your service Reti 43. Please write your answer in code in the answer field. Thanks – Mobin Ghanbari Jan 23 '21 at 16:46
  • Does this answer your question? [Split a string by spaces -- preserving quoted substrings -- in Python](https://stackoverflow.com/questions/79968/split-a-string-by-spaces-preserving-quoted-substrings-in-python) – Reti43 Jan 23 '21 at 16:52
  • Your first answer was correct. If you can. Write the same as code. – Mobin Ghanbari Jan 23 '21 at 16:59

0 Answers0