【问题标题】:Creating a new 2 column numpy array from filtering through the first coumn/array通过过滤第一列/数组创建一个新的 2 列 numpy 数组
【发布时间】:2018-10-04 05:22:29
【问题描述】:

我正在尝试创建一个新的二维或 2 列数组,该数组将由第一列中的(数据值

# 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

获得距离后,我只想从计算中获取距离

到目前为止,我编写了这段代码来返回计算出的距离和 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

但我在返回新数组时遇到问题,该数组仅包含距离

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

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

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

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

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

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

    Numpy 会将不等式视为掩码,因此 hist[0]&lt;=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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多