This question already has answers here:
Is it possible to speed up this loop in Python?
(6 answers)
Vectorized spatial distance in python using numpy
(1 answer)
Closed 3 years ago.
I think this has been asked before, but I'm trying to implement the following: I've got a list of tuples containing the 2D coordinates of the N particles that I have. I have defined a numpy.zeros((N,N)) array to store the distance between them. How could I do this the fastest?
Thanks in advance for any help! :)
Edited to add: I've already written a function to measure the distance between two tuples, and was wondering how to iterate it!
My distance measuring function:
def calc_distance(p1, p2):
distance = numpy.linalg.norm(p1 - p2)
return distance
Distance matrix is what you are looking for:
coords = [(0,0), (1,1), (3,2)]
from scipy.spatial import distance_matrix
distance_matrix(coords, coords)
Output:
array([[0. , 1.41421356, 3.60555128],
[1.41421356, 0. , 2.23606798],
[3.60555128, 2.23606798, 0. ]])
Related
This question already has answers here:
Average every x numbers in NumPy array
(4 answers)
Closed 3 years ago.
I have a long vector (50,000 datapoints)
and I want to reduce it by applying mean in chunks of 5. (So I will get vector at the size of 10,000).
For example,
if the vector is
[1,8,-1,0,2 , 6,8,11,4,6]
the output will be
[2,7]
What is the most efficient way to do so?
Thanks
When you know that the vector is always divisible by 5:
import numpy as np
vec = np.array([1,8,-1,0,2 , 6,8,11,4,6])
averaged = vec.reshape(-1, 5).mean(axis=1).flatten()
print(averaged)
Output
array([2., 7.])
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:
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
This question already has answers here:
Python: Find number of occurrences of given array within two-dimensional array
(6 answers)
Closed 9 years ago.
I have a numpy array of arrays:
qv=array([[-1.075, -1.075, -3. ],
[-1.05 , -1.075, -3. ],
[-1.025, -1.075, -3. ],
...,
[-0.975, -0.925, -2. ],
[-0.95 , -0.925, -2. ],
[-0.925, -0.925, -2. ]])
And I want to determine if an array is contained in that 2-D array and return its index.
qt=array([-1. , -1.05, -3. ])
I can convert both arrays to lists and use the list.index() function:
qlist=qv.tolist()
ql=qt.tolist()
qindex=qlist.index(ql)
But I would like to avoid doing this because I think it will be a performance hit.
This should do the trick,
import numpy as np
np.where((qv == qt).all(-1))
Or
import numpy as np
tol = 1e-8
diff = (qv - qt)
np.where((abs(diff) < tol).all(-1))
The second method might be more appropriate when floating point precision issues come into play. Also, there might be a better approach if you have many qt to test against. For example scipy.spatial.KDTree.