Question: Your task: Define build _ cluster _ matrix as follows: Given a k - means clustering result, construct a k - means assignment assignment matrix.

Your task: Define build_cluster_matrix as follows:
Given a k-means clustering result, construct a k-means assignment assignment matrix. From the preceeding discussion, it is R matrix.
Inputs:
*) labels: A 1-D array where labels[i] is the cluster ID assigned to document i
*)max_label: The largest cluster ID plus 1. That is,0<= labels[i]< max_label for all i
Return: R, a scipy COO matrix
Steps:
The returned matrix R should have len(labels) rows and max_label columns. It should only have entries equal to 1.0 at positions (i,j) where j == labels[i]; all other entries should be regarded as zero values and therefore not stored explicitly.
Example:
Suppose we run:
build_cluster_matrix(np.array([1,2,0,0,2,2,2,3,0,3]),4)
A correct implementation would return:
(0,1)1.0
(1,2)1.0
(2,0)1.0
(3,0)1.0
....
(9,3)1.0
Solution:
def build_cluster_matrix(labels: np.ndarray, max_label: int)-> sp.sparse.coo_matrix:
##code here
demo_R = build_cluster_matrix(np.array([1,2,0,0,2,2,2,3,0,3]),4)
print(demo_R)

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!