-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.py
132 lines (89 loc) · 3.38 KB
/
reader.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
#!/usr/bin/python
import aifc
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn.cross_validation import KFold
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.covariance import EllipticEnvelope
from sklearn import svm
import numpy as np
import preprocess
def drawFrames(frames, n=7):
ctr = 0
f, axarr = plt.subplots(n, n)
for ii in range(0,n):
for jj in range(0,n):
axarr[ii, jj].plot( frames[ctr] )
ctr += 1
plt.show()
def transform( features, labels ):
# for ff, ll in zip(features, labels):
# print ll, ff
# for rr in range(0, len(features) ):
# features[rr] = scaler.fit_transform( features[rr] )
print "transforming features via pca"
pca = PCA(n_components = 30)
features = pca.fit_transform( features )
envelope = EllipticEnvelope()
envelope.fit( features )
print envelope.predict( features )
scaler = MinMaxScaler()
features = scaler.fit_transform( features )
return features, labels
def classify( features, labels ):
clf = svm.SVC(kernel="linear")#DecisionTreeClassifier()
kf = KFold(len(labels), n_folds=4, shuffle=True)
for train_index, test_index in kf:
features_train, features_test = features[train_index], features[test_index]
labels_train, labels_test = labels[train_index], labels[test_index]
clf.fit( features_train, labels_train )
pred = clf.predict( features_test )
print "training labels: ", labels_train
print "predictions: ", pred
print "test labels: ", labels_test
acc = accuracy_score( pred, labels_test )
print acc
print "fitting classifier"
print "classification done"
def main():
#raw = aifc.open("/Users/katiemalone/Desktop/data_podcast/fisherfaces/fisherfaces.aif", "r")
#edited = aifc.open("/Users/katiemalone/Desktop/data_podcast/fisherfaces/fisherfaces_edited.aif", "r")
raw = aifc.open("fisherfaces.aif", "r")
edited = aifc.open("fisherfaces_edited.aif", "r")
print raw.getparams()
s_markers, b_markers = preprocess.signalBackgroundMarkers( raw )
window_length = preprocess.findMaxWindow(s_markers)
s_frames = preprocess.normedFrames( raw, window_length, start_points=s_markers)
# drawFrames( s_frames )
b_frames = preprocess.normedFrames( edited, window_length, n_samples=200 )
# drawFrames( b_frames )
features = []
labels = []
for ii in range(0, len(s_frames)):
features.append(s_frames[ii])
labels.append(1)
for jj in range(0, len(b_frames)):
features.append(b_frames[jj])
labels.append(0)
print "data acquisition done"
features = np.asarray( features )
labels = np.asarray( labels )
features, labels = transform( features, labels )
# f, axarr = plt.subplots(2, 2)
# ctr = 0
for ff, ll in zip(features, labels):
if ll==0:
#axarr[0, 0].scatter( ff[0], ff[1], color="red")
plt.scatter( ff[0], ff[1], color="red")
if ll==1:
plt.scatter( ff[0], ff[1], color="blue")
# for ii in range(0,n):
# for jj in range(0,n):
# axarr[ii, jj].plot( frames[ctr] )
# ctr += 1
plt.show()
classify( features, labels )
if __name__=="__main__":
main()