| 1 | GaussianMixture predict and fit_predict disagree when n_init>1 |
| 2 | #### Description |
| 3 | When `n_init` is specified in GaussianMixture, the results of fit_predict(X) and predict(X) are often different. The `test_gaussian_mixture_fit_predict` unit test doesn't catch this because it does not set `n_init`. |
| 4 | |
| 5 | #### Steps/Code to Reproduce |
| 6 | ``` |
| 7 | python |
| 8 | from sklearn.mixture import GaussianMixture |
| 9 | from sklearn.utils.testing import assert_array_equal |
| 10 | import numpy |
| 11 | X = numpy.random.randn(1000,5) |
| 12 | print 'no n_init' |
| 13 | gm = GaussianMixture(n_components=5) |
| 14 | c1 = gm.fit_predict(X) |
| 15 | c2 = gm.predict(X) |
| 16 | assert_array_equal(c1,c2) |
| 17 | print 'n_init=5' |
| 18 | gm = GaussianMixture(n_components=5, n_init=5) |
| 19 | c1 = gm.fit_predict(X) |
| 20 | c2 = gm.predict(X) |
| 21 | assert_array_equal(c1,c2) |
| 22 | ``` |
| 23 | |
| 24 | #### Expected Results |
| 25 | ``` |
| 26 | no n_init |
| 27 | n_init=5 |
| 28 | ``` |
| 29 | No exceptions. |
| 30 | |
| 31 | #### Actual Results |
| 32 | ``` |
| 33 | no n_init |
| 34 | n_init=5 |
| 35 | Traceback (most recent call last): |
| 36 | File "test_gm.py", line 17, in <module> |
| 37 | assert_array_equal(c1,c2) |
| 38 | File "/home/scott/.local/lib/python2.7/site-packages/numpy/testing/_private/utils.py", line 872, in assert_array_equal |
| 39 | verbose=verbose, header='Arrays are not equal') |
| 40 | File "/home/scott/.local/lib/python2.7/site-packages/numpy/testing/_private/utils.py", line 796, in assert_array_compare |
| 41 | raise AssertionError(msg) |
| 42 | AssertionError: |
| 43 | Arrays are not equal |
| 44 | |
| 45 | (mismatch 88.6%) |
| 46 | x: array([4, 0, 1, 1, 1, 3, 3, 4, 4, 2, 0, 0, 1, 2, 0, 2, 0, 1, 3, 1, 1, 3, |
| 47 | 2, 1, 0, 2, 1, 0, 2, 0, 3, 1, 2, 3, 3, 1, 0, 2, 2, 0, 3, 0, 2, 0, |
| 48 | 4, 2, 3, 0, 4, 2, 4, 1, 0, 2, 2, 1, 3, 2, 1, 4, 0, 2, 2, 1, 1, 2,... |
| 49 | y: array([4, 1, 0, 2, 2, 1, 1, 4, 4, 0, 4, 1, 0, 3, 1, 0, 2, 2, 1, 2, 0, 0, |
| 50 | 1, 0, 4, 1, 0, 4, 0, 1, 1, 2, 3, 1, 4, 0, 1, 4, 4, 4, 0, 1, 0, 2, |
| 51 | 4, 1, 1, 2, 4, 3, 4, 0, 2, 3, 2, 3, 0, 0, 2, 3, 3, 3, 3, 0, 3, 2,... |
| 52 | ``` |
| 53 | |
| 54 | #### Versions |
| 55 | ``` |
| 56 | System: |
| 57 | python: 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0] |
| 58 | machine: Linux-4.15.0-43-generic-x86_64-with-Ubuntu-18.04-bionic |
| 59 | executable: /usr/bin/python |
| 60 | |
| 61 | BLAS: |
| 62 | macros: HAVE_CBLAS=None, NO_ATLAS_INFO=-1 |
| 63 | cblas_libs: cblas |
| 64 | lib_dirs: /usr/lib/x86_64-linux-gnu |
| 65 | |
| 66 | Python deps: |
| 67 | Cython: 0.28.5 |
| 68 | scipy: 1.2.0 |
| 69 | setuptools: 39.0.1 |
| 70 | pip: 19.0.1 |
| 71 | numpy: 1.16.0 |
| 72 | pandas: 0.23.1 |
| 73 | sklearn: 0.20.2 |
| 74 | ``` |