Question: Write a function named cluster_membership (Z, C) that takes as input two numpy arrays, a data array Z, and an array holding the centroid

Write a function named cluster_membership (Z, C) that takes as input two 

Write a function named cluster_membership (Z, C) that takes as input two numpy arrays, a data array Z, and an array holding the centroid coordinates C, and returns as output a two dimensional numpy array of type interger, member_iter, and size n x 1 with each row indicating the cluster number k = 1,2,..., K) the data sample z,, i = 0, 1,..., n-1, belongs to. Your function should: 1. Find the number K of centroids from the size of C. 2. Find the dimensions, n X p, of the data array Z. 3. Create a one dimensional array D of size K to hold the L2 distances of each data sample z,, i = 0, 1, ..., n- 1, from each centroid. 4. Create the two dimensional array member_iter decribed above. 5. For each data sample z, i = 0, 1, ... , n - 1: A. Find the distance of the sample point to each of the centroids and store them into D. B. Use the numpy function argmin () to select the index corresponding to the minimum of those K distances and store that index into member_iter [i,0]. 6. Return the integer array member_iter. In order to verify your code, you can run the normalized, projected data stored in Z, with the centroids calculated in Problem 2A and check that the entries of the array member_iter are between 1 and 3. ]: # Enter your code here def cluster membership (Z, C): K = C.shape [0] n, p= Z.shape D = np.zeros(K, 'double') member_iter # for i in range(0,n): # for each data point Z[i,:], find for k in range (0,K): D[k] = # distance from point to centroid k (5A) above member_iter [i,0] = # index of D with smaller value +1 (5B) above return member_iter

Step by Step Solution

3.40 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres the code for the clustermembership function following the steps youve ... View full answer

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!