Created
July 2, 2020 01:39
-
-
Save cocomoff/814a8a75adce0d0af4f58890d93d6921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see https://qiita.com/takechanman1228/items/6d1f65f94f7aaa016377 | |
from sklearn.decomposition import NMF | |
import numpy as np | |
R = np.array([ | |
[5, 3, 0, 1], | |
[4, 0, 0, 1], | |
[1, 1, 0, 5], | |
[1, 0, 0, 4], | |
[0, 1, 5, 4] | |
]) | |
for k in range(1, 4): | |
model = NMF(n_components=k, init='random', random_state=0) | |
P = model.fit_transform(R) | |
Q = model.components_ | |
print("****************************") | |
print("k:",k) | |
print("Pは") | |
print(P) | |
print("Q^Tは") | |
print(Q) | |
print("P×Q^Tは") | |
print(np.dot(P,Q)) | |
print("R-P×Q^Tは") | |
print(model.reconstruction_err_) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment