-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.py
101 lines (86 loc) · 3.59 KB
/
Util.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
# -*- coding: cp1252 -*-
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import math
import csv
import os
class Util:
def getAccuracy(self, testSet, predictions): # APENAS 1-NN
self.acertos = 0
for i in range (len(testSet)):
if (testSet[i, -1] == predictions[i]):
self.acertos = self.acertos + 1
return (self.acertos / float(len(testSet))) * 100.0
def getRealAccuracy(self, realClasses, predictions):
self.acertos = 0
for i in range(len(realClasses)):
if realClasses[i] == predictions[i]:
self.acertos += 1
return (self.acertos / float(len(realClasses))) * 100.0
def write_csv(self, f, folder):
c = csv.writer(open(f, "wb"))
SEPARATOR = ";"
BASE_PATH = folder
label = 0
for dirname, dirnames, filenames in os.walk(BASE_PATH):
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
abs_path = "%s/%s" % (subject_path, filename)
c.writerow(["%s%s%d" % (abs_path, SEPARATOR, label)])
label = label + 1
def write_results(self, filename, results):
with open("results - 100 holdouts//%s.txt" %(filename), "w") as f:
for i in results:
f.write("%s\n" %i)
def plot2D(self, File, x, knn, svc, gnb, x_ini, x_end, y_ini, y_end):
fig, ax = plt.subplots()
ax.plot(x, knn, 'bo-', label="1-NN")
ax.plot(x, svc, 'rs-', label="Support Vector Classification")
ax.plot(x, gnb, 'g^-', label="Gaussian Naive Bayes")
legend = ax.legend(loc=4, shadow=True)
plt.xlabel("Theta (rad)")
plt.title("Base: %s" % File)
plt.ylabel("Accuracy Rate (%)")
plt.xlim(x_ini, x_end)
plt.ylim(y_ini, y_end)
plt.grid()
plt.show()
def plot3D(self, File, x, y, rfc, knn, gnb, svc, scale):
plt.figure()
plt.subplot(1,4,1)
z = np.array(rfc) / 100.0
normi = mpl.colors.Normalize(vmin=np.amin(z), vmax=np.amax(z))
plt.title("%s - RFC Accuracy" % File)
plt.xlabel("Theta1")
plt.ylabel("Theta2")
plt.contourf(x,y,z.T, np.linspace(np.amin(z), np.amax(z), 300), cmap=scale, norm=normi, extend="both")
plt.grid()
plt.colorbar(format="%.2f")
plt.subplot(1,4,2)
z = np.array(knn) / 100.0
normi = mpl.colors.Normalize(vmin=np.amin(z), vmax=np.amax(z))
plt.title("%s - KNN Accuracy" % File)
plt.xlabel("Theta1")
plt.ylabel("Theta2")
plt.contourf(x,y,z.T, np.linspace(np.amin(z), np.amax(z), 300), cmap=scale, norm=normi, extend="both")
plt.grid()
plt.colorbar(format="%.2f")
plt.subplot(1,4,3)
z = np.array(gnb) / 100.0
normi = mpl.colors.Normalize(vmin=np.amin(z), vmax=np.amax(z))
plt.title("%s - GNB Accuracy" % File)
plt.xlabel("Theta1")
plt.contourf(x,y,z.T, np.linspace(np.amin(z), np.amax(z), 300), cmap=scale, norm=normi, extend="both")
plt.grid()
plt.colorbar(format="%.2f")
plt.subplot(1,4,4)
z = np.array(svc) / 100.0
normi = mpl.colors.Normalize(vmin=np.amin(z), vmax=np.amax(z))
plt.title("%s - SVC Accuracy" % File)
plt.contourf(x,y,z.T, np.linspace(np.amin(z), np.amax(z), 300), cmap=scale, norm=normi, extend="both")
plt.xlabel("Theta1")
plt.grid()
plt.colorbar(format="%.2f")
plt.show()