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

Classes

class  CKMeansState
 

Functions

 ToolSimpleKmeans (V, K, numMaxIter=1000, prevState=None)
 helper function: kmeans clustering
 
 assignClusterLabels_I (V, state)
 
 computeClusterMeans_I (V, clusterIdx, K)
 
 reinitState_I (state, clusterIdx, K, range_V)
 

Function Documentation

◆ assignClusterLabels_I()

assignClusterLabels_I ( V,
state )

Definition at line 51 of file ToolSimpleKmeans.py.

51def assignClusterLabels_I(V, state):
52
53 # number of clusters
54 K = state.mu.shape[1]
55
56 D = np.zeros([K, V.shape[1]])
57
58 for k in range(K):
59 D[k, :] = np.sqrt(np.sum((np.tile(state.mu[:, [k]], (1, V.shape[1])) - V)**2, axis=0, keepdims=True))
60
61 clusterIdx = np.argmin(D, axis=0).astype(int)
62
63 return clusterIdx
64
65
Here is the caller graph for this function:

◆ computeClusterMeans_I()

computeClusterMeans_I ( V,
clusterIdx,
K )

Definition at line 66 of file ToolSimpleKmeans.py.

66def computeClusterMeans_I(V, clusterIdx, K):
67
68 # init
69 mu = np.zeros([V.shape[0], K])
70
71 for k in range(K):
72 if np.count_nonzero(clusterIdx == k) != 0:
73 mu[:, k] = np.sum(V[:, clusterIdx == k], axis=1) / np.count_nonzero(clusterIdx == k)
74
75 return CKMeansState(mu)
76
77
Here is the caller graph for this function:

◆ reinitState_I()

reinitState_I ( state,
clusterIdx,
K,
range_V )

Definition at line 78 of file ToolSimpleKmeans.py.

78def reinitState_I(state, clusterIdx, K, range_V):
79
80 for k in range(K):
81 if np.count_nonzero(clusterIdx == k) == 0:
82 state.mu[:, k] = np.random.rand(state.mu.shape[0], 1) * (range_V[:, 1]-range_V[:, 0]) + range_V[:, 0]
83
84 return state
Here is the caller graph for this function:

◆ ToolSimpleKmeans()

ToolSimpleKmeans ( V,
K,
numMaxIter = 1000,
prevState = None )

helper function: kmeans clustering

Parameters
Vfeatures for all train observations (dimension iNumFeatures x iNumObservations)
Knumber of clusters
numMaxItermaximum number of iterations (stop if not converged before, default: 1000)
prevStateinternal state that can be stored to continue clustering later
Returns
clusterIdx: cluster index of each observation (iNumObservations)
state: result containing internal state (if needed)

Definition at line 20 of file ToolSimpleKmeans.py.

20def ToolSimpleKmeans(V, K, numMaxIter=1000, prevState=None):
21
22 # init
23 if prevState is None:
24 state = CKMeansState(V[:, np.round(np.random.rand(K) * (V.shape[1]-1)).astype(int)])
25 else:
26 state = CKMeansState(prevState.mu.copy())
27 range_V = np.array([np.min(V, axis=1), np.max(V, axis=1)])
28
29 # assign observations to clusters
30 clusterIdx = assignClusterLabels_I(V, state)
31
32 for j in range(numMaxIter):
33 prevState = CKMeansState(state.mu.copy())
34
35 # update clusters
36 state = computeClusterMeans_I(V, clusterIdx, K)
37
38 # reinit empty clusters
39 state = reinitState_I(state, clusterIdx, K, range_V)
40
41 # assign observations to clusters
42 clusterIdx = assignClusterLabels_I(V, state)
43
44 # if converged, break
45 if np.max(np.sum(np.abs(state.mu-prevState.mu))) == 0:
46 break
47
48 return clusterIdx, state.mu
49
50
Here is the call graph for this function: