选择从numpy的阵列特定的行和列 [英] Selecting specific rows and columns from NumPy array

查看:537
本文介绍了选择从numpy的阵列特定的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经快疯了试图找出我在做什么愚蠢的事错在这里。

我使用numpy的,我有具体的行索引和具体列索引,我要从中选择。这里是我的问题的要点是:

 导入numpy的是NP一个= np.arange(20).reshape((5,4))
#阵列([0,1,2,3]
#[4,5,6,7],
#[8,9,10,11],
#[12,13,14,15],
#[16,17,18,19])#如果我选择的某些行,它的工作原理
打印[0,1,3]:]
#阵列([0,1,2,3]
#[4,5,6,7],
#[12,13,14,15]])#如果我选择某些行和一列,它的工作原理
打印[0,1,3] 2]
#阵列([2,6,14])#但是,如果我选择某些行和某些列,它失败
打印[[0,1,3],[0,2]
#回溯(最近通话最后一个):
#文件<&标准输入GT;,1号线,上述<&模块GT;
#ValueError错误:形状不匹配:对象不能被广播到单个形状

这是怎么回事?当然,我应该能够选择第一,第二,和第4行,第1和第3列?我期待的结果是:

  A [0,1,3],[0,2] => [[0,2],
                      [4,6]
                      [12,14]]


解决方案

花式索引需要你提供所有指数为每个维度。您提供3指数的第一个,也是唯一2第二个,因此错误。你想要做这样的事情:

 >>>一个[[[0,0],[1,1],[3,3],[0,2],[0,2],[0,2]]]
阵列([0,2],
       [4,6]
       [12,14]])

这当然是一个痛苦的写,这样可以让广播帮助您:

 >>>一个[[[0],[1],[3],[0,2]
阵列([0,2],
       [4,6]
       [12,14]])

这是更简单的做,如果你使用数组索引,而不是名单:

 >>> row_idx = np.array([0,1,3])
>>> col_idx = np.array([0,2])
>>>一个[row_idx [:,无],col_idx]
阵列([0,2],
       [4,6]
       [12,14]])

I've been going crazy trying to figure out what stupid thing I'm doing wrong here.

I'm using NumPy, and I have specific row indices and specific column indices that I want to select from. Here's the gist of my problem:

import numpy as np

a = np.arange(20).reshape((5,4))
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [12, 13, 14, 15],
#        [16, 17, 18, 19]])

# If I select certain rows, it works
print a[[0, 1, 3], :]
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [12, 13, 14, 15]])

# If I select certain rows and a single column, it works
print a[[0, 1, 3], 2]
# array([ 2,  6, 14])

# But if I select certain rows AND certain columns, it fails
print a[[0,1,3], [0,2]]
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# ValueError: shape mismatch: objects cannot be broadcast to a single shape

Why is this happening? Surely I should be able to select the 1st, 2nd, and 4th rows, and 1st and 3rd columns? The result I'm expecting is:

a[[0,1,3], [0,2]] => [[0,  2],
                      [4,  6],
                      [12, 14]]

解决方案

Fancy indexing requires you to provide all indices for each dimension. You are providing 3 indices for the first one, and only 2 for the second one, hence the error. You want to do something like this:

>>> a[[[0, 0], [1, 1], [3, 3]], [[0,2], [0,2], [0, 2]]]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

That is of course a pain to write, so you can let broadcasting help you:

>>> a[[[0], [1], [3]], [0, 2]]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

This is much simpler to do if you index with arrays, not lists:

>>> row_idx = np.array([0, 1, 3])
>>> col_idx = np.array([0, 2])
>>> a[row_idx[:, None], col_idx]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

这篇关于选择从numpy的阵列特定的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆