Error - Calculating Euclidean distance for PCA in python Error - Calculating Euclidean distance for PCA in python numpy numpy

Error - Calculating Euclidean distance for PCA in python


I think you want to change the line where you calculate d to something like this:

#Step 10: Euclidean distanced = np.sqrt(np.sum(np.asarray(w - w_in)**2, axis=1)

This gives you a list of length M (number of training images) of the squared, summed, rooted distances between each images pixels. I believe that you don't want the matrix product, you want the elementwise square of each value, hence the np.asarray to make it not a matrix. This gives you the 'euclidean' difference between w_in and each of the w matrices.


When you go (w - w_in), the result is not a square matrix. To multiply a matrix by itself it must be square (that's just a property of matrix multiplication). So either you've constructed your w and w_in matrices wrong, or what you actually meant to do is square each element in the matrix (w - w_in) which is a different operation. Search for element-wise multiplication to find the numpy syntax.