【发布时间】:2016-06-07 00:17:19
【问题描述】:
我有两个数组上的绘图数据,这些数组以未排序的方式存储,因此绘图不连续地从一个地方跳到另一个地方: 我试过one example of finding the closest point in a 2D array:
import numpy as np
def distance(pt_1, pt_2):
pt_1 = np.array((pt_1[0], pt_1[1]))
pt_2 = np.array((pt_2[0], pt_2[1]))
return np.linalg.norm(pt_1-pt_2)
def closest_node(node, nodes):
nodes = np.asarray(nodes)
dist_2 = np.sum((nodes - node)**2, axis=1)
return np.argmin(dist_2)
a = []
for x in range(50000):
a.append((np.random.randint(0,1000),np.random.randint(0,1000)))
some_pt = (1, 2)
closest_node(some_pt, a)
我可以用它来“清理”我的数据吗? (在上面的代码中,a 可以是我的数据)
我计算的示例数据是:
array([[ 2.08937872e+001, 1.99020033e+001, 2.28260611e+001,
6.27711094e+000, 3.30392288e+000, 1.30312878e+001,
8.80768833e+000, 1.31238275e+001, 1.57400130e+001,
5.00278061e+000, 1.70752624e+001, 1.79131456e+001,
1.50746185e+001, 2.50095731e+001, 2.15895974e+001,
1.23237801e+001, 1.14860312e+001, 1.44268222e+001,
6.37680265e+000, 7.81485403e+000],
[ -1.19702178e-001, -1.14050879e-001, -1.29711421e-001,
8.32977493e-001, 7.27437322e-001, 8.94389885e-001,
8.65931116e-001, -6.08199292e-002, -8.51922900e-002,
1.12333841e-001, -9.88131292e-324, 4.94065646e-324,
-9.88131292e-324, 4.94065646e-324, 4.94065646e-324,
0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
-4.94065646e-324, 0.00000000e+000]])
【问题讨论】:
-
能否发布您的数据或如何获取数据?
-
a不是你展示的情节 -
另外,你真的需要线图吗?你不能只绘制数据点(不画线)吗?
-
看起来您需要的只是按其
y值对数据进行排序,但发布一些示例数据会有所帮助。 -
@Ohm,我认为具有合适距离度量的最近邻方法可能是最好的解决方案,请参阅编辑。
标签: python arrays sorting numpy matplotlib