简体   繁体   English

如何在Python中解释数组的形状?

[英]How to intepret the shape of the array in Python?

I am using a package and it is returning me an array. 我正在使用一个包,它将返回一个数组。 When I print the shape it is (38845,) . 当我打印形状时是(38845,) Just wondering why this ','. 只是想知道为什么这个','。

I am wondering how to interpret this. 我想知道如何解释这一点。

Thanks. 谢谢。

Python has tuples, which are like lists but of fixed size. Python有元组,类似于列表,但大小固定。 A two-element tuple is (a, b) ; 二元组是(a, b) ; a three-element one is (a, b, c) . 一个三元素的是(a, b, c) However, (a) is just a in parentheses. 然而, (a)仅仅是a在括号中。 To represent a one-element tuple, Python uses a slightly odd syntax of (a,) . 为了表示一个元素的元组,Python使用(a,)语法有点奇怪。 So there is only one dimension, and you have a bunch of elements in that one dimension. 因此只有一个维度,并且在那个维度中您有一堆元素。

It sounds like you're using Numpy. 听起来您正在使用Numpy。 If so, the shape (38845,) means you have a 1-dimensional array, of size 38845. 如果是这样,则形状(38845,)表示您具有一维数组,大小为38845。

It seems you're talking of a Numpy array. 看来您在谈论Numpy数组。

shape returns a tuple with the same size as the number of dimensions of the array. shape返回一个与数组维数大小相同的元组。 Each value of the tuple is the size of the array along the corresponding dimensions, or, as the tutorial says: 元组的每个值都是沿着相应维度的数组大小,或者,如本教程所述

An array has a shape given by the number of elements along each axis. 数组的形状由沿每个轴的元素数确定。

Here you have a 1D-array (as indicated with a 1-element tuple notation, with the coma (as @Amadan) said), and the size of the 1st (and only dimension) is 38845. For example (3,4) would be a 2D-array of size 3 for the 1st dimension and 4 for the second. 在这里,您有一个1D数组(用1元素元组表示法表示,带有逗号(如@Amadan)),并且第1个数组的大小(也是唯一的维度)是38845。例如(3,4)将是一个二维数组,第一个尺寸为3,第二个尺寸为4。

You can check the documentation for shape here: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html 您可以在此处查看shape文档: http : //docs.scipy.org/doc/numpy/reference/genic/numpy.ndarray.shape.html

Just wondering why this ','. 只是想知道为什么这个','。

Because (38845) is the same thing as 38845 , but a tuple is expected here, not an int (since in general, your array could have multiple dimensions). 由于(38845)是同样的事情38845 ,但一个元组在这里预期,而不是一个int(因为一般来说,你的阵列可以有多个维度)。 (38845,) is a 1-tuple. (38845,)是1元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM