If we are given "starting_point"
and "list_of_points"
, how do we create a new numpy array "distances" that contains the distance between the "starting_point" and each point in "list_of_points"?
如果给出“starting_point”和“list_of_points”,我们如何创建一个新的numpy数组“距离”,其中包含“starting_point”与“list_of_points”中每个点之间的距离?
I tried to do this by looping through "list_of_points"
with the following code, but it did not work:
我尝试通过使用以下代码循环“list_of_points”来执行此操作,但它不起作用:
distances = sqrt( (list_of_points[num][0] - starting_point[0])^2 + list_of_points[num][1] - starting_point[1])^2 ) for num in range (0,4)
starting_point = np.array([1.0, 2.0])
list_of_points = np.array([-5.0, -3.0], [-4.0, 2.0], [7.0, 8.0], [6.0, -9.0])
distances = np.array([ d1 ], [ d2 ], [ d3 ], [ d4 ])
1 个解决方案
#1
0
You are on the right track with using Numpy for this. I personally found Numpy very unintuitive when I first used it, but it gets (a little) easier with practice.
你正在使用Numpy的正确轨道。我个人发现Numpy在我第一次使用它时非常不直观,但是通过练习它会变得更容易。
The basic idea is that you want to avoid loops and use vectorized operations. This allows much faster operations on large data structures.
基本思想是你想避免循环并使用矢量化操作。这允许在大型数据结构上更快地操作。
On part of vectorization is broadcasting — where Numpy can apply operations over differently shaped objects. So in this case you can subtract without looping:
矢量化的一部分是广播 - Numpy可以在不同形状的对象上应用操作。所以在这种情况下,您可以在不循环的情况下减去:
import numpy as np
starting_point = np.array([1.0, 2.0])
list_of_points = np.array([[4, 6], [5, 5], [3, 2]])
# subtract starting_point from each point in list_of_points
dif = list_of_points - starting_point
If you dig around in the docs you'll find all sorts of vectorized operations including np.linalg.norm()
(docs) which calculates different kinds of norms including distances. The trick to using this is to figure out which axis to use. I've changes the values to make it easy to confirm the correct answers:
如果你在文档中挖掘,你会发现各种矢量化操作,包括np.linalg.norm()(docs),它计算不同种类的规范,包括距离。使用它的技巧是找出要使用的轴。我更改了值以便于确认正确的答案:
import numpy as np
starting_point = np.array([1.0, 2.0])
list_of_points = np.array([[4, 6], [5, 5], [3, 2]])
np.linalg.norm(starting_point - list_of_points, axis=1)
# array([ 5., 5., 2.])
You can also do it the hard way by squaring, summing and taking the square root if you want to:
你也可以通过平方,求和和取平方根来做到这一点,如果你想:
np.sqrt(
np.sum(
np.square(list_of_points - starting_point),
axis = 1)
)
# array([ 5., 5., 2.])
#1
0
You are on the right track with using Numpy for this. I personally found Numpy very unintuitive when I first used it, but it gets (a little) easier with practice.
你正在使用Numpy的正确轨道。我个人发现Numpy在我第一次使用它时非常不直观,但是通过练习它会变得更容易。
The basic idea is that you want to avoid loops and use vectorized operations. This allows much faster operations on large data structures.
基本思想是你想避免循环并使用矢量化操作。这允许在大型数据结构上更快地操作。
On part of vectorization is broadcasting — where Numpy can apply operations over differently shaped objects. So in this case you can subtract without looping:
矢量化的一部分是广播 - Numpy可以在不同形状的对象上应用操作。所以在这种情况下,您可以在不循环的情况下减去:
import numpy as np
starting_point = np.array([1.0, 2.0])
list_of_points = np.array([[4, 6], [5, 5], [3, 2]])
# subtract starting_point from each point in list_of_points
dif = list_of_points - starting_point
If you dig around in the docs you'll find all sorts of vectorized operations including np.linalg.norm()
(docs) which calculates different kinds of norms including distances. The trick to using this is to figure out which axis to use. I've changes the values to make it easy to confirm the correct answers:
如果你在文档中挖掘,你会发现各种矢量化操作,包括np.linalg.norm()(docs),它计算不同种类的规范,包括距离。使用它的技巧是找出要使用的轴。我更改了值以便于确认正确的答案:
import numpy as np
starting_point = np.array([1.0, 2.0])
list_of_points = np.array([[4, 6], [5, 5], [3, 2]])
np.linalg.norm(starting_point - list_of_points, axis=1)
# array([ 5., 5., 2.])
You can also do it the hard way by squaring, summing and taking the square root if you want to:
你也可以通过平方,求和和取平方根来做到这一点,如果你想:
np.sqrt(
np.sum(
np.square(list_of_points - starting_point),
axis = 1)
)
# array([ 5., 5., 2.])