I have a python numpy array of size (800,800,4) which is my hyperspectral camera data.

When I try standard GMM methods from scikit-learn I get an error saying that the expected dimension of the data should be less than or equal to 2.

Current method:



Are there any python packages or functions available to process higher dimensional data using gmm?


If I understand your type of data correctly, what you have is essentially an image with $800\times800 =640000$ pixels and each pixel has a vector of 4 values.

If this stands, I suppose you could then transform your data to a $640000\times4$ matrix, so as to conform with scikit-learn's data representation schema of inputting matrices of shape ($\#samples\times\#features$) and then you could use the GMM class implemented by the package.

And you could transform the data back if needed.

P.S.: On a second thought, I don't know if the spatial distribution of pixels plays a role on the generation of the GMM components. Maybe try it out and let us know?

Ok, after you have trained your model you can ask for it to predict the mixture model from which each pixel in your dataset has been generated.

That is if you run again: $clusters=gmm.predict(newdata)$ you will get an array with values $0-2$, denoting the cluster that each pixel belongs to. You can, then map these values in the image at hand, applying a mask of different color for each pixel in order to see if your model correctly identifies the three classes.

Here is an example of what I mean:

from scipy.misc import imread, imshow
from sklearn.datasets import load_sample_image

X = load_sample_image('china.jpg')
old_shape = X.shape
X = X.reshape(-1,3)
gmm = mixture.GMM(covariance_type='full', n_components=5)
clusters = gmm.predict(X)
clusters = clusters.reshape(old_shape[0], old_shape[1])
  • $\begingroup$ So I tried this and it worked! I have a picture of a pond and I am trying to segment the pond rocks from the rocks around the edge from the grass in the picture. This is the output of my code: prntscr.com/ff2lii How can I now take these mean values and use them on the original data to cluster my pixels? $\endgroup$ Jun 2 '17 at 13:42
  • $\begingroup$ First of all, I think you should update your question to properly express what you desire to do. $\endgroup$
    – Bogas
    Jun 2 '17 at 14:17
  • $\begingroup$ You are the man! This worked perfectly! Thank you so much $\endgroup$ Jun 2 '17 at 15:03
  • $\begingroup$ I have 1000 images of size (60, 60, 3), so the shape of the array is (1000, 60, 60, 3). How should I reshape this in order for GMM to work? $\endgroup$ Feb 10 '18 at 21:42
  • 1
    $\begingroup$ @YashKatariya Following the same line of thought you would need to transform your data in a $\#samples\times\#features$ matrix. If you want to find the class of each image, then you would transform it in a $1000 \times (60*60*3=10800) $ matrix (each image represented through its 10800 pixels, concatenated as features). $\endgroup$
    – Bogas
    Feb 12 '18 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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