2

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.

6
  • 1
    Please provide an excerpt of your "3d" dataset. If "3d" means three features per sample, your dataset can be represented by a matrix (n,m), where n is the number of samples and m is the number of features.
    – sentence
    Apr 15, 2019 at 14:01
  • one question, what is n? As in, what does it signify? Moreover, is 68 the number of data points? I get 2 is the number of dimensions each point has
    – nickyfot
    Apr 15, 2019 at 14:01
  • 1
    You can reshape your data to be (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.
    – piman314
    Apr 15, 2019 at 14:03
  • @sentence The data is like (pastebin.com/HfAQJzKV) Apr 15, 2019 at 14:11
  • @nickthefreak This is actually the data of position of landmark points for each image in a video. So each image has 68 points, with each point having 2 coordinates. n is the number of images in the video. I am trying to find k key images from these n images through clustering. Apr 15, 2019 at 14:14

1 Answer 1

4

sklearn KMeans fit() method expects X to have a 2d shape as shown in documentation here:

X : {array-like, sparse matrix}, shape = [n_samples, n_features]

This means that you will have to reshape your np array to use the library. The new shape depends on what each dimension actually is (apologies, not perfectly clear from your question), but assuming n is the number of samples and 68,2 is the shape of the data points you can transform x in the following way:

x = x.reshape(n,68*2)

As correctly pointed out in the comments, you can also write your own distance method that would handle 3-d data, but it might be an overkill depending on the data needs.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.