Jovian
⭐️
Sign In

Understand NMF using image and some random matrix

In [43]:
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_
In [44]:
W
Out[44]:
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.        ]])
In [45]:
H
Out[45]:
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]])
In [46]:
X
Out[46]:
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]])
In [47]:
np.dot(W,H)
Out[47]:
array([[12.07530549, 10.68724604,  8.22667344,  8.79493362,  8.74995284,
        17.66206005,  6.00269064,  7.15789252],
       [ 8.78340283, 14.85991693,  5.62316953,  7.65003313, 11.87830951,
        15.16611433,  4.04656821,  8.24879656],
       [16.1292212 ,  1.77514049, 11.8107219 ,  9.70923499,  1.95676346,
        19.36940227,  8.81865292,  4.31979095],
       [ 6.8014757 , 12.96045037, 14.63505466, 15.74006315, 10.0782001 ,
         4.89164949, 16.26752147, 14.00144147],
       [10.76339342, 11.11243582, 10.87323545, 11.46277064,  8.94588268,
        13.69965694,  9.8949476 ,  9.50566284],
       [ 2.202428  , 18.71499246,  5.44958962,  9.0018258 , 14.5249077 ,
         5.30918984,  6.46069734, 11.74549451],
       [10.73424252, 15.225132  ,  7.92699578,  9.66612117, 12.21070088,
        16.93323292,  6.23196556,  9.43194107],
       [ 8.98518979,  6.55771563, 17.15820672, 16.42106263,  5.15998344,
         4.92544165, 18.50802546, 12.12977001]])
In [48]:
import seaborn as sns
In [49]:
sns.heatmap(W,square=True)
Out[49]:
<matplotlib.axes._subplots.AxesSubplot at 0x274541dfe08>
Notebook Image
In [50]:
sns.heatmap(X,square=True)
Out[50]:
<matplotlib.axes._subplots.AxesSubplot at 0x2745427bf88>
Notebook Image
In [51]:
sns.heatmap(H,square=True)
Out[51]:
<matplotlib.axes._subplots.AxesSubplot at 0x27454315dc8>
Notebook Image
In [52]:
sns.heatmap(np.dot(W,H),square=True)
Out[52]:
<matplotlib.axes._subplots.AxesSubplot at 0x274543e58c8>
Notebook Image
In [69]:
im = plt.imread("test_image.jpg")
plt.imshow(im)
Out[69]:
<matplotlib.image.AxesImage at 0x274546ba788>
Notebook Image
In [92]:
X = np.copy(im[:,:,0])
model = NMF(n_components=10, init='random', random_state=0)
W = model.fit_transform(X)
H = model.components_
In [89]:
plt.imshow(W)
Out[89]:
<matplotlib.image.AxesImage at 0x27455240608>
Notebook Image
In [90]:
plt.imshow(H)
Out[90]:
<matplotlib.image.AxesImage at 0x274550e0048>
Notebook Image
In [85]:
X.shape
Out[85]:
(159, 152)
In [86]:
W.shape
Out[86]:
(159, 2)
In [87]:
H.shape
Out[87]:
(2, 152)
In [101]:
plt.imshow(np.dot(W,H))
Out[101]:
<matplotlib.image.AxesImage at 0x27456531748>
Notebook Image