0

I'm performing the Decision Tree with the help of below sample data.

enter image description here

So I've converted the above data to LabelEncoder to perform Decision Tree and successfully created a DT model.

enter image description here

So now my requirement is I would like to predict on the below values, So how to pass these values in the python code.

enter image description here

In order to predict the existing value I can use the below Predict Code.

model.predict([[2,1,1]])

COMPLETE CODE

import pandas as pd

df = pd.read_csv(r"salaries.csv")
df.head()

inputs = df.drop('salary_more_then_100k',axis='columns')
target = df['salary_more_then_100k']

from sklearn.preprocessing import LabelEncoder
le_company = LabelEncoder()
le_job = LabelEncoder()
le_degree = LabelEncoder()

inputs['company_n'] = le_company.fit_transform(inputs['company'])
inputs['job_n'] = le_job.fit_transform(inputs['job'])
inputs['degree_n'] = le_degree.fit_transform(inputs['degree'])

inputs_n = inputs.drop(['company','job','degree'],axis='columns')

from sklearn import tree
model = tree.DecisionTreeClassifier()
model.fit(inputs_n, target)

model.score(inputs_n,target)
Vikas
  • 199
  • 1
  • 7
  • Maybe [this](https://stackoverflow.com/questions/21057621/sklearn-labelencoder-with-never-seen-before-values) is your answer – Vaziri-Mahmoud Sep 26 '20 at 11:13
  • thanks for that. I've tried as per your suggestion by using the code ```new_list = ['google'] print(le_company.fit_transform(new_list))``` and I've tested for google where it is displaying output ```[0]``` but it has to be ```[2]``` – Vikas Sep 26 '20 at 11:49
  • You should use `le_company.transform(new_list)` – Vaziri-Mahmoud Sep 26 '20 at 11:51
  • I've modified and is working when I pass **google** but when i pass **apple** it is saying ```y contains previously unseen labels: ['apple']``` – Vikas Sep 26 '20 at 11:56
  • Because encoder never has seen 'apple'. You should read [this](https://stackoverflow.com/questions/21057621/sklearn-labelencoder-with-never-seen-before-values) – Vaziri-Mahmoud Sep 26 '20 at 12:01
  • Got that. If i want to predict on new values how will i do that? – Vikas Sep 26 '20 at 12:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222116/discussion-between-vikas-and-vaziri-mahmoud). – Vikas Sep 26 '20 at 13:46

0 Answers0