Multiplying an array by a designated row vector of another matrix

189 views Asked by At

Good afternoon all relatively simple`question here from a mechanical standpoint.

I'm currently performing PCA and have successfully written a code that computes the covariance matrix and correlation matrix, and the associated eigenspectrum.

Now, I have created an array that represents the eigenvectors row wise, and i would like to compute the transformation C*v^t, where c is the observation matrix and v^t is the element wise entries of the eigen vector transposed.

Now, since some of these matrices are pretty big-i'd like to be able to tell python which row of the eigenvector matrix to mulitply C by. So far I have tried some of the numpy functions, but to no avail.

(for those of you wondering, i don't want to compute the matrix product of all the eigen vecotrs, i only need to multiply by a small subset of them-the ones associated with the largest eigenvalues)

Thanks!

1

There are 1 answers

1
wim On

To "slice" a vector of row n out of 2-dimensional array A, you use a syntax like A[n]. If it's slicing columns you wanted instead, the syntax is A[:,n].

For transformations with numpy arrays and vectors, the syntax is with matrix multiplication operator:

>>> A = np.array([[0, -1], [1, 0]])
>>> vs = np.array([[1, 2], [3, 4]])
>>> A @ vs[0]  # this is a rotation of the first row of vs by A
array([-2,  1])
>>> A @ vs[1]  # this is a rotation of second row of vs by A
array([-4,  3])

Note: If you're on older python version (< 3.5), you might not have @ available yet. Then you'll have to use a function np.dot(array, vector) instead of the operator.