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

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

问题描述

我一直在发疯,试图弄清楚我在这里做错了什么愚蠢的事情.

我正在使用 NumPy,并且我有要从中选择的特定行索引和特定列索引.这是我的问题的要点:

将 numpy 导入为 npa = 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]]# 回溯(最近一次调用最后一次):# 文件<stdin>",第 1 行,在 <module># ValueError:形状不匹配:对象不能广播到单个形状

为什么会这样?我当然应该能够选择第 1、第 2 和第 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])>>>a[row_idx[:, None], 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天全站免登陆