Decision tree with python : How to return a predicted value to column and export to excel

Viewed 34

Blockquote

how to insert test file.csv with blank in column 'CAR TYPE' (labels) then predict with desicion tree and return tha value to column 'CAR TYPE' after calculate from decision tree with parameter in column SPEED SEATS WHEEL

Blockquote file File car_class1 File car_class1_cartypeblank Code

import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg

#Insert dataset
df = pandas.read_csv("car_class1.csv")


#show data
print(df)

features = ['SPEED', 'SEATS', 'WHEEL']
X = df[features]
y = df['CAR_TYPE']
#print(X)
#print(y)

#Change string values into numerical values:

d = {'ECO_CAR': 0, 'VAN': 1,'PPV':2}
df['CAR_TYPE'] = df['CAR_TYPE'].map(d)
#print(df)


#Classify by Decision tree
dtree = DecisionTreeClassifier(
            criterion = "entropy",max_depth = 4 )

dtree = dtree.fit(X, y)
print('ANSWER = ', end = ' ')

print(dtree.predict([[110, 4,15]]))


data = tree.export_graphviz(dtree, out_file=None,feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mydecisiontree.png')

img=pltimg.imread('mydecisiontree.png')
imgplot = plt.imshow(img)
plt.show()
0 Answers
Related