【问题标题】:Group numpy matrix based on value of particular column, only on row indices from given array根据特定列的值对 numpy 矩阵进行分组,仅基于给定数组的行索引
【发布时间】:2021-04-22 10:04:04
【问题描述】:

我有一个由二进制值组成的 numpy 矩阵。我也有一个行索引列表。现在我必须从特定列的值为 1 的矩阵中获取索引,并且索引必须包含在行索引列表中。什么是这样做的有效方法?我目前正在做:

result = [index for index in np.where(dataset[:, col] == 1)[0] if index in indices]

【问题讨论】:

    标签: python list numpy matrix numpy-slicing


    【解决方案1】:

    也许循环遍历行索引列表会更快,因为您不需要遍历矩阵的所有行:

    result = [index for index in indices if dataset[index, col] == 1]
    

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2013-02-16
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      • 2014-08-11
      相关资源
      最近更新 更多