Skip to content

IncrementalPCA.partial_fit gives an uninformative error message and is possibly broken  #6452

Closed
@erensezener

Description

@erensezener

IncrementalPCA.partial_fit raises the following error when the number of samples is smaller than n_components:

  File "___/sklearn/decomposition/incremental_pca.py", line 218, in partial_fit
    (self.components_.shape[0], self.n_components_))
ValueError: Number of input features has changed from 10 to 20 between calls to partial_fit! 
Try setting n_components to a fixed value.

This error can be generated via the following snippet as long as n_samples < n_components:

import numpy as np
from sklearn.decomposition import IncrementalPCA

n_samples, n_features = 10, 50
ipca = IncrementalPCA(n_components=20)

for i in range(5):
    ipca.partial_fit(np.random.rand(n_samples, n_features))

The error message is not informative because the number of input features is constant (i.e., 50)
and n_components is fixed anyways.

Also, I don't see any fundamental reason why this shouldn't work. In regular PCA, number of components after transformation can be larger than the number of samples.
Is this a limitation of the incremental PCA algorithm or the implementation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementModerateAnything that requires some knowledge of conventions and best practicesSprint

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions