1

I've to do NMF with sklearn, I've used the instructions here: http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.NMF.html

I want to add my initialized matrix H, there is option to do init='custom' but I don't know how to give him the matrix H. I've tried:

model = NMF(n_components=2, init='custom',H=myInitializationH random_state=0);

but it doesn't work.

In addition, is someone know how to fix my matrix and update only W?

edit:

Thanks for the answer

When I choose the custom option, I get the error:

ValueError: input contains nan infinity or a value too large for dtype('float64')

However, the matrix don't contains any nan or infinity. Moreover, I did it for very small matrix to see if it is fine and its not:

import numpy as np
from sklearn.decomposition import NMF

x=np.ones((2,3));
#model = NMF(n_components=1, init='custom', solver='mu',beta_loss=1,max_iter=500,random_state=0,alpha=0,verbose=0, shuffle=False);
model = NMF(n_components=1, init='custom');
fixed_W = model.fit_transform(x,H=np.ones((1,3)));
fixed_H = model.components_;

print(np.matmul(fixed_W,fixed_H));

I got the same error unless I do 'random' instead of 'custom'.

Is it happen also to you? why is it happen?

kalonymus
  • 27
  • 5

1 Answers1

2

Pass the W and H in fit() or fit_transform().

As per the documentation of fit_transform():-

W : array-like, shape (n_samples, n_components)
    If init=’custom’, it is used as initial guess for the solution.

H : array-like, shape (n_components, n_features)
    If init=’custom’, it is used as initial guess for the solution.

Same applies for fit().

Do something like:

model.fit(X, H=myInitializationH, W=myInitializationW)

Update: Seems like if you pass the init='custom' param, you need to supply both W and H. If you provide H and not W, it will be taken as None, and then throw an error.

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
  • @kalonymus That depends on your data. Is your data correct. According to the error, it contains some missing info or some infinite values. – Vivek Kumar Apr 16 '18 at 10:04
  • the data is correct, I did test case with 3X2 matrix only with only 1 component and it is still have problem. please see the edited post – kalonymus Apr 16 '18 at 10:08
  • @kalonymus Please accept the answer if it helped you. – Vivek Kumar Apr 16 '18 at 10:34
  • I did. I've also voted for you but it is not publicly displayed because this is new user and I dont have enough reputations. Anyway, you are totaly deserve it. Thank you! – kalonymus Apr 16 '18 at 13:27