Skip to content

GMM with a population : array shape issue #4509

Closed

Description

@natoromano

I'm using GMM on a singe population (list), with version 0.16.

I get the following error, after calling the score_sample function:

'The shape of X  is not compatible with self'

When digging a little bit, I found that what used to be, in the previous versions:

    X = np.asarray(X)
    if X.ndim == 1:
        X = X[:, np.newaxis]
    if X.size == 0:
        return np.array([]), np.empty((0, self.n_components))
    if X.shape[1] != self.means_.shape[1]:
        raise ValueError('The shape of X  is not compatible with self')

Is now:

    X = check_array(X)
    if X.ndim == 1:
        X = X[:, np.newaxis]
    if X.size == 0:
        return np.array([]), np.empty((0, self.n_components))
    if X.shape[1] != self.means_.shape[1]:
        raise ValueError('The shape of X  is not compatible with self')

This changes everything, as when you use it on a list, np.asarray returns a 1d array, which is then turned to a 2d array of shape (n,1) by the next lines, and the array is in the correct format. Whereas check_array turns our list into a (1,n) array, which leads to the error.

Am I getting this wrong or is this indeed what the problem is ? And in this case it's probably a bug in the latest release.

Activity

  1. amueller commented on Apr 3, 2015

    @amueller
    Member

    Argh, it is an error.

  2. xuewei4d commented on Apr 3, 2015

    @xuewei4d
    Contributor

    I would like to try to fix it, but the docstring of score_sample said

    X: array_like, shape (n_samples, n_features)

    Maybe we should stick to that X must be [n_sample, n_feature] 2D array ? That means the input cannot be a list?

  3. amueller commented on Apr 3, 2015

    @amueller
    Member

    This is a problem with ndim=1, not lists. I am against dropping support for X of shape (n_samples,) (I know @GaelVaroquaux has somewhat different views). but we did support it in many places before, and I think it is better to make the support consistent (just working on that).

  4. amueller commented on Apr 3, 2015

    @amueller
    Member

    There was a related issue recently but I can't find it :-/

  5. amueller commented on Apr 3, 2015

    @amueller
    Member
  6. atabakd commented on Jul 5, 2015

    @atabakd

    Hey,
    Can I ask if there is some kind of a workaround?
    for now, I just did this:

       check_is_fitted(self, 'means_')
        X = check_array(X)
        if X.shape[0] == 1:
            X = X[0,:]
    
  7. amueller commented on Jul 12, 2015

    @amueller
    Member

    simple workaround: don't use 1d arrays ;)

  8. amueller commented on Sep 9, 2015

    @amueller
    Member

    Fixed by #5152 (somewhat). It warns to use 2d X now and then breaks. I don't think we should back-fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions