python - Numpy 矩阵索引等价

标签 python numpy indexing

我有一个矩阵mat:

 array([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10],
        [11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20],
        [21, 22, 23, 24, 25]])

我想知道为什么 mat[:3,1:2] 是:

 array([[ 2],
        [ 7],
        [12]])`

但是 mat[:3,1] 是:

 array([ 2, 7, 12])

最佳答案

An integer, i, returns the same values as i:i+1 except the dimensionality of the returned object is reduced by 1. In particular, a selection tuple with the p-th element an integer (and all other entries :) returns the corresponding sub-array with dimension N - 1. If N = 1 then the returned object is an array scalar. These objects are explained in Scalars. https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html

谢谢@Kasramvd

关于python - Numpy 矩阵索引等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49981791/

相关文章:

python - 使用 Python 请求从本地 url 获取文件?

python - 自定义属性的ListProperty

python - 从总值最高的 2 个数组中从 N 个数字中选择 k 个

python - 如何在 python numpy 中并行化求和计算?

python - numpy.r_ 中的字符串是什么意思?

python - opencv在numpy矢量化后显示黑色图像

javascript - 通过索引jquery获取数组值

matlab - 在matlab中对列和行使用查找函数

python - 是否有任何命令可以使用 kaggle API 从 Kaggle Competition 的特定文件夹下载数据

python - 如何在 Numpy 中使用基于列表的索引的附加赋值