python - 如何在对其应用精美的索引过滤器的同时将数组的一列放入一个新的数组中?
问题描述
所以基本上我有一个数组,它由 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
解决方案
我认为布尔索引是前进的方向。这项工作的快捷方式如下:
#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]
推荐阅读
- javascript - 为什么带数字的键出现在以下对象中的字母之前?
- javascript - 我能找出这个错误的真正原因吗?(反应原生)
- c - 找不到发生分段错误的原因
- javascript - Javascript (extjs) : 只选中两个中的一个复选框
- reactjs - 在 Kubernetes Ingress 后面使用 HTTPS 和后端 API 运行 ReactJS 应用程序
- node.js - 动态codegen和静态codegen的包导入路径不同
- r - 自以来的最高值 - 在 R 中找到区间
- wordpress - 在 WordPress 中将 Elementor 工具用于默认主页的问题
- signal-processing - LP 阶数和采样频率对语音共振峰的影响
- javascript - 反应功能自定义按钮未注册点击