-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (84 loc) · 3.77 KB
/
main.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
import numpy
from analysis import Settings, Tester
""" HELPERS """
def analyse(property, learning_samples=25, components=32, voices=25):
Settings.NUMBER_OF_VOICES = voices
Settings.SAMPLES_TO_LEARN = learning_samples
Settings.DECOMPOSITION_COMPONENTS = components
predictions = Tester().perform_analysis()
return predictions[property]
def save_result_to_file(filename, column_names=[], columns=[]):
def write_line(file, columns):
line = "\t".join([str(cell) for cell in columns]) + "\n"
file.write(line)
file_path = "{0}/{1}".format(Settings.ANALYSIS_PATH, filename)
with open(file_path, "w+") as file:
write_line(file, column_names)
zipped = zip(*columns)
for column in zipped:
write_line(file, column)
""" FINDING THE BEST PAIRS (DECOMPOSITION, CLASSIFIER) OF ALGORITHMS """
def test_algorithms(property, number_of_learning_samples):
decompositors = Settings.AVAILABLE_DECOMPOSITION_ALGORITHMS
classifiers = Settings.AVAILABLE_CLASSIFIER_ALGORITHMS
N = number_of_learning_samples
column_names = ["N = " + str(N)] + [str(algorithm.__name__) for algorithm in classifiers]
columns = [[str(algorithm.__name__) for algorithm in decompositors]]
for classifier in classifiers:
Settings.CLASSIFIER_ALGORITHM = classifier
column = []
for decompositor in decompositors:
Settings.DECOMPOSITION_ALGORITHM = decompositor
column += [analyse(property, learning_samples=N)]
columns += [column]
filename = "algorithms_SAMPLES_{0}_PROPERTY_{1}.txt".format(N, property)
save_result_to_file(filename, column_names, columns)
def test_algorithms_range(property):
for N in [1, 5, 10, 15, 25, 50]:
test_algorithms(property, N)
""" NUMBER OF LEARNING SAMPLES """
from sklearn.decomposition import PCA, FactorAnalysis, FastICA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
def test_learning_samples(algorithm_pair, property):
column_names = ["N"] + ["{0}_{1}".format(pair[0].__name__, pair[1].__name__) for pair in algorithm_pair]
columns = [list(range(1, 65))]
for pair in algorithm_pair:
Settings.DECOMPOSITION_ALGORITHM = pair[0]
Settings.CLASSIFIER_ALGORITHM = pair[1]
columns += [[analyse(property, learning_samples=N) for N in range(1, 65)]]
filename = "learning_samples_PROPERTY_{0}.txt".format(property)
save_result_to_file(filename, column_names, columns)
algorithm_pairs = [
(FastICA, GaussianNB),
(FactorAnalysis, GaussianNB),
(PCA, GaussianNB),
(FastICA, KNeighborsClassifier),
(FactorAnalysis, KNeighborsClassifier),
]
""" NUMBER OF COMPONENTS """
def test_components(algorithm_pair, property):
column_names = ["C"] + ["{0}_{1}".format(pair[0].__name__, pair[1].__name__) for pair in algorithm_pair]
columns = [list(range(1, 65))]
for pair in algorithm_pair:
Settings.DECOMPOSITION_ALGORITHM = pair[0]
Settings.CLASSIFIER_ALGORITHM = pair[1]
columns += [[analyse(property, components=N) for N in range(1, 65)]]
filename = "components_PROPERTY_{0}.txt".format(property)
save_result_to_file(filename, column_names, columns)
""" NUMBER OF VOICES """
def test_voices(algorithm_pair, property):
column_names = ["V"] + ["{0}_{1}".format(pair[0].__name__, pair[1].__name__) for pair in algorithm_pair]
columns = [list(range(1, 65))]
for pair in algorithm_pair:
Settings.DECOMPOSITION_ALGORITHM = pair[0]
Settings.CLASSIFIER_ALGORITHM = pair[1]
columns += [[analyse(property, voices=N) for N in range(1, 26)]]
filename = "voices_PROPERTY_{0}.txt".format(property)
save_result_to_file(filename, column_names, columns)
""" ANALYSIS """
#test_algorithms_range("correct_prediction")
#test_learning_samples(algorithm_pairs, "correct_prediction")
#test_learning_samples(algorithm_pairs, "probability")
#test_components(algorithm_pairs, "correct_prediction")
#test_voices(algorithm_pairs, "correct_prediction")