pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
Functions
pyACA.ToolSimpleKnn Namespace Reference

Functions

 ToolSimpleKnn (v_test, V_train, ClassIdx_train, K=1)
 helper function: k nearest neighbor classifier
 
 getClasses_I (labels)
 
 computePairwiseDistance_I (test_data, train_data)
 
 computeEucDist_I (vector, matrix)
 
 inferClass_I (closest_labels, classes, closeness)
 

Function Documentation

◆ computeEucDist_I()

computeEucDist_I ( vector,
matrix )

Definition at line 61 of file ToolSimpleKnn.py.

61def computeEucDist_I(vector, matrix):
62
63 d = np.sum((vector - matrix)**2, axis=1, keepdims=True)
64 return np.sqrt(d.T)
65
66
Here is the caller graph for this function:

◆ computePairwiseDistance_I()

computePairwiseDistance_I ( test_data,
train_data )

Definition at line 52 of file ToolSimpleKnn.py.

52def computePairwiseDistance_I(test_data, train_data):
53
54 # you may also use sp.spatial.distance.cdist
55 d = np.zeros((test_data.shape[1], train_data.shape[1]))
56 for i, v in enumerate(test_data.T):
57 d[i, :] = computeEucDist_I(v, train_data.T)
58 return d
59
60
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getClasses_I()

getClasses_I ( labels)

Definition at line 47 of file ToolSimpleKnn.py.

47def getClasses_I(labels):
48
49 return np.unique(labels)
50
51
Here is the caller graph for this function:

◆ inferClass_I()

inferClass_I ( closest_labels,
classes,
closeness )

Definition at line 67 of file ToolSimpleKnn.py.

67def inferClass_I(closest_labels, classes, closeness):
68
69 # first, try to do a simple majority vote
70 # res_class = sp.stats.mode(class_labels, axis = 0)
71 hist = np.histogram(closest_labels, bins=len(classes), range=(min(classes), max(classes)))
72
73 # check if we have a clear majority
74 s = np.flip(np.sort(hist[0]))
75 # fallback: if not, do a histogram weighted with the closeness (inverted distance)
76 if s[0] == s[1]:
77 hist = np.histogram(closest_labels, bins=len(classes), range=(min(classes), max(classes)), weights=closeness)
78
79 return classes[np.argmax(hist[0])]
Here is the caller graph for this function:

◆ ToolSimpleKnn()

ToolSimpleKnn ( v_test,
V_train,
ClassIdx_train,
K = 1 )

helper function: k nearest neighbor classifier

Parameters
v_testtest feature vector
V_trainfeatures for all train observations (dimension iNumFeatures x iNumObservations)
ClassIdx_trainclass labels (length observations)
Knumber of neighbors taken into account (default = 3)
Returns
est_class: index of estimated class

Definition at line 14 of file ToolSimpleKnn.py.

14def ToolSimpleKnn(v_test, V_train, ClassIdx_train, K=1):
15
16 # sanity checks
17 if v_test.shape[0] != V_train.shape[0]:
18 return -1
19
20 # get dimensions
21 # num_features = TestFeatureVector.shape[0]
22 classes = getClasses_I(ClassIdx_train)
23
24 # init result
25 est_class = -1*np.ones(v_test.shape[1])
26
27 # compute pairwise distances between all test data and all train data points
28 d = computePairwiseDistance_I(v_test, V_train)
29
30 # sort distances
31 ind = np.argsort(d, axis=1).astype(int)
32 ind = ind[:, range(K)]
33
34 # extension for distance based weighting: convert distance to closeness (easier later with the histogram)
35 ma = np.amax(d)
36 if ma <= 0:
37 ma = 1
38 d = 1-d/ma
39
40 # infer which class
41 for obs, index in enumerate(ind):
42 est_class[obs] = inferClass_I(ClassIdx_train[index], classes, d[obs, index])
43
44 return np.squeeze(est_class.astype(int))
45
46
Here is the call graph for this function: