forked from SergeGuillemart/Hackathon-BigData-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeCompleted.py
More file actions
142 lines (88 loc) · 2.86 KB
/
Copy pathTreeCompleted.py
File metadata and controls
142 lines (88 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 20 11:48:12 2019
@author: THIBAULT Sébastien
"""
import numpy as np
import pandas as pd
import io
from IPython.display import clear_output
from matplotlib import pyplot as plt
f = open('ressources\\Traite\\Finished\\Total3.csv',encoding="utf-16")
df = pd.read_csv(f)
df.pop('DATE')
df.pop('MIN')
df.pop('CODE_POSTAL')
df.pop('LOCALITE')
df.pop('LANGUE1')
df.pop('LANGUE2')
df.pop('LANGUE3')
df.pop('LANGUE4')
df.pop('LANGUE5')
#df.pop('KM')
df = df.dropna()
L1 = df.pop('PAYS')
#df.pop('KM')
y = df.pop('COMMENT_NOUS_AVEZ_VOUS_DECOUVERT')
for i in range(len(y)):
if(not (L1.values[i] in ["FRA","LUX","BEL","DEU"])):
L1.values[i] = "OTHER"
for i in range(len(y)):
if (y.values[i].endswith('visite')or y.values[i]=='DEJנVENU'):
y.values[i] = "Xeme_Visite"
classes = list(dict.fromkeys(y))
print(classes)
#print(y)
#display(df)
from sklearn import tree
from sklearn.preprocessing import OneHotEncoder
from sklearn.tree.export import export_text
enc = OneHotEncoder(handle_unknown='ignore',sparse=False)
L1 = L1.values.reshape(-1,1)
enc.fit(L1)
L1bis = enc.transform(L1)
#print(L1bis)
L11 = []
for i in L1:
L11 = L11+ [ ""+i[0]+""]
#print(L11)
L1list = list(dict.fromkeys(L11))
print(L1list)
####################### Km + Country -> Comm
df2 = np.append(L1bis,df,axis=1)
clf = tree.DecisionTreeClassifier(max_depth=4)
clf = clf.fit(df2, y)
tree.plot_tree(clf.fit(df2,y),feature_names=L1list+["KM"])
print(export_text(clf,feature_names=L1list+["KM"]))
dotfile = open("dt.dot", 'w')
tree.export_graphviz(clf, out_file=dotfile,feature_names=L1list+["KM"],class_names = clf.classes_,impurity=False)
dotfile.close()
####################### Km -> comm
clf2 = tree.DecisionTreeClassifier(max_depth=4)
clf2 = clf2.fit(df, y)
tree.plot_tree(clf2.fit(df,y),feature_names=["KM"])
print(export_text(clf2))
dotfile = open("dt.dot", 'w')
tree.export_graphviz(clf2, out_file=dotfile,feature_names=["KM"],class_names = clf2.classes_,impurity=False)
dotfile.close()
####################### country -> comm
clf3 = tree.DecisionTreeClassifier(max_depth=4)
clf3 = clf3.fit(L1bis, y)
tree.plot_tree(clf3.fit(L1bis,y),feature_names=L1list)
print(export_text(clf3))
dotfile = open("dt.dot", 'w')
tree.export_graphviz(clf3, out_file=dotfile,feature_names=L1list,class_names = clf3.classes_,impurity=False)
dotfile.close()
####################### determine nb visites
y4 = y
df4 = df2
for j in range(len(y4)):
if(y4.values[j]!="Xeme_Visite"):
y4.values[j] = "First"
clf4 = tree.DecisionTreeClassifier(max_depth=4)
clf4 = clf4.fit(df2, y)
tree.plot_tree(clf4.fit(df2,y),feature_names=L1list+["KM"])
print(export_text(clf4,feature_names=L1list+["KM"]))
dotfile = open("dt.dot", 'w')
tree.export_graphviz(clf4, out_file=dotfile,feature_names=L1list+["KM"],class_names = clf4.classes_,impurity=False)
dotfile.close()