C2W2 Assignment get_top_covariances unit test

Hi I was trying to use the helper functions to get the top covariances. But while doing so I get the following error
ValueError: operands could not be broadcast together with shapes (3,4) (3,)

As the shape of my highest covariance matrix is (3,4) and the corresponding vector for subtracting is [5,4,2] assuming it gets a shape of (3,).

I have followed the following steps to :

covariance_matrix = covariance_matrix_from_examples(classification_changes.numpy())
 top_magnitude_indices = get_top_magnitude_indices(covariance_matrix[target_index])
relevant_indices = top_magnitude_indices[1:top_n + 1]
ind =  remove_from_list(relevant_indices, target_index) highest_covariances = covariance_matrix[ind]

I get the relevant indices as (2,3,0 ) which is as expected for the assertion.

I will remove the code for futurte.

The first couple of lines look fine, but things go off the rails after that. You want to use the remove_from_list function directly, rather than arbitrarily “peeling off” the first layer. How do you know the first entry is the one to be removed? That’s the whole point of remove_from_list, right?

Thank you for pointing that out. Yeah makes sense use simply remove_from_list to remove the indices rather than removing them manually.
I have updated it to

covariance_matrix = covariance_matrix_from_examples
    
top_magnitude_indices = get_top_magnitude_indices
        
relevant_indices =  remove_from_list

However it still cannot broadcast as the highest covariance matrix has a shape of (3,4) and the matrix being compared to it in the assertions has a shape of (3,)

You’re still missing a few steps. After the remove from list, you then index off the top_n values from the remaining list. Then you need to select the columns from the covariance matrix corresponding to the target index and the relevant indices.

I added some print statements in my code to show the shapes of various things, although I used different variable names. Here’s what I see with code that passes the tests:

covary.shape = (4, 4)
covary =
[[ 9.99769502  2.14481113 -0.44831385 -4.88043127]
 [ 2.14481113 11.085514    5.0194733   4.03402515]
 [-0.44831385  5.0194733  10.10289731  1.97473644]
 [-4.88043127  4.03402515  1.97473644 10.98517752]]
values [ 2.14481113 11.085514    5.0194733   4.03402515]
top_indices = [1 2 3 0]
all_indices.shape = (4,)
relevant_indices.shape = (3,)
highest_cov.shape = (3,)
relevant_indices [2 3 0]
highest_covariances [5.0194733  4.03402515 2.14481113]
covary.shape = (4, 4)
covary =
[[ 9.99769502  2.14481113 -0.44831385 -4.88043127]
 [ 2.14481113 11.085514    5.0194733   4.03402515]
 [-0.44831385  5.0194733  10.10289731  1.97473644]
 [-4.88043127  4.03402515  1.97473644 10.98517752]]
values [ 9.99769502  2.14481113 -0.44831385 -4.88043127]
top_indices = [0 3 1 2]
all_indices.shape = (4,)
relevant_indices.shape = (3,)
highest_cov.shape = (3,)
relevant_indices [3 1 2]
highest_covariances [-4.88043127  2.14481113 -0.44831385]
covary.shape = (4, 4)
covary =
[[ 9.99769502  2.14481113 -0.44831385 -4.88043127]
 [ 2.14481113 11.085514    5.0194733   4.03402515]
 [-0.44831385  5.0194733  10.10289731  1.97473644]
 [-4.88043127  4.03402515  1.97473644 10.98517752]]
values [-0.44831385  5.0194733  10.10289731  1.97473644]
top_indices = [2 1 3 0]
all_indices.shape = (4,)
relevant_indices.shape = (2,)
highest_cov.shape = (2,)
relevant_indices [1 3]
highest_covariances [5.0194733  1.97473644]
covary.shape = (4, 4)
covary =
[[ 9.99769502  2.14481113 -0.44831385 -4.88043127]
 [ 2.14481113 11.085514    5.0194733   4.03402515]
 [-0.44831385  5.0194733  10.10289731  1.97473644]
 [-4.88043127  4.03402515  1.97473644 10.98517752]]
values [-4.88043127  4.03402515  1.97473644 10.98517752]
top_indices = [3 0 1 2]
all_indices.shape = (4,)
relevant_indices.shape = (2,)
highest_cov.shape = (2,)
relevant_indices [0 1]
highest_covariances [-4.88043127  4.03402515]
All tests passed
1 Like

Yeah found my mistakes. Thank you so much. I realized I wasn’t selecting the one with the highest covariance before selecting the relevant indices. Makes sense now.
Thank you so much. It works now.

That’s great. Onward! :nerd_face: