# 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.

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

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