首页 > 解决方案 > 如何在对其应用精美的索引过滤器的同时将数组的一列放入一个新的数组中?

问题描述

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

我对 Python 很陌生。正如我上面提到的,我认为我知道我必须做什么[使用精美的索引过滤器],但我不知道我该怎么做。我尝试在原始数组中执行此操作,但没有成功,因此我认为我需要将信息放入另一个数组并在该数组上使用布尔过滤器。

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

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

标签: pythonarraysnumpy

解决方案


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

#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]

推荐阅读