编程语言
首页 > 编程语言> > python NIMFA 非负矩阵分解

python NIMFA 非负矩阵分解

作者:互联网

>>> import numpy as np
>>> import scipy.sparse as spr
>>> import nimfa
>>> V=spr.csr_matrix([[1, 0, 2, 4], [0, 0, 6, 3], [4, 0, 5, 6]])
>>> print(V)
  (0, 0)        1
  (0, 2)        2
  (0, 3)        4
  (1, 2)        6
  (1, 3)        3
  (2, 0)        4
  (2, 2)        5
  (2, 3)        6
>>> print('Target:\n%s' % V.todense())
Target:
[[1 0 2 4]
 [0 0 6 3]
 [4 0 5 6]]
>>> nmf=nimfa.Nmf(V,max_iter=200,rank=2,update='euclidean',objective='fro')
>>> nmf_fit=nmf() 
>>> W=nmf_fit.basis()
>>>> print(W)
  (0, 0)        3.044943595437404
  (1, 1)        4.510880380218402
  (1, 0)        2.0362823494561906
  (2, 0)        5.392166550228975
>>> print('Basis matrix:\n%s' %W.todense())
Basis matrix:
[[3.0449436  0.        ]
 [2.03628235 4.51088038]
 [5.39216655 0.        ]]
>>> H=nmf_fit.coef()
>>> print('Mixtrue matrix:\n%s' % H.todense())
Mixtrue matrix:
[[0.5792295  0.         0.         1.19174817]
 [0.         0.         1.33011729 0.        ]]
>>> print('Euclidean distance: %5.3f' % nmf_fit.distance(metric='euclidean'))
Euclidean distance: 32.391
>>> sm = nmf_fit.summary()
print('Sparseness Basis: %5.3f  Mixture: %5.3f' % (sm['sparseness'][0], sm['sparseness'][1]))
print('Iterations: %d' % sm['n_iter'])
print('Target estimate:\n%s' % np.dot(W.todense(), H.todense()))>>> print('Sparseness Basis: %5.3f  Mixture: %5.3f' % (sm['sparseness'][0], sm['sparseness'][1]))
Sparseness Basis: 0.586  Mixture: 1.000
>>> print('Iterations: %d' % sm['n_iter'])
Iterations: 5
>>> print('Target estimate:\n%s' % np.dot(W.todense(), H.todense()))
Target estimate:
[[1.76372117 0.         0.         3.62880595]
 [1.17947481 0.         6.         2.42673576]
 [3.12330196 0.         0.         6.4261046 ]]

标签:5.3,matrix,非负,python,nmf,todense,sm,print,NIMFA
来源: https://blog.csdn.net/m0_37586991/article/details/89946925