Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
Understand NMF using image and some random matrix
import numpy as np
X = np.random.randint(1,20,64).reshape(8,8)
from sklearn.decomposition import NMF
model = NMF(n_components=3, init='random', random_state=0)
W = model.fit_transform(X)
H = model.components_
W
array([[0.65908054, 2.46550098, 2.1994926 ],
[0. , 3.66329273, 1.87605977],
[1.77254496, 0. , 2.42391908],
[2.4952005 , 3.51749554, 0.01527803],
[1.3871326 , 2.72017775, 1.49985618],
[0.18888674, 5.01174563, 0.36160411],
[0.4253659 , 3.72835522, 2.04769359],
[3.33135637, 1.78131666, 0. ]])
H
array([[2.69715659e+00, 0.00000000e+00, 4.71903975e+00, 4.09038952e+00,
3.01271305e-02, 1.21059719e+00, 4.96672930e+00, 2.43705579e+00],
[0.00000000e+00, 3.68138679e+00, 8.06933225e-01, 1.56879319e+00,
2.84038169e+00, 5.01040057e-01, 1.10147748e+00, 2.25174377e+00],
[4.68183528e+00, 7.32343130e-01, 1.42166948e+00, 1.01440501e+00,
7.85241464e-01, 7.10566802e+00, 6.14786871e-03, 0.00000000e+00]])
X
array([[10, 10, 9, 9, 8, 19, 5, 9],
[12, 17, 8, 3, 12, 13, 5, 7],
[19, 2, 13, 12, 1, 17, 4, 6],
[ 3, 15, 19, 18, 10, 6, 15, 8],
[ 8, 15, 13, 3, 8, 16, 18, 5],
[ 6, 18, 5, 10, 13, 3, 2, 17],
[ 8, 11, 2, 17, 15, 19, 7, 9],
[12, 4, 13, 16, 6, 4, 19, 17]])