I've used Decision Tree Classifier
and I want to enter my input
as a string
rather than giving an integer
value, but it gives me error
like:
Traceback (most recent call last):
File "D:/backup code for odoo project/New folder/New folder/main.py", line 38, in <module>
theme_res = lebel_encoder.transform(theme_input)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\preprocessing\_label.py", line 277, in transform
_, y = _encode(y, uniques=self.classes_, encode=True)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\preprocessing\_label.py", line 121, in _encode
return _encode_numpy(values, uniques, encode,
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\preprocessing\_label.py", line 50, in _encode_numpy
raise ValueError("y contains previously unseen labels: %s"
ValueError: y contains previously unseen labels: ['Food', 'cafe', 'sticky']
Code:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn import tree
df = pd.read_csv("new_data.csv", encoding='latin1')
inputs = df.drop('selected_theme', axis='columns')
target = df['selected_theme']
lebel_encoder = LabelEncoder()
inputs['main_cat_n'] = lebel_encoder.fit_transform(inputs['main_cat'])
inputs['sub_cat_n'] = lebel_encoder.fit_transform(inputs['sub_cat'])
inputs['nav_bar_n'] = lebel_encoder.fit_transform(inputs['nav_bar'])
inputs_n = inputs.drop(['main_cat', 'sub_cat', 'nav_bar'], axis='columns')
model = tree.DecisionTreeClassifier()
model.fit(inputs_n, target)
print(model.score(inputs_n, target))
theme_input = ['Food', 'cafe', 'sticky']
theme_res = lebel_encoder.transform(theme_input)
result_theme = model.predict(theme_res)
print(result_theme)
The error occurs before classifier, it happens on this line
theme_res = lebel_encoder.transform(theme_input)
error message says to you that your label_encoder
never saw such categories as "Food", "cafe", "sticky". It happens because you rewrite your LabelEncoders. You should use separate LabelEncoders for different features, e.g.:
categorical_features = ['main_cat', 'sub_cat', 'nav_bar']
encoders = dict()
for cat in categorical_features:
encoders[cat] = LabelEncoder()
inputs[f'{cat}_n'] = encoders[cat].fit_transform(inputs[cat])
inputs_n = inputs.drop(['main_cat', 'sub_cat', 'nav_bar'], axis='columns')
...