Covariance_matrix_from_examples

I’ve been looking through other threads and have what I think to be a solution based on other tips it am still getting an error.

covary=np.cov(examples.reshape((-1, n_features)), rowvar=False)

return covary

ValueError: operands could not be broadcast together with shapes (40,40) (4,4)

I’m not sure where these numbers are coming from

Hi Mitch,

I’m having trouble accessing the notebook right now, but we could try though experiments until later.

broadcasting is a way that numpy attempts to repeat rows/columns of a matrix in order to be compatible doing numerical operations against a matrix of a different size. It was not able to do this for your particular data.

Here is a good article regarding this kind of issue.

If you still have an issue after reviewing this article, let’s chat more!

Is n_features a local variable in your covariance_from_examples function? If not, that would be a problem. In my solution, I just used the appropriate dimension of the input examples tensor as the number of features. Referencing global variables from the local scope of a function is never a good idea at least the way they structure things in these courses.

Update: I checked my version of this notebook and there is a global variable n_features which happens to have the value 40, whereas the test cases for this function have 4 features. Hmmm. :nerd_face: