Question: We use this function to pre - calculate a W for X . The code cell below loads this W: with open ( ' resource

We use this function to pre-calculate a W for X. The code cell below loads this W:
with open('resource/asnlib/publicdata/W.dill', 'rb') as fp:
W = dill.load(fp)
print (W.shape)
Output: (72282,10)
Your task: Define get_nmf_clusters as follows:
Determine cluster assignments from an NMF computation.
Return:
labels: Cluster assignments: for each row i of W, labels[i] is the cluster (column) assignment.
ids: Cluster IDs: a Numpy array containing the values from 0 to m-1
sizes: Cluster sizes: sizes[c] is the number of rows of W that are assigned to cluster c
STEPS:
*) let m be the number of clusters, which is equal to the number of columnsof W
*)For each row i of W, assign it to the cluster c having largest value of W[i, c]
*) the ids array is simply a Numpy array of length m whose values go from 0 to m-1
*)For each cluster c, count how many rows were assigned to it and store that as sizes[c].
Example: For the demo code, a correct implementation should return:
(array([1,0,2,0,2,1,3,0,0]), array([0,1,2,3]), array([4,2,2,1}))
Solution:
def get_nmf_clusters(W: np.ndarray)->(np.ndarray, np.ndarray, np.ndarray):
##code here##
with open('resource/asnlib/publicdata/demo_get_nmf_clusters_W.dill', 'rb') as fp:
demo_W= dill.load(fp)
print("demo input W:")
pprint(demo_W)
print("
Your output:")
print(get_nmf_clusters(demo_W))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!