I have data (a numpy array p) with shape (n,68,2). I am trying to apply k-means clustering to this data using Scikit-learn. I need to find k clusters from this data and the final output data after clustering should have dimensions of (k,68,2).
When I provide p to the Kmeans function like
kmeans = KMeans(n_clusters=no_of_clusters, random_state=0).fit(p1)
it gives an Error
ValueError: Found array with dim 3. Estimator expected <= 2.
To get around this problem I had to separate the x and y coordinates to get 2 arrays of dimensions (n,68) and apply kmeans separately to them and combine the results later.
kmeans_y = KMeans(n_clusters=no_of_clusters, random_state=0).fit(p[:,:,1])
kmeans_x = KMeans(n_clusters=no_of_clusters, random_state=0).fit(p[:,:,0])
I would like to apply kmeans directly to the whole 2D coordinates without separating x and y and applying them separately. But I do need output data of dimensions (k,68,2) and not (k,2) which I got when i tried reshaping the array.
(n,m)
, wheren
is the number of samples andm
is the number of features.(n, 136)
. The default distance is euclidean, it's worth thinking about if this makes sense in terms of your data, if not then you can write a custom distance metric.