<h4 dir="auto">Description</h4>
<p dir="auto">sklearn PCA with n_components = 'mle' and svd_solver = 'full' results in math domain error</p>
<p dir="auto">The problem is in <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/decomposition/pca.py#L88">this line of code</a>. The result of  <code class="notranslate">(spectrum[i] - spectrum[j])</code> is 0 and therefore i get <code class="notranslate">log(0)</code> which causes this exception.<br>
Is this a sign of bad data or should the implementation handle this case?</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<p dir="auto">Store this in a file i.e. foo.csv</p>
<blockquote>
<p dir="auto">0.00000,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99960,   0.99903,   0.28285,   0.28246,   1.00000,   0.00000,   0.00000,   0.92666,   0.82283,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99991,   0.99934,   0.31101,   0.31064,   1.00000,   0.00000,   0.00000,   0.99241,   0.51799,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99991,   0.99934,   0.31101,   0.31064,   1.00000,   0.00000,   0.00000,   0.99241,   0.51799,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99991,   0.99934,   0.31101,   0.31064,   1.00000,   0.00000,   0.00000,   0.99241,   0.51799,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00006,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99920,   0.99863,   0.35419,   0.35384,   1.00000,   0.00000,   0.00000,   0.99421,   0.47923,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
1.00000,   0.11108,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99144,   0.99088,   0.31786,   0.31748,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000<br>
0.00027,   0.35941,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.87935,   0.87885,   0.32529,   0.32492,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00011,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00966,   0.99986,   0.99929,   0.35232,   0.35196,   1.00000,   0.00000,   0.00000,   0.98768,   0.53487,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000<br>
0.00000,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99882,   0.99825,   0.21965,   0.21922,   1.00000,   0.00000,   0.00000,   1.00000,   0.60636,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00027,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99883,   0.99826,   0.33354,   0.33318,   1.00000,   0.00000,   0.00000,   0.99208,   0.51767,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000<br>
0.00064,   0.38476,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.63538,   0.99263,   0.99207,   0.35795,   0.35759,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000<br>
0.00064,   0.38476,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.74959,   0.96602,   1.00000,   0.35795,   0.35759,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.11961,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99962,   0.99906,   0.28849,   0.28810,   1.00000,   0.00000,   0.00000,   0.96794,   0.61479,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00018,   0.17423,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.99251,   0.99195,   0.39428,   0.39395,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000<br>
0.00041,   0.24312,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.98138,   0.99330,   0.99273,   0.40907,   0.40988,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00007,   0.16128,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.25979,   0.99251,   0.99195,   0.41658,   0.41626,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.09204,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99967,   0.99911,   0.34629,   0.34593,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00069,   0.10755,   1.00000,   1.00000,   1.00000,   1.00000,   1.00000,   1.00000,   0.00000,   0.99592,   0.99536,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00091,   0.18421,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   1.00000,   1.00000,   0.00000,   0.00000,   0.92623,   0.82295,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00091,   0.18421,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   1.00000,   1.00000,   0.00000,   0.00000,   0.92623,   0.82295,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99998,   0.99941,   0.31381,   0.31343,   1.00000,   0.00000,   0.00000,   0.96963,   0.69883,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.94950,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.99943,   0.31559,   0.31521,   1.00000,   0.00000,   0.00000,   0.92511,   0.68432,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00224,   0.89557,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.99999,   0.99942,   0.31477,   0.31439,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.79127,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.99943,   0.31602,   0.31580,   1.00000,   0.00000,   0.00000,   0.96963,   0.69883,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00000,   0.61136,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.99943,   0.31618,   0.31580,   1.00000,   0.00000,   0.00000,   0.96963,   0.69883,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000<br>
0.00022,   0.19567,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   1.00000,   0.99943,   0.31477,   0.31439,   1.00000,   0.00000,   0.00000,   0.98541,   0.98441,   1.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000,   0.00000</p>
</blockquote>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy
from sklearn.decomposition import PCA

test = numpy.genfromtxt(&quot;path/to/foo.csv&quot;, delimiter=',')
pca = PCA(n_components='mle', svd_solver='full')
data = pca.fit_transform(test) # This will produce the error"><pre class="notranslate"><code class="notranslate">import numpy
from sklearn.decomposition import PCA

test = numpy.genfromtxt("path/to/foo.csv", delimiter=',')
pca = PCA(n_components='mle', svd_solver='full')
data = pca.fit_transform(test) # This will produce the error
</code></pre></div>
<h4 dir="auto">Expected Results</h4>
<p dir="auto">I expect data to have dimensions (X,Y) with X &lt;= 26 and Y &lt;=26</p>
<h4 dir="auto">Actual Results</h4>
<blockquote>
<p dir="auto">Traceback (most recent call last):<br>
File "path/to/playground.py", line 13, in <br>
data = pca.fit_transform(test) # This will produce the error<br>
File "$path$\Python\Python36\lib\site-packages\sklearn\decomposition\pca.py", line 344, in fit_transform<br>
U, S, V = self._fit(X)<br>
File "$path$\Python\Python36\lib\site-packages\sklearn\decomposition\pca.py", line 388, in <em>fit<br>
return self.<em>fit_full(X, n_components)<br>
File "$path$\Python\Python36\lib\site-packages\sklearn\decomposition\pca.py", line 427, in <em>fit_full<br>
<em>infer_dimension</em>(explained_variance</em>, n_samples, n_features)<br>
File "$path$\Python\Python36\lib\site-packages\sklearn\decomposition\pca.py", line 103, in <em>infer_dimension</em><br>
ll[rank] = <em>assess_dimension</em>(spectrum, rank, n_samples, n_features)<br>
File "$path$\Python\Python36\lib\site-packages\sklearn\decomposition\pca.py", line 88, in <em>assess_dimension</em><br>
(1. / spectrum</em>[j] - 1. / spectrum</em>[i])) + log(n_samples)<br>
ValueError: math domain error</p>
</blockquote>
<h4 dir="auto">Versions</h4>
<p dir="auto">Windows-10-10.0.14393-SP0<br>
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]<br>
NumPy 1.13.1<br>
SciPy 0.19.1<br>
Scikit-Learn 0.19.0</p>
<h4 dir="auto">Side References</h4>
<p dir="auto">I asked a question regarding this problem on <a href="https://stackoverflow.com/questions/47548178/sklearn-pca-with-n-components-mle-and-svd-solver-full-results-in-math-do" rel="nofollow">stackoverflow</a></p>
<p dir="auto">Sorry no show_versions output (yet to add to <code class="notranslate">debian/rules</code>, numpy 1.12.1), and I just wanted to possibly seek ideas/clues before digging deeper or skipping those tests - everything seems to be ok on more recentish systems:</p>
<h2 dir="auto">1</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="____________________________ test_lars_cv_max_iter _____________________________

    @pytest.mark.filterwarnings('ignore::FutureWarning')
    def test_lars_cv_max_iter():
        with warnings.catch_warnings(record=True) as w:
            X = diabetes.data
            y = diabetes.target
            rng = np.random.RandomState(42)
            x = rng.randn(len(y))
            X = np.c_[X, x, x]  # add correlated features
            lars_cv = linear_model.LassoLarsCV(max_iter=5)
            lars_cv.fit(X, y)
&gt;       assert_true(len(w) == 0)"><pre class="notranslate"><code class="notranslate">____________________________ test_lars_cv_max_iter _____________________________

    @pytest.mark.filterwarnings('ignore::FutureWarning')
    def test_lars_cv_max_iter():
        with warnings.catch_warnings(record=True) as w:
            X = diabetes.data
            y = diabetes.target
            rng = np.random.RandomState(42)
            x = rng.randn(len(y))
            X = np.c_[X, x, x]  # add correlated features
            lars_cv = linear_model.LassoLarsCV(max_iter=5)
            lars_cv.fit(X, y)
&gt;       assert_true(len(w) == 0)
</code></pre></div>
<p dir="auto">re <code class="notranslate">assert_true</code> submitted <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="378548639" data-permission-text="Title is private" data-url="https://github.com/scikit-learn/scikit-learn/issues/12547" data-hovercard-type="pull_request" data-hovercard-url="/scikit-learn/scikit-learn/pull/12547/hovercard" href="https://github.com/scikit-learn/scikit-learn/pull/12547">#12547</a> - but anything sounds familiar?</p>
<h2 dir="auto">2</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="__________________________ test_docstring_parameters ___________________________

    @pytest.mark.filterwarnings('ignore::DeprecationWarning')
    @pytest.mark.skipif(IS_PYPY, reason='test segfaults on PyPy') 
    def test_docstring_parameters(): 
        # Test module docstring formatting
...
        msg = '\n' + '\n'.join(sorted(list(set(incorrect))))
        if len(incorrect) &gt; 0:
&gt;           raise AssertionError(&quot;Docstring Error: &quot; + msg)
E           AssertionError: Docstring Error:
E           sklearn.utils.fixes.divide arg mismatch: ['dtype', 'out', 'x1', 'x2']

sklearn/tests/test_docstring_parameters.py:125: AssertionError"><pre class="notranslate"><code class="notranslate">__________________________ test_docstring_parameters ___________________________

    @pytest.mark.filterwarnings('ignore::DeprecationWarning')
    @pytest.mark.skipif(IS_PYPY, reason='test segfaults on PyPy') 
    def test_docstring_parameters(): 
        # Test module docstring formatting
...
        msg = '\n' + '\n'.join(sorted(list(set(incorrect))))
        if len(incorrect) &gt; 0:
&gt;           raise AssertionError("Docstring Error: " + msg)
E           AssertionError: Docstring Error:
E           sklearn.utils.fixes.divide arg mismatch: ['dtype', 'out', 'x1', 'x2']

sklearn/tests/test_docstring_parameters.py:125: AssertionError
</code></pre></div>
<p dir="auto">so this one is particularly mysterious ;)</p>
<details>
<summary>show_versions()</summary>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ATLAS version 3.10.3 built by buildd on Tue Jan 17 22:59:14 UTC 2017:
   UNAME    : Linux x86-ubc-01 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1 (2016-12-30) x86_64 GNU/Linux
   INSTFLG  : -1 0 -a 1 -l 1
   ARCHDEFS : -DATL_OS_Linux -DATL_ARCH_x86SSE2 -DATL_CPUMHZ=2297 -DATL_SSE2 -DATL_SSE1 -DATL_USE64BITS -DATL_GAS_x8664
   F2CDEFS  : -DAdd_ -DF77_INTEGER=int -DStringSunStyle
   CACHEEDGE: 1048576
   F77      : /usr/bin/x86_64-linux-gnu-gfortran, version GNU Fortran (Debian 6.3.0-2) 6.3.0 20161229
   F77FLAGS : -fomit-frame-pointer -mfpmath=sse -O2 -msse2 -fPIC -m64
   SMC      : /usr/bin/gcc-6, version gcc-6 (Debian 6.3.0-2) 6.3.0 20161229
   SMCFLAGS : -fomit-frame-pointer -mfpmath=sse -O2 -msse2 -fPIC -m64
   SKC      : /usr/bin/gcc-6, version gcc-6 (Debian 6.3.0-2) 6.3.0 20161229
   SKCFLAGS : -fomit-frame-pointer -mfpmath=sse -O2 -msse2 -fPIC -m64

System
------
    python: 2.7.13 (default, Nov 24 2017, 17:33:09)  [GCC 6.3.0 20170516]
   machine: Linux-4.16.0-0.bpo.2-amd64-x86_64-with-debian-9.5
executable: /usr/bin/python2.7

BLAS
----
    macros: HAVE_CBLAS=None, ATLAS_INFO=&quot;\&quot;3.10.3\&quot;&quot;
cblas_libs: f77blas, cblas, atlas, f77blas, cblas
  lib_dirs: /usr/lib/atlas-base

Python deps
-----------
    Cython: None
     scipy: 0.18.1
setuptools: 33.1.1
       pip: None
     numpy: 1.12.1
    pandas: 0.22.0
   sklearn: 0.20.0"><pre class="notranslate"><code class="notranslate">ATLAS version 3.10.3 built by buildd on Tue Jan 17 22:59:14 UTC 2017:
   UNAME    : Linux x86-ubc-01 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1 (2016-12-30) x86_64 GNU/Linux
   INSTFLG  : -1 0 -a 1 -l 1
   ARCHDEFS : -DATL_OS_Linux -DATL_ARCH_x86SSE2 -DATL_CPUMHZ=2297 -DATL_SSE2 -DATL_SSE1 -DATL_USE64BITS -DATL_GAS_x8664
   F2CDEFS  : -DAdd_ -DF77_INTEGER=int -DStringSunStyle
   CACHEEDGE: 1048576
   F77      : /usr/bin/x86_64-linux-gnu-gfortran, version GNU Fortran (Debian 6.3.0-2) 6.3.0 20161229
   F77FLAGS : -fomit-frame-pointer -mfpmath=sse -O2 -msse2 -fPIC -m64
   SMC      : /usr/bin/gcc-6, version gcc-6 (Debian 6.3.0-2) 6.3.0 20161229
   SMCFLAGS : -fomit-frame-pointer -mfpmath=sse -O2 -msse2 -fPIC -m64
   SKC      : /usr/bin/gcc-6, version gcc-6 (Debian 6.3.0-2) 6.3.0 20161229
   SKCFLAGS : -fomit-frame-pointer -mfpmath=sse -O2 -msse2 -fPIC -m64

System
------
    python: 2.7.13 (default, Nov 24 2017, 17:33:09)  [GCC 6.3.0 20170516]
   machine: Linux-4.16.0-0.bpo.2-amd64-x86_64-with-debian-9.5
executable: /usr/bin/python2.7

BLAS
----
    macros: HAVE_CBLAS=None, ATLAS_INFO="\"3.10.3\""
cblas_libs: f77blas, cblas, atlas, f77blas, cblas
  lib_dirs: /usr/lib/atlas-base

Python deps
-----------
    Cython: None
     scipy: 0.18.1
setuptools: 33.1.1
       pip: None
     numpy: 1.12.1
    pandas: 0.22.0
   sklearn: 0.20.0
</code></pre></div>
</details>