Stuck with dimensions on compute_pca

Hello All,

Good afternoon!

I am stuck on this exercise with the below:

print(eigen_vecs_subset.shape) ---> (3, 2)
print(X_demeaned.shape)          ---> (3, 10)

Then when X_reduced returns an error of:

ValueError: operands could not be broadcast together with shapes (3,2) (3,10)

Any tips?

Thanks,
Carlos.

Hi @cserranobr,

Can you share your lab ID with me ? In the assignment, when you click the top right “Help” button, a panel will open and your lab ID will be shown at the bottom.

I shall take a look.

When you reply back, kindly tag me in the post so that I’m notified.

Thanks,
Mubsi

@cserranobr I too faced same situation. The shape of vectors at various stages should be as follows:

X_meaned.shape: (3, 10)
cov_mat.shape: (10, 10)
eigen_values.shape: (10,)
eigen_vectors.shape: (10, 10)
idx.shape: (10,)
sorted_index.shape: (10,)
sorted_eigenvalue.shape: (10,)
sorted_eigenvectors.shape: (10, 10)
eigenvector_subset.shape: (10, 2)
X_reduced.shape: (3, 2)

3 Likes

Txs!

ahwwsnox

@Mubsi

Hi Shantimohan_Elchuri,

I guess the problem is below:

covariance_matrix = np.cov(X)
print(covariance_matrix.shape) --> (3, 3)

Am I missing any argument with np.cov?

Txs

Hi All,

I was able to fix the dimensions using rowvar=False for covariance matrix. The dimensions are now the same of what Shantimohan_Elchuri shared.

But now I get the below when running w3_unittest.test_compute_pca(compute_pca):

Wrong output shape. Check if you are taking the proper number of dimensions.
	Expected: [[ 0.43437323  0.49820384]
 [ 0.42077249 -0.50351448]
 [-0.85514571  0.00531064]].
	Got: [[ 0.31677367  0.10374636]
 [ 0.01082884  0.14382224]
 [ 0.48585945 -0.40713217]].

and:

Wrong output shape. Check if you are taking the proper number of dimensions.
	Expected: [[-0.32462796  0.01881248 -0.51389463]
 [-0.36781354  0.88364184  0.05985815]
 [-0.75767901 -0.69452194  0.12223214]
 [ 1.01698298 -0.17990871 -0.33555475]
 [ 0.43313753 -0.02802368  0.66735909]].
	Got: [[-0.12207901 -0.34370828 -0.06546364]
 [ 0.35704706 -0.16584007  0.62461965]
 [-0.02652169  0.19987506 -0.49724209]
 [ 0.09110832  0.33947962  0.48197534]
 [ 0.40133806  0.06503059  0.19160542]].
 4  Tests passed
 2  Tests failed

Thanks

1 Like

Bump, I’m stuck on the same exact thing as Carlos.

@cserranobr Check if you sorting eigen_vecs also in the decresing index like eigen_vals and set to eigen_vecs_sorted? I don’t see any other you got those wrong numbers.

Hello,
I’m facing the same problem as @cserranobr. The shape seems to be correct but the values seem to be wrong. However, when I continue the plot seems to be ok. Is there a problem with my calculation of X_demeaned:

(code snippet removed)

Also my final calculation is:

(code snippet removed)

Thanks.

Hi Reghu,

For the assignment you just have to demean the data not standardize the variance. So you don’t have to divide by the standard deviation.

1 Like

Hi @cserranobr,

I’m facing the same issue, and I’ve followed all solutions in this post. I’m at the same stage here, passed 4 tests but failed 2. Were you able to resolve the issue? Do you mind sharing if you did?

Thanks

Hi @cserranobr @Tejas_Joshi ,

Having the same issue…4/6 tests passed. Different values. The shapes look correct but the test says otherwise.

My lab ID is fjwkvdps

Hi.

Make sure, when you sort the eigen_vecs using the idx_sorted_decreasing indices to sort by column, not by row.

5 Likes

Tried this. 4 passed, 2 failed, yet the same.
I can’t seem to understand what’s the problem.

Do you mind having a look? My Lab ID: zxfvgfon

Your help is appreciated, thanks a lot.

Okay, I am stuck here as well; 4 passed, 2 failed.
The correct shape but wrong expected values.
I played around with the sort order of the vecs and that didn’t work.
I put the original vals in Excel and sorted them.
They were in order, so when I reversed the order, it went from 9 to 0.
Is this a clue, were the vals already in ascending order?
So when I sort the vecs, it is just putting them in reverse order.
All the shapes are correct, so could it be that my covariance matrix is not working?
I don’t see any suggestions other than sorting the vecs correctly.
I followed the hints eigen_vecs[:,idx_sorted_decreasing]

I found my mistake. The directions said use np.mean(a, axis=None) and copied that exactly.
I failed to continue reading where it said to set axis to = 0.
FAIL!

4 Likes

Hi:

I am having similar problems getting X_reduced. I have read the related threads. My X_demeaned.T and eigen_vecs_reduced.T can’t be multipled: the shapes aren’t compatible. My lab id is isitxkmp

Thanks in advance,
Drew

Hi! I have the same error, could I please ask for help? For demeaning I used the axis = 1 means only (no stdev, as had been suggested in the slides); for eigh, I used rowvar = False; all the dimensions are logical and match throughout, but the final numbers do not match. Here is my lab_id, with kind thanks: pvmqenzc

May I copy the code here, for you to look at?

I’m also stuck at the 4 tests passing 2 tests failing stage, after following everyone’s advice. Obviously the plots don’t look like they’re supposed to. I’m frustrated by this exercise - lots of transformatory steps are given without any explanation or clues. If we’re not going to go in depth on the PCA algorithm, I’d much rather work with the library, like in the example lab in the lecture track. My assignment ID is sqvzibve if anyone would be kind enough to take a look. Grader passes it at 80%, I may come back to it later, but moving forward for now.

2 Likes