<h4 dir="auto">Description</h4>

<p dir="auto">This maybe a possible bug or my assumption is wrong regarding the Incremental PCA's fit / partial_fit methods. For valid parameters, the error thrown is <em>ValueError: Number of input features has changed from 10 to 100 between calls to partial_fit! Try setting n_components to a fixed value.</em></p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<p dir="auto">Example:<br>
I want to perform the pca on the below matrix with dimensions (n_samples, n_features) = (100, 1000).<br>
I want to reduce it to a 100 dimension feature space and hence, I am setting the number of components to 100.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
input_matrix = np.random.randn(100, 1000)

ipca = IncrementalPCA(n_components=100, batch_size=10)
ipca.fit(input_matrix) # Error at this line
reduced_matrix = ipca.transform(input_matrix)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">input_matrix</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randn</span>(<span class="pl-c1">100</span>, <span class="pl-c1">1000</span>)

<span class="pl-s1">ipca</span> <span class="pl-c1">=</span> <span class="pl-v">IncrementalPCA</span>(<span class="pl-s1">n_components</span><span class="pl-c1">=</span><span class="pl-c1">100</span>, <span class="pl-s1">batch_size</span><span class="pl-c1">=</span><span class="pl-c1">10</span>)
<span class="pl-s1">ipca</span>.<span class="pl-en">fit</span>(<span class="pl-s1">input_matrix</span>) <span class="pl-c"># Error at this line</span>
<span class="pl-s1">reduced_matrix</span> <span class="pl-c1">=</span> <span class="pl-s1">ipca</span>.<span class="pl-en">transform</span>(<span class="pl-s1">input_matrix</span>)</pre></div>
<h4 dir="auto">Expected Results</h4>
<p dir="auto">No error is thrown and the reduced matrix shape must be (n_samples, n_features) = (100, 100)</p>
<h4 dir="auto">Actual Results</h4>
<p dir="auto">ValueError: Number of input features has changed from 10 to 100 between calls to partial_fit! Try setting n_components to a fixed value. Commenting out the condition check produces expected result.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="if (self.components_ is not None) and (self.components_.shape[0] != self.n_components_):
	raise ValueError(&quot;Number of input features has changed from %i &quot;
		&quot;to %i between calls to partial_fit! Try &quot;
		&quot;setting n_components to a fixed value.&quot; %
		(self.components_.shape[0], self.n_components_))"><pre class="notranslate"><code class="notranslate">if (self.components_ is not None) and (self.components_.shape[0] != self.n_components_):
	raise ValueError("Number of input features has changed from %i "
		"to %i between calls to partial_fit! Try "
		"setting n_components to a fixed value." %
		(self.components_.shape[0], self.n_components_))
</code></pre></div>
<h4 dir="auto">Versions</h4>

<p dir="auto">Linux-3.10.0-514.26.2.el7.x86_64-x86_64-with-centos-7.3.1611-Core<br>
('Python', '2.7.5 (default, Nov  6 2016, 00:28:07) \n[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]')<br>
('NumPy', '1.13.0')<br>
('SciPy', '0.18.1')<br>
('Scikit-Learn', '0.18.1')</p>
<p dir="auto"><code class="notranslate">IncrementalPCA.partial_fit</code> raises the following error when the number of samples is smaller than <code class="notranslate">n_components</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  File &quot;___/sklearn/decomposition/incremental_pca.py&quot;, 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."><pre class="notranslate"><code class="notranslate">  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.
</code></pre></div>
<p dir="auto">This error can be generated via the following snippet as long as <code class="notranslate">n_samples &lt; n_components</code>:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="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))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">decomposition</span> <span class="pl-k">import</span> <span class="pl-v">IncrementalPCA</span>

<span class="pl-s1">n_samples</span>, <span class="pl-s1">n_features</span> <span class="pl-c1">=</span> <span class="pl-c1">10</span>, <span class="pl-c1">50</span>
<span class="pl-s1">ipca</span> <span class="pl-c1">=</span> <span class="pl-v">IncrementalPCA</span>(<span class="pl-s1">n_components</span><span class="pl-c1">=</span><span class="pl-c1">20</span>)

<span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">5</span>):
    <span class="pl-s1">ipca</span>.<span class="pl-en">partial_fit</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">rand</span>(<span class="pl-s1">n_samples</span>, <span class="pl-s1">n_features</span>))</pre></div>
<p dir="auto">The error message is not informative because the number of input features is constant (i.e., 50)<br>
and  <code class="notranslate">n_components</code> is fixed anyways.</p>
<p dir="auto">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.<br>
Is this a limitation of the incremental PCA algorithm or the implementation?</p>