-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposture_classifier.py
94 lines (69 loc) · 2.78 KB
/
posture_classifier.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
'''
Based on a novel by Mihai Trascau, famous author and scholar
'''
import matplotlib.pyplot as pp
import numpy as np
from sklearn import tree
from sklearn import cross_validation
class Example:
'''Stores the name of a posture and a list of parameters set that exemplify that posture'''
def __init__(self, posture, feature_vectors):
print 'Building an example for', posture, 'with', len(feature_vectors), 'labels'
''' - posture is the name of the posture
- vectors is a list of feature vectors'''
self.posture = posture
self.feature_vectors = feature_vectors
def input_data_of_examples(examples):
'''given a list of Example objects returns two X,y for learning algorithms'''
X = []
y = []
for e in examples:
for features in e.feature_vectors:
X.append(features)
y.append(e.posture)
return X,y
def filter_by_kl(feature_vectors):
''' filters the examples using divergence'''
def normalize(vector):
m = min(vector) - 10e-18
vector = [v - m for v in vector]
s = sum(vector)
return [v / s for v in vector]
def distance(index):
p = normalize(feature_vectors[i])
q = normalize(feature_vectors[i+1])
#p = feature_vectors[i]
#q = feature_vectors[i+1]
return kl(p, q)
distances = [distance(i) for i in range(len(feature_vectors) -1)]
filtered = []
print 'Got max:', max(distances), ', min:', min(distances), ', avg:', np.mean(distances), ' std:', np.std(distances)
print 'Filtered', len(filtered), 'examples from initial', len(feature_vectors), 'examples'
pp.plot(distances)
pp.show()
return [range(22)]
def kl(p, q):
"""Kullback-Leibler divergence D(P || Q) for discrete distributions
Parameters
----------
p, q : array-like, dtype=float, shape=n
Discrete probability distributions.
"""
idx = [i for i in range(len(p)) if p[i] != 0 and q[i] != 0]
p = [p[i] for i in idx]
q = [q[i] for i in idx]
p = np.asarray(p, dtype=np.float)
q = np.asarray(q, dtype=np.float)
return np.sum(np.where(p != 0, p * np.log(p / q), 0))
def tree_classifier(examples):
''' Receives a list of Example objects and returns a tree.DecisionTreeClassifier'''
X, y = input_data_of_examples(examples)
print 'Having', len(X), 'example for', len(examples), 'classes'
X_learn, X_test, y_learn, y_test = cross_validation.train_test_split(X, y)
print 'Learning from', len(X_learn), 'examples'
clf = tree.DecisionTreeClassifier()
clf.fit(X_learn, y_learn)
print 'Validating with', len(X_test), 'examples'
score = clf.score(X_test, y_test)
print 'Got score', score
return clf