Skip to content

LinearDiscriminantAnalysis.fit() fails if #samples == #labels. #12374

@jenstroeger

Description

@jenstroeger

Description

The LinearDiscriminantAnalysis.fit() method throws an exception if number of samples and number of labels is the same, i.e. each label has exactly one sample.

Steps/Code to Reproduce

>>> import sklearn.discriminant_analysis
>>> import numpy
>>> X = numpy.array([[0.5, 0.6], [0.6, 0.5]])
>>> y = numpy.array(["a", "b"])
>>> lda = sklearn.discriminant_analysis.LinearDiscriminantAnalysis()
>>> lda.fit(X, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/…/lib/python3.6/site-packages/sklearn/discriminant_analysis.py", line 455, in fit
    self._solve_svd(X, y)
  File "/…/lib/python3.6/site-packages/sklearn/discriminant_analysis.py", line 379, in _solve_svd
    fac = 1. / (n_samples - n_classes)
ZeroDivisionError: float division by zero

Expected Results

No exception, and a successful call to fit().

Actual Results

Exception, see above.

Versions

>>> sklearn.show_versions()

System
------
    python: 3.6.6 (default, Jun 28 2018, 05:50:42)  [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
executable: /…/bin/python
   machine: Darwin-15.6.0-x86_64-i386-64bit

BLAS
----
    macros: NO_ATLAS_INFO=3, HAVE_CBLAS=None
  lib_dirs: 
cblas_libs: cblas

Python deps
-----------
       pip: 18.0
setuptools: 28.8.0
   sklearn: 0.20.0
     numpy: 1.15.2
     scipy: 1.1.0
    Cython: None
    pandas: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions