了解numpy.array的形状 [英] Understanding the shape of a numpy.array

查看:124
本文介绍了了解numpy.array的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数组 x 声明为:

x = np.array([[1, 2], [3, 4]])

x 的形状为(2,2),因为它是2x2的矩阵.

the shape of x is (2, 2) because it is a 2x2 matrix.

但是,对于一维矢量,例如:

However, for a 1-dimensional vector, such as:

x = np.array([1, 2, 3])

为什么 x 的形状给出的是(3,)而不是(1,3)?

why does the shape of x gives (3,) and not (1,3)?

将形状理解为(行,列)是我的错误吗?

Is it my mistake to understand the shape as (row, column)?

推荐答案

因为 np.array([1,2,3])是一维数组.(3,)表示这是具有三个元素的一维.

Because np.array([1,2,3]) is one-dimensional array. (3,) means that this is single dimension with three elements.

(1,3)表示这是一个二维数组.如果在数组上使用 reshape()方法,并为其提供参数(1,3),则会在其中添加其他括号.

(1,3) means that this is a two-dimensional array. If you use reshape() method on the array, and give it arguments (1,3), additional brackets will be added to it.

>>> np.array([1,2,3]).reshape(1,3)
array([[1, 2, 3]])

这篇关于了解numpy.array的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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