kalonymus
kalonymus

Reputation: 27

custom matrix in NMF in scikit-learn

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?

Upvotes: 1

Views: 2065

Answers (1)

Vivek Kumar
Vivek Kumar

Reputation: 36619

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.

Upvotes: 2

Related Questions