Skip to main content
 首页 » 编程设计

python之通过过滤第一列/数组创建一个新的 2 列 numpy 数组

2025年01月19日10dyllove98

我正在尝试创建一个新的二维或两列数组,其中包含第一列中的(数据值 <=20000),以及第二列中它们的关联 ID 值。从数学上讲,我正在做以下事情: 我正在从文本文件中读取数据。我正在寻找从最后一点到所有点的距离。

# ID M1 M2 M3 M4 R4 M5 R5 x y z 
10217 11.467 11.502 13.428 13.599  432.17 13.266  281.06 34972.8 42985.9 14906 
7991 11.529 11.559 13.438 13.520  435.23 13.224  272.23 8538.05 33219.8 43375.1 
2100 11.526 11.573 13.478 13.490  448.97 13.356  301.27 9371.75 13734.1 43398.6 
9467 11.557 11.621 13.481 13.537  449.99 13.367  303.67 33200.3 36008.9 12735.8 
4002 11.454 11.530 13.502 13.583  457.34 13.327  294.53 44607.2 10410.9 9090 
2971 11.475 11.563 13.506 13.558  458.77 13.391  309.43 29818.3 98.65 11718.6 
1243 11.538 11.581 13.509 13.513  459.62 13.377  306.09 16238.4 11067.9 25048 
9953 11.523 11.544 13.559 13.913  477.72 13.440  321.20 34589.6 42869 14878.6 
7411 11.547 11.576 13.610 13.658  496.81 13.479  330.96 31436 42092.8 12307.8 
1820 11.606 11.619 13.652 12.543  513.11 13.571  355.21 1758.75 15809.8 40473.6 
2792 11.647 11.679 13.744 13.877  550.82 13.643  375.38 24393 6774.8 8346.35 
510 11.687 11.717 13.771 13.810  562.27 13.642  375.14 22340.3 9316.4 13209.9 
1721 11.602 11.646 13.821 14.139  584.37 13.770  413.84 2144.95 15769.1 40470.1 

在我得到距离后,我只想从我的计算中获取距离 <=20,000 以及它们相关的 ID 列。

到目前为止,我编写了这段代码来返回计算的距离和 ID:

# Find nearest neighbors  
 
import numpy as np 
import matplotlib.pyplot as plt 
 
 
 
halo = 'nntest.txt' 
ID, m,r,x,y,z= np.loadtxt(halo, usecols=(0,6,7,8,9,10), unpack =True) 
 
 
 
# selet the last point 
m_mass = m[-1:] 
ID_mass = ID[-1:] 
r_mass = r[-1:] 
x_mass = x[-1:] 
y_mass = y[-1:] 
z_mass = z[-1:] 
 
####################################### 
#Find distance to all points from our targeted point 
nearest_neighbors = [] 
 
def neighbors(ID_mass, cx,cy,cz, ID, x, y, z): 
 
    dist = np.sqrt((cx-x)**2 + (cy-y)**2 + (cz-z)**2) 
 
    return dist, ID 
 
 
for i in range(len(ID_mass)): 
    hist = neighbors(ID_mass[i], x_mass[i], y_mass[i], z_mass[i], ID, x, y, z) 
    print hist 
 
    #print all the IDs which are associated with dist<=20000 
    if (hist[0]<=20000): 
        print ID 
    nearest_neighbors.append(hist) 
 
 
 
print nearest_neighbors 

但我在返回新数组时遇到问题,该数组将仅包含距离<=20000 和关联的 ID。如果这不是一个很好的工作示例,我提前道歉。但我将非常感谢您提出的获得所需输出的建议。

请您参考如下方法:

在您提出的问题和您提供的代码之间,我仍然不太清楚您要完成什么。但我至少可以告诉你代码中哪里有错误,也许还能给你所需的工具。

正如您现在的代码,x、y、z 都是向量。所以邻居距离计算的结果,

dist = np.sqrt((cx-x)**2 + (cy-y)**2 + (cz-z)**2) 

将是一个向量。我认为这就是您的意图,因为其他值已编入索引。但这意味着你遇到了麻烦

if (hist[0]<=20000): 
    print ID 

Numpy 会将不等式视为掩码,因此 hist[0]<=2000看起来像 [True, False, False, ...] .如果使用得当,我认为 numpy 数组掩码非常适合您想要的。 例如,您可以尝试

for i in range(len(ID_mass)): 
    hist = neighbors(ID_mass[i], x_mass[i], y_mass[i], z_mass[i], ID, x, y, z) 
    print hist 
 
    #print all the IDs which are associated with dist<=20000 
    print ID[hist[0]<=20000] 
    nearest_neighbors.extend(list(zip(hist[0][hist[0]<=20000],ID[hist[0]<=20000]))) 
 
print nearest_neighbors 

我们扩展 nearest_neighbors 列表的这一行有点乱,我可能没有完全理解你想要输出的样子。但这将生成一个元组列表,其中每个元组包含距离值和距离小于 20000 的所有情况的 ID。