Skip to content

Instantly share code, notes, and snippets.

@cocomoff
Created July 2, 2020 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocomoff/814a8a75adce0d0af4f58890d93d6921 to your computer and use it in GitHub Desktop.
Save cocomoff/814a8a75adce0d0af4f58890d93d6921 to your computer and use it in GitHub Desktop.
# 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