10000 Merge pull request #6125 from AnishShah/issue6118 · scikit-learn/scikit-learn@6b1d351 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b1d351

Browse files
committed
Merge pull request #6125 from AnishShah/issue6118
[MRG + 2] inverse_transformation for NMF decomposition
2 parents 9522916 + a6604c3 commit 6b1d351

File tree

3 files changed

+35
-0
lines changed
  • doc
    • < 8000 div class="PRIVATE_TreeView-item-level-line prc-TreeView-TreeViewItemLevelLine-KPSSL">
  • sklearn/decomposition
  • 3 files changed

    +35
    -0
    lines changed

    doc/whats_new.rst

    Lines changed: 5 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -102,6 +102,9 @@ Enhancements
    102102
    - Prediction of out-of-sample events with Isotonic Regression is now much
    103103
    faster (over 1000x in tests with synthetic data). By `Jonathan Arfa`_.
    104104

    105+
    - Added ``inverse_transform`` function to :class:`decomposition.nmf` to compute
    106+
    data matrix of original shape. By `Anish Shah`_.
    107+
    105108
    Bug fixes
    106109
    .........
    107110

    @@ -4088,3 +4091,5 @@ David Huard, Dave Morrill, Ed Schofield, Travis Oliphant, Pearu Peterson.
    40884091
    .. _Devashish Deshpande: https://github.com/dsquareindia
    40894092

    40904093
    .. _Jonathan Arfa: https://github.com/jarfa
    4094+
    4095+
    .. _Anish Shah: https://github.com/AnishShah

    sklearn/decomposition/nmf.py

    Lines changed: 17 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1108,6 +1108,23 @@ def transform(self, X):
    11081108
    self.n_iter_ = n_iter_
    11091109
    return W
    11101110

    1111+
    def inverse_transform(self, W):
    1112+
    """
    1113+
    Parameters
    1114+
    ----------
    1115+
    W: {array-like, sparse matrix}, shape (n_samples, n_components)
    1116+
    Transformed Data matrix
    1117+
    1118+
    Returns
    1119+
    -------
    1120+
    X: {array-like, sparse matrix}, shape (n_samples, n_features)
    1121+
    Data matrix of original shape
    1122+
    1123+
    .. versionadded:: 0.18
    1124+
    """
    1125+
    check_is_fitted(self, 'n_components_')
    1126+
    return np.dot(W, self.components_)
    1127+
    11111128

    11121129
    @deprecated("It will be removed in release 0.19. Use NMF instead."
    11131130
    "'pg' solver is still available until release 0.19.")

    sklearn/decomposition/tests/test_nmf.py

    Lines changed: 13 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -120,6 +120,19 @@ def test_nmf_transform():
    120120
    assert_array_almost_equal(ft, t, decimal=2)
    121121

    122122

    123+
    @ignore_warnings
    124+
    def test_nmf_inverse_transform():
    125+
    # Test that NMF.inverse_transform returns close values
    126+
    random_state = np.random.RandomState(0)
    127+
    A = np.abs(random_state.randn(6, 4))
    128+
    for solver in ('pg', 'cd'):
    129+
    m = NMF(solver=solver, n_components=4, init='random', random_state=0)
    130+
    ft = m.fit_transform(A)
    131+
    t = m.transform(A)
    132+
    A_new = m.inverse_transform(t)
    133+
    assert_array_almost_equal(A, A_new, decimal=2)
    134+
    135+
    123136
    @ignore_warnings
    124137
    def test_n_components_greater_n_features():
    125138
    # Smoke test for the case of more components than features.

    0 commit comments

    Comments
     (0)
    0