-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper.py
103 lines (69 loc) · 2.89 KB
/
paper.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
#!/usr/bin/env python
# encoding: utf-8
# Andre Anjos <[email protected]>
# Wed 17 Jun 18:31:35 CEST 2015
'''User script to conduct the first hypothesis in the course'''
import logging
import itertools
import numpy
numpy.seterr(divide='ignore')
import database
import preprocessor
import algorithm
import analysis
def test_one(protocol, variables):
"""Runs one single test, returns the CER on the test set"""
# 1. get the data from our preset API for the database
train = database.get(protocol, 'train', database.CLASSES, variables)
# 2. preprocess the data using our module preprocessor
norm = preprocessor.estimate_norm(numpy.vstack(train))
train_normed = preprocessor.normalize(train, norm)
# 3. trains our logistic regression system
trainer = algorithm.MultiClassTrainer()
machine = trainer.train(train_normed)
# 4. applies the machine to predict on the 'unseen' test data
test = database.get(protocol, 'test', database.CLASSES, variables)
test_normed = preprocessor.normalize(test, norm)
test_predictions = machine.predict(numpy.vstack(test_normed))
test_labels = algorithm.make_labels(test).astype(int)
return analysis.CER(test_predictions, test_labels)
def test_impact_of_variables_single(tabnum):
"""Builds the first table of my report"""
for n, p in enumerate(database.PROTOCOLS):
print "\nTable %d: Single variables for Protocol `%s`:" % \
(n+tabnum, p)
print 60*"-"
for k in database.VARIABLES:
result = test_one(p, [k])
print ("%-15s" % k), "| %d%%" % (100 * result,)
def test_impact_of_variables_2by2(tabnum):
"""Builds the first table of my report"""
for n, p in enumerate(database.PROTOCOLS):
print "\nTable %d: Variable combinations, 2x2 for Protocol `%s`:" % \
(n+tabnum, p)
print 60*"-"
for k in itertools.combinations(database.VARIABLES, 2):
result = test_one(p, k)
print ("%-30s" % ' + '.join(k)), "| %d%%" % (100 * result,)
def test_impact_of_variables_3by3(tabnum):
"""Builds the first table of my report"""
for n, p in enumerate(database.PROTOCOLS):
print "\nTable %d: Variable combinations, 3x3 for Protocol `%s`:" % \
(n+tabnum, p)
print 60*"-"
for k in itertools.combinations(database.VARIABLES, 3):
result = test_one(p, k)
print ("%-45s" % ' + '.join(k)), "| %d%%" % (100 * result,)
def test_impact_of_variables_all(tabnum):
"""Builds the first table of my report"""
for k, p in enumerate(database.PROTOCOLS):
print "\nTable %d: All variables for Protocol `%s`:" % (k+tabnum, p)
print 60*"-"
result = test_one(p, database.VARIABLES)
print ("%-45s" % ' + '.join(database.VARIABLES)), "| %d%%" % (100 * result,)
if __name__ == '__main__':
print "Main script for Logistic Regression on Iris Flowers."
test_impact_of_variables_single(1)
test_impact_of_variables_2by2(3)
test_impact_of_variables_3by3(5)
test_impact_of_variables_all(7)