Skip to main content
 首页 » 编程设计

python之如何在对数组应用花式索引过滤器的同时将数组的一列放入新数组中

2024年11月24日9linjiqin

所以基本上我有一个数组,由 14 列和 426 行组成,每一列代表一只狗的一个属性,每一行代表一只狗,现在我想知道一只生病的狗的平均心脏频率,即 14 . column 是表示Dog 是否生病的列[0 = Healthy 1 = ill],8. row 是心脏频率。现在我的问题是,我不知道如何从整个数组中获取第 8 列并对其使用 bool 过滤器

我是 Python 的新手。正如我上面提到的,我认为我知道我必须做什么 [使用一个花哨的索引过滤器],但我不知道我该怎么做。我尝试在仍处于原始数组中时执行此操作,但没有成功,因此我认为我需要将信息放入另一个数组中并在该数组上使用 bool 过滤器。

编辑:好的,这是我现在得到的代码:

import numpy as np 
 
def average_heart_rate_for_pathologic_group(D): 
 
    a=np.array(D[:, 13])    #gets information, wether the dogs are sick or not 
    b=np.array(D[:, 7])     #gets the heartfrequency 
    R=(a >= 0)              #gets all the values that are from sick dogs 
    amhr = np.mean(R)       #calculates the average heartfrequency 
    return amhr 

请您参考如下方法:

我认为 bool 索引是前进的方向。 这项工作的快捷方式如下:

#Your data: 
data = [[0,1,2,3,4,5,6,7,8...],[..]...] 
#This indexing chooses the rows in the 8th column that equals 1 and then their 
#column number 14 values. Any analysis can be done after this on the new variable 
heart_frequency_ill = data[data[:,7] == 1,13]