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

Functions

 ToolLooCrossVal (V, ClassIdx)
 helper function: leave one out cross validation
 
 crossvalidate_I (data, labels, num_folds=10, k=3)
 
 getClasses_I (labels)
 
 splitData_I (gt_labels, num_folds, classes)
 
 evaluate_I (gt, est, class_indices)
 
 computeConfMat_I (gt, est, class_indices)
 

Function Documentation

◆ computeConfMat_I()

computeConfMat_I ( gt,
est,
class_indices )

Definition at line 127 of file ToolLooCrossVal.py.

127def computeConfMat_I(gt, est, class_indices):
128 conf_mat = np.zeros((len(class_indices), len(class_indices)))
129
130 gt = np.asarray(gt)
131 if gt.ndim == 0:
132 conf_mat[gt - class_indices[0], est - class_indices[0]] += 1
133 else:
134 # assume the class indices to be increasing integers
135 for i, row in enumerate(gt):
136 conf_mat[row - class_indices[0], np.asarray(est)[i] - class_indices[0]] += 1
137
138 return conf_mat.astype(int)
Here is the caller graph for this function:

◆ crossvalidate_I()

crossvalidate_I ( data,
labels,
num_folds = 10,
k = 3 )

Definition at line 27 of file ToolLooCrossVal.py.

27def crossvalidate_I(data, labels, num_folds=10, k=3):
28
29 # init result
30 classes = getClasses_I(labels)
31 fold_accuracies = np.zeros(num_folds)
32 conf_mat = np.zeros((num_folds, len(classes), len(classes)))
33
34 # split data
35 fold_ind = splitData_I(labels, num_folds, classes)
36
37 # do classification for each fold
38 for n in range(num_folds):
39 train_data = np.zeros((data.shape[0], 0))
40 test_data = np.zeros((data.shape[0], 0))
41 train_label = np.zeros(0)
42
43 # split train and testdata
44 for i, train in enumerate(fold_ind):
45 if i == n:
46 test_data = np.hstack((test_data, data[:, fold_ind[n]]))
47 test_label = labels[np.squeeze(fold_ind[i])].astype(int)
48 continue
49 train_data = np.hstack((train_data, data[:, fold_ind[i]]))
50 train_label = np.append(train_label, labels[fold_ind[i]])
51
52 # classify
53 est_label = ToolSimpleKnn(test_data, train_data, train_label, k)
54
55 # evaluate result
56 [fold_accuracies[n], conf_mat[n, :, :]] = evaluate_I(test_label, est_label, classes)
57
58 # compute overall metrics from fold results
59 avg_accuracy = np.mean(fold_accuracies)
60 conf_mat = np.sum(conf_mat, axis=0)
61
62 return avg_accuracy, fold_accuracies, conf_mat
63
64
Here is the call graph for this function:
Here is the caller graph for this function:

◆ evaluate_I()

evaluate_I ( gt,
est,
class_indices )

Definition at line 114 of file ToolLooCrossVal.py.

114def evaluate_I(gt, est, class_indices):
115 # compute confusion matrix
116 conf_mat = computeConfMat_I(gt, est, class_indices)
117
118 gt = np.asarray(gt)
119 if gt.ndim == 0:
120 accuracy = np.trace(conf_mat)
121 else:
122 accuracy = np.trace(conf_mat)/len(gt)
123
124 return accuracy, conf_mat
125
126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getClasses_I()

getClasses_I ( labels)

Definition at line 65 of file ToolLooCrossVal.py.

65def getClasses_I(labels):
66
67 return np.unique(labels)
68
69
Here is the caller graph for this function:

◆ splitData_I()

splitData_I ( gt_labels,
num_folds,
classes )

Definition at line 70 of file ToolLooCrossVal.py.

70def splitData_I(gt_labels, num_folds, classes):
71
72 # check number of observations per class
73 num_obs_class = np.zeros(len(classes))
74 for k, c in enumerate(classes):
75 num_obs_class[k] = int(len(gt_labels[gt_labels == c]))
76 num_obs_class = num_obs_class.astype(int)
77
78 # compute observations per fold stratified
79 avg_obs_fold = np.floor(len(gt_labels)/num_folds).astype(int)
80 num_obs_fold = np.zeros([num_folds, len(classes)]).astype(int)
81 while np.sum(num_obs_class) > np.sum(num_obs_fold) and np.sum(num_obs_fold) <= num_folds * avg_obs_fold:
82 for k in range(len(classes)):
83 for f in range(num_folds):
84 if np.sum(num_obs_fold[f, :]) >= avg_obs_fold:
85 continue
86 if num_obs_class[k]-np.sum(num_obs_fold[:, k]) > 0:
87 num_obs_fold[f, k] += 1
88 else:
89 break
90
91 # take care of any unassigned stragglers
92 while np.sum(num_obs_class) > np.sum(num_obs_fold):
93 for k in range(len(classes)):
94 if num_obs_class[k]-np.sum(num_obs_fold[:, k]) <= 0:
95 continue
96 for f in range(num_folds):
97 num_obs_fold[f, k] += 1
98
99 # split actual data into folds
100 last = np.zeros(len(classes)).astype(int)
101 data_ind = [[] for _ in range(num_folds)]
102 for f in range(num_folds):
103 for k, c in enumerate(classes):
104 num = num_obs_fold[f, k]
105 data_ind[f] = data_ind[f] + np.argwhere(gt_labels == c)[np.arange(last[k], last[k]+num)].astype(int).tolist()
106 last[k] += num
107
108 for f in range(num_folds):
109 data_ind[f] = np.ravel(data_ind[f]).astype(int)
110
111 return data_ind
112
113
Here is the caller graph for this function:

◆ ToolLooCrossVal()

ToolLooCrossVal ( V,
ClassIdx )

helper function: leave one out cross validation

Parameters
Vfeature matrix with all observations (dimension iNumFeatures x iNumObservations)
ClassIdxclass labels (length: iNumObservations)
Returns
avg_accuracy: overall accuracy
fold_accuracies: accuracies per fold
conf_mat: confusion matrix

Definition at line 16 of file ToolLooCrossVal.py.

16def ToolLooCrossVal(V, ClassIdx):
17
18 if V.ndim == 1:
19 V = V[None, :]
20
21 iNumObservations = V.shape[1]
22 kNearestNeighbor = 3
23
24 return crossvalidate_I(V, ClassIdx, iNumObservations, kNearestNeighbor)
25
26
Here is the call graph for this function: