-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.py
164 lines (128 loc) · 4.89 KB
/
class.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Renan Teruo Carneiro
# Numero USP 6514157
# MAC 0460 - Aprendizagem Computacional
# Tarefa 2
from scipy.stats import *
import sys
def argmax(array):
return array.index(max(array))
dists = ["normal", "exponencial", "uniforme"]
########################################################
# Classe Data: Armazena dados do conjunto de treinamento
########################################################
class Data:
def __init__(self):
# Cada posicao de raw data guarda um vetor com as amostras de cada classe
self.raw_data = [[],[],[]]
# Eventualmente, cada posicao de pdfs guarda tres fdps, uma para cada distribuicao, indexada pelo nome da mesma
self.pdfs = [{},{},{}]
# Cada posicao de mean guarda a media de cada classe; o mesmo vale para var, maximum e minimum
self.mean = [[], [], []]
self.var = [[], [], []]
self.maximum = [[], [], []]
self.minimum = [[], [], []]
# Insere um novo dado na classe especificada
def add_data(self, data, data_class):
self.raw_data[data_class-1] += [data]
# Gera media, variancia, maximo, minimo e fdps
def calculate_extra_data(self):
for i in xrange(0,3):
self.mean[i] = tmean(self.raw_data[i])
self.var[i] = tvar(self.raw_data[i])
self.maximum[i] = max(self.raw_data[i])
self.minimum[i] = min(self.raw_data[i])
self.generate_pdfs(i)
# Gera as fdps
def generate_pdfs(self, data_class):
self.pdfs[data_class]["normal"] = norm(loc = self.mean[data_class], scale=self.var[data_class])
self.pdfs[data_class]["uniforme"] = uniform(loc = self.minimum[data_class], scale = self.maximum[data_class] - self.minimum[data_class])
self.pdfs[data_class]["exponencial"] = expon(scale = self.mean[data_class])
#######################################################
# Classe Classifier: Classifica amostras
#######################################################
class Classifier:
def __init__(self, data, dist1, dist2, dist3):
self.pdfs = [{},{},{}]
self.successes = 0
self.pdfs[0]["dist"] = data.pdfs[0][dist1]
self.pdfs[0]["name"] = dist1
self.pdfs[1]["dist"] = data.pdfs[1][dist2]
self.pdfs[1]["name"] = dist2
self.pdfs[2]["dist"] = data.pdfs[2][dist3]
self.pdfs[2]["name"] = dist3
self.confusion_table = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# Classifica a amostra dada, escolhendo o maior valor P(x|w), e verifica se acertou
def choose_class(self, x, data_class):
results = []
data_class -= 1
for i in self.pdfs:
results += [i["dist"].pdf(x)]
result = argmax(results)
if result == data_class:
self.successes += 1
self.add_to_confusion_table(data_class, result)
return result
# Retorna o numero de acertos
def get_results(self):
return self.successes
# Imprime as distribuicoes usadas nesse classificador
def print_self(self):
print (self.pdfs[0]["name"], self.pdfs[1]["name"], self.pdfs[2]["name"])
# Adiciona, na tabela de confusao, o valor esperado (i) e o valor devolvido(j)
def add_to_confusion_table(self, i, j):
self.confusion_table[i][j] += 1
# Imprime a tabela de confusao
def print_confusion_table(self):
print "\t\t\t\tClassificado"
print "\t\t\t\tClasse 1\tClasse 2\tClasse 3"
print "\t\tClasse 1\t",self.confusion_table[0][0],"\t\t",self.confusion_table[0][1],"\t\t", self.confusion_table[0][2]
print "Esperado\tClasse 2\t",self.confusion_table[1][0],"\t\t",self.confusion_table[1][1],"\t\t", self.confusion_table[1][2]
print "\t\tClasse 3\t",self.confusion_table[2][0],"\t\t",self.confusion_table[2][1],"\t\t", self.confusion_table[2][2]
#########################################################
if len(sys.argv) < 3:
print "Especifique o conjunto de treinamento e o conjunto de testes!"
exit()
training_file = open(sys.argv[1])
training_data = training_file.readlines()
training_file.close()
data = Data()
for i in training_data:
vals = i.split(' ')
val = float(vals[0])
data_class = int(vals[1])
data.add_data(val, data_class)
data.calculate_extra_data()
classifiers = []
for i in dists:
for j in dists:
for k in dists:
classifiers += [Classifier(data, i, j, k)]
test_file = open(sys.argv[2])
test_data = test_file.readlines()
test_file.close()
total_cases = 0
for i in test_data:
vals = i.split(' ')
val = float(vals[0])
data_class = int(vals[1])
total_cases += 1
for j in classifiers:
result = j.choose_class(val, data_class)
results = []
for i in classifiers:
i.print_self()
results += [i.get_results()]
print "Erros: ", i.get_results(), "\n"
maximum = max(results)
maxargs = []
for i in xrange(0,27):
if results[i] == maximum:
maxargs += [i]
print "Modelos escolhidos: \n"
for i in maxargs:
classifiers[i].print_self()
print "Erros: ", total_cases - results[i]
classifiers[i].print_confusion_table()
print "\n"