-
Notifications
You must be signed in to change notification settings - Fork 0
/
principalID3holdout.py
101 lines (72 loc) · 3.17 KB
/
principalID3holdout.py
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
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from matplotlib import pyplot as plt
# load dataset
pima = pd.read_csv("speedDating_trab.csv")
#substituimos todos os 0's da coluna prob e like para 1's
pima['prob']=pima['prob'].replace(0.0,1.0)
pima['like']=pima['like'].replace(0.0,1.0)
#o prob e like sao preenchido com a media
gf= pima['prob'].mean()
r= round(gf, 1)
pima['prob'].fillna(value=r,inplace=True)
gf= pima['like'].mean()
r=round(gf, 1)
pima['like'].fillna(value=r,inplace=True)
#os restantes com a moda exceto o age, age_o e id
gf= pima['met'].mode()[0]
pima['met'].fillna(value=gf,inplace=True)
gf= pima['length'].mode()[0]
pima['length'].fillna(value=gf,inplace=True)
gf= pima['int_corr'].mode()
pima['int_corr'].fillna(value=gf,inplace=True)
gf= pima['go_out'].mode()[0]
pima['go_out'].fillna(value=gf,inplace=True)
gf= pima['date'].mode()[0]
pima['date'].fillna(value=gf,inplace=True)
gf= pima['goal'].mode()[0]
pima['goal'].fillna(value=gf,inplace=True)
#para age e age_o faz-se a mediana
gf= pima['age_o'].median()
pima['age_o'].fillna(value=gf,inplace=True)
gf= pima['age'].median()
pima['age'].fillna(value=gf,inplace=True)
#aqui preenche o unico id a NaN por 22
pima['id'].fillna(value=22,inplace=True)
#convertemos de float para int
pima[['met','length','go_out','date','goal','age_o','age','id']]= pima[['met','length','go_out','date','goal','age_o','age','id']].astype(int)
#dropamos a tabela int_corr pq é irrelevante neste momento
pima = pima.drop('int_corr',1)
#divisao do cassos em q tem match e os q n tem match
resultados = pima['match']
dados = pima.drop(['match'],axis=1)
# Split dataset into training set and test set (30%/70%)
dados_treino, dados_teste, resultados_treino, resultados_teste= train_test_split( dados, resultados, test_size=0.3, random_state=1)
# Create Decision Tree classifer object
clf = DecisionTreeClassifier(criterion="entropy")
# Train Decision Tree Classifer
clf = clf.fit(dados_treino, resultados_treino)
#Predict the response for test dataset
predicted_test = clf.predict(dados_teste)
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(resultados_teste, predicted_test))
print("Matriz de confusão:\n",confusion_matrix(resultados_teste, clf.predict(dados_teste)))
print("Classification Report:\n",classification_report(resultados_teste, clf.predict(dados_teste)))
#desenhar o grafo da arvore de decisao
from sklearn.tree import export_graphviz
from six import StringIO
from IPython.display import Image
import pydotplus
feature_cols = ['id','partner','age','age_o','goal','date','go_out','length','met', 'like', 'prob','match']
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,
special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('principalID3holdout.png')
Image(graph.create_png())