Skip to content

SparsePCA inconsistent on sub-sample #10431

Closed

Description

@Johayon

Description

SparsePCA and MiniBatchSparsePCA will output different results on the method transform if it is applied on all the data or a subset. This issue follows from the PR #10428

Steps/Code to Reproduce

import numpy as np
from sklearn.decomposition import SparsePCA
rnd = np.random.RandomState(0)
X = 2 * rnd.uniform(size=(5, 2))
y = X[:, 0].astype(np.int)
spca = SparsePCA()
spca.fit(X,y)
res_all = spca.transform(X)
res_one = np.array([spca.transform(X[i].reshape(1, X.shape[1]))[0] 
                    for i in range(X.shape[0])])
print(res_all)
print(res_one)

Expected Results

res_one = res_all

Actual Results

[[-0.48574184 -0.39159711]
 [-0.3700734  -0.43009217]
 [-0.43867793 -0.30229211]
 [-0.60567379 -0.31223336]
 [-0.26042556 -0.68760616]]
[[-1. -1.]
 [-1. -1.]
 [-1. -1.]
 [-1. -1.]
 [-1. -1.]]

Versions

Linux-4.10.0-42-generic-x86_64-with-debian-stretch-sid
Python 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:09:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
NumPy 1.12.1
SciPy 0.19.0
Scikit-Learn 0.19.1 

Activity

  1. jnothman commented on Jan 8, 2018

    @jnothman
    Member
  2. changed the title [-]SparsePCA inconsistant on sub-sample[/-] [+]SparsePCA inconsistent on sub-sample[/+] on May 22, 2018
  3. krassowski commented on Mar 23, 2020

    @krassowski

    I think that maybe this issue could be closed now. To check if the problem is still there I need to set alpha != 1 to get a non-trivial solution; the results appear to be consistent now (whether transforming the full matrix, or one sample at a time).

    import numpy as np
    from sklearn.decomposition import SparsePCA
    rnd = np.random.RandomState(0)
    X = 2 * rnd.uniform(size=(5, 2))
    y = X[:, 0].astype(np.int)
    spca = SparsePCA(alpha=0.5)
    spca.fit(X,y)
    res_all = spca.transform(X)
    res_one = np.array([spca.transform(X[i].reshape(1, X.shape[1]))[0] 
                        for i in range(X.shape[0])])
    print(res_all)
    print(res_one)
    

    Result:

    >>> print(res_all)
    [[ 0.15639301  0.        ]
     [-0.10078385  0.        ]
     [ 0.30639312  0.        ]
     [ 0.5187504   0.        ]
     [-0.88075267  0.        ]]
    >>> print(res_one)
    [[ 0.15639301  0.        ]
     [-0.10078385  0.        ]
     [ 0.30639312  0.        ]
     [ 0.5187504   0.        ]
     [-0.88075267  0.        ]]
    >>> from sklearn import __version__
    >>> __version__
    '0.22.2.post1'
    

    I think that it was fixed by #11585

    Is the test from #10428 still failing?

  4. thomasjpfan commented on May 23, 2020

    @thomasjpfan
    Member

    Thank you for testing @krassowski. This issue can be closed along with allowing the test to run on the SparsePCA and MiniBatchSparsePCA estimators.

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