直观地解释此4D numpy数组索引 [英] Explain this 4D numpy array indexing intuitively

查看:87
本文介绍了直观地解释此4D numpy数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

x = np.random.randn(4, 3, 3, 2)
print(x[1,1])

output:
[[ 1.68158825 -0.03701415]
[ 1.0907524  -1.94530359]
[ 0.25659178  0.00475093]]

我是python新手.我真的不能理解上面的4维数组索引. x [1,1]是什么意思?

I am python newbie. I can't really understand 4-D array index like above. What does x[1,1] mean?

例如,对于向量

a = [[2][3][8][9]], a[0 = 2, a[3] = 9. 

我明白了,但我不知道x [1,1]指的是什么.

I get this but I don't know what x[1,1] refers to.

请详细说明.谢谢.

推荐答案

2D数组是矩阵:数组的数组.

A 2D array is a matrix : an array of arrays.

4D数组基本上是矩阵矩阵:

A 4D array is basically a matrix of matrices:

指定一个索引可为您提供一系列矩阵:

Specifying one index gives you an array of matrices:

>>> x[1]
array([[[-0.37387191, -0.19582887],
        [-2.88810217, -0.8249608 ],
        [-0.46763329,  1.18628611]],

       [[-1.52766397, -0.2922034 ],
        [ 0.27643125, -0.87816021],
        [-0.49936658,  0.84011388]],

       [[ 0.41885001,  0.16037164],
        [ 1.21510322,  0.01923682],
        [ 0.96039904, -0.22761806]]])

指定两个索引将为您提供一个矩阵:

Specifying two indices gives you a matrix:

>>> x[1, 1]
array([[-1.52766397, -0.2922034 ],
       [ 0.27643125, -0.87816021],
       [-0.49936658,  0.84011388]])

指定三个索引可为您提供一个数组:

Specifying three indices gives you an array:

>>> x[1, 1, 1]
array([ 0.27643125, -0.87816021])

指定四个索引可为您提供一个元素:

Specifying four indices gives you a single element:

>>> x[1, 1, 1, 1]
-0.87816021212791107

x[1,1]为您提供了保存在大矩阵第二行第二列中的小矩阵.

x[1,1] gives you the small matrix that was saved in the 2nd column of the 2nd row of the large matrix.

这篇关于直观地解释此4D numpy数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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