切片索引如何在 numpy 数组中工作 [英] How does slice indexing work in numpy array

查看:54
本文介绍了切片索引如何在 numpy 数组中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个数组

a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

现在我有以下

row_r1 = a[1, :]
row_r2 = a[1:2, :]

print(row_r1.shape)
print(row_r2.shape)

我不明白为什么 row_r1.shape 是 (4,) 而 row_r2.shape 是 (1,4)

I don't understand why row_r1.shape is (4,) and row_r2.shape is (1,4)

它们的形状不应该都等于(4,)吗?

Shouldn't their shape all equal to (4,)?

推荐答案

我喜欢这样想.第一种方式 row[1, :], states go get me all values on row 1 like this:

I like to think of it this way. The first way row[1, :], states go get me all values on row 1 like this:

返回:数组([5, 6, 7, 8])

形状

(4,) numpy 数组中的四个值.

(4,) Four values in a numpy array.

作为第二个 row[1:2, :],states go get me a slice of data between index 1 and index 2:

Where as the second row[1:2, :], states go get me a slice of data between index 1 and index 2:

返回:

array([[5, 6, 7, 8]]) 注意:双括号

形状

(1,4) np.array 中一行的四个值.

(1,4) Four values in on one row in a np.array.

这篇关于切片索引如何在 numpy 数组中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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