This question already has answers here:
Vectorized way of calculating row-wise dot product two matrices with Scipy
(5 answers)
Closed 6 years ago.
We are currently working on a python project and have to vectorize a lot due to performance constraints. We end up with the following calculation: We have two numpy arrays of shape (20,6) and want to calculate the pairwise dot product of the rows, i.e. we should obtain a (20,1) matrix in the end, where each row is the scalar obtained by the respective vector dot multiplication.
You can multiply the two arrays element wise and then do sum by rows, and then you have an array where each element is a dot product from rows of the two original arrays:
a = np.array([[1,2], [3,4]])
b = np.array([[3,4], [2,1]])
(a * b).sum(axis=1)
# array([11, 10])
Related
This question already has answers here:
How to get the element-wise mean of an ndarray
(2 answers)
Closed 3 years ago.
I have 'n' numpy arrays each with shape (128,)
How to get an average numpy array of shape (128,) for the list of numpy arrays.
I have seen the documentation of numpy's average() and mean() which describes that the average is calculated for all the elements in a single numpy array rather than multiple or list of numpy arrays.
Example
numpyArrayList = [ar1,ar2,ar3,ar4...arn]
avgNumpyArray = avg(numpyArrayList)
avgNumpyArray.shape
should give result as (128,)
and this array should contain the average of all the numpy arrays
Thanks in advance
I would use np.mean([ar1,ar2,ar3,ar4...arn], axis=0).
You can achieve this by using the following code
ar = [ar1,ar2,ar3,...,arn]
r = np.mean(ar)
for axis=0 use following
r = np.mean(ar, axis=0)
for axis=1 use following
r = np.mean(ar, axis=1)
something like?
mean=0
n=len(numpyArrayList)
for i in numpyArrayList:
mean += i.sum()/(128.*n)
Edit: misunderstood the question, sry
This question already has an answer here:
numpy matrix vector multiplication [duplicate]
(1 answer)
Closed 4 years ago.
Hi for my code I have to multiply a point/vector (1,0) by matrix [1.00583, -0.087156], [0.087156, 1.00583]. The result should give me a new point (x,y)
This is what I have so far:
import matplotlib.pyplot as plt
import numpy as np
A = np.array([[1],[0]])
B = np.array([[1.00583, -0.087156], [0.087156, 1.00583]])
test =np.multiply(A, B)
print (test)
The result still gives me a (2x2) matrix instead of a (2x1) that i can use as a point. Is there another function or a better way of going about this?
First thing, if you want to do matrix multiplication use numpy.matmul or the # operator, e.g. B#A.
Also, when you define A like
A = np.array([[1],[0]])
this creates a 2x1 vector (not 1x2). So if you want to multiply the vector A with the matrix B (2x2) this should be C = B*A, where C will be a 2x1 vector
C = B#A
Otherwise if you want to multiply A*B and B is still the 2x2 matrix you should define A as a 1x2 vector:
A = np.array([1,0])
and get a 1x2 result with
C = A#B
test =np.matmul(B,A)
This should do the trick.
This question already has answers here:
Multiplying 3D matrix with 2D matrix
(1 answer)
filtering a 3D numpy array according to 2D numpy array
(2 answers)
Closed 4 years ago.
I am standing in front of following problem: I have a stack of images - shape is (x, y, N), and I want to multiply it fast by a 2D array - shape(x, y).
The only way I came up with is to interate through elements of the 2D array and multiply it :
3D[id_x, id_y,:]=2D[id_x, id_y]*3D[id_x, id_y,:].
But for bigger arrays (and this is my case) it would not be time efficient. So here comes by question, do you how to do it without iteration?
This question already has answers here:
Vectorized NumPy linspace for multiple start and stop values
(4 answers)
Closed 6 years ago.
Is there a vectorized assigment of elements to sequences in numpy like in this discussion?
for instance:
xx = np.array([1,2], dtype=object)
expanded = np.arange(xx, xx+2)
instead of loops:
xx = np.array([1,2], dtype=object)
expanded = np.array([np.arange(x, x+2) for x in xx]).flatten()
This would be for mapping a scalar heuristic to the neighboring cells in a matrix that determined it (e.g. the range of cells that had the peak overlap from a correlation() operation).
Like this?
>>> xx = np.array([3,8,19])
>>> (xx[:,None]+np.arange(2)[None,:]).flatten()
array([ 3, 4, 8, 9, 19, 20])
The xx[:,None] operation turns the length n vector into an nx1 matrix, and the np.arange(2)[None,:]) operation creates a length 1x2 matrix containing [0., 1.]. Added together using array broadcasting gives an nx2 matrix, which is then flattened into a length 2n vector.
This question already has answers here:
How to normalize a 2-dimensional numpy array in python less verbose?
(12 answers)
Closed 8 years ago.
For example, to normalize each row in a 2-dimensional vector such that the magnitude of a row is one:
import numpy as np
a = np.arange(0,27,3).reshape(3,3)
result = a / norm_of_rows( a )
Such that:
np.sum( result**2, axis=-1 )
# array([ 1., 1., 1.])
The original question, How to normalize a 2-dimensional numpy array in python less verbose?, which people feel my question is a duplicate of, the author actually asks how to make the elements of each row sum to one. This is different than normalizing each row such that its magnitude is one (the sum of the square of each element equals one).
np.max(a[0,:]) will give you the maximum of the 1st row,
np.max(a[1,:]) will give you the maximum of the 2nd row
To normalize the whole matrix just loop through your rows and divide each element by the corresponding max_row