This Question pertains to Matrix Factorization and the full question is given below. Provide for k-means clustering of the Olivetti dataset the following visualizations:

  • A scatter plot of the r = 2-dimensional representation of the faces in the latent space, together with the faces which represent the two dimensions/features.
  • A visualization of the faces which define the features of the latent space for r = 5.
  • The reconstruction of the faces with indices i ∈ {0, 10, 20} when using a rank of r ∈ {5, 25, 50, 100}

To load the Olivetti Dataset run the following code:

from sklearn.datasets import fetch_olivetti_faces
faces = fetch_olivetti_faces()

And print(faces.DESCR) provides a description of the data set.

When I try to K-Means clustering on the model I receive

**TypeError: float() argument must be a string or a number, not 'Bunch'**

Thank you for your time and consideration


1 Answer 1


I have a couple of observations about this:

  1. Before you run clustering, I'd run some dimensionality reduction model. Pixel data are too noisy to be fed as they are into a model. With dimensionality reduction you get both noise reduction and lower computational costs.

  2. Images usually come as 2D or 3D arrays (depending whether they are in grayscale or coloured). On the other side, k-Means and other sklearn models require your whole dataset as a 2D array. You should process each image and turn it into a 1D array that can be fed as a single observation.

Could you share your clustering code, just to make sure?

  • $\begingroup$ from sklearn.cluster import KMeans from sklearn import datasets from sklearn.datasets import fetch_olivetti_faces faces = fetch_olivetti_faces() kmeans = KMeans(n_clusters = 50) kmeans.fit(faces) $\endgroup$ Apr 25, 2020 at 11:03
  • $\begingroup$ Yeah, I think it gives you error because you are feeding 2D observations (pixel images). You should collapse each observation to a 1D vector, and that 1D vector should be a row of a new processed dataset. In that way it should work. $\endgroup$
    – Leevo
    Apr 25, 2020 at 11:27

Your Answer

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

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