Shape relates to the size of the dimensions of an N-dimensional array.

Size regarding arrays, relates to the amount (or count) of elements that are contained in the array (or sometimes, at the top dimension of the array - when used as length).

For example, let a be a matrix

1  2  3  4
5  6  7  8
9 10 11 12

the shape of a is (3, 4), the size of a is 12 and the size of a[1] is 4.

Answer from Uriel on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what's the difference between a numpy array with shape (x,) and (x, 1)?
r/learnpython on Reddit: what's the difference between a numpy array with shape (x,) and (x, 1)?
October 6, 2023 -

hey, im doing an ai thing in school and my code didnt work as expected, and after 5 hours i found out i reshaped an array from (206,) to (206,1) and that made the results wrong. and from what i understand, the shape means the length of each dimension, and length is not 0 indexed so a size of 1 would be equal to just 1D no?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ why is numpy inconsistent with size vs shape?
r/learnpython on Reddit: Why is NumPy inconsistent with size vs shape?
July 17, 2018 -

Is there a reasoning (besides legacy) that NumPy is inconsistent with "size" and "shape"? Or, am I missing some underlying logic.

For example, If I have a multi-dimensional array X and I want to make a random array the same dimensions, I would do:

np.random.uniform(size=X.shape)

But conversely, if you look at the signature of np.ones, it calles the first argument shape. I do not know about you, I but often get confused if the keyword is size or shape for any given function.

And then there is the easy-to-mess-up np.array.resize (in-place) and np.array.reshape (returns an array). And there is the non-object oriented version np.reshape which again returns an array but there is no np.resize that would modify the argument in-place.

(Personally, I do not like when functions modify an argument in place such as random.shuffle. A method should be the main way. But that is a different story)

Is there an underlying logic? Way to keep it straight? Reasoning?

Thanks!

Top answer
1 of 2
1

Personally, I do not like when functions modify an argument in place such as random.shuffle. A method should be the main way.

which would necessarily require that a run of the mill list or any other mutable container maintaining order has to acknowledge the existence of random and pull it as a dependency. And then what happens when somebody writes betterrandom package with shuffling, and then evenmoreawesomerandom package with shuffling?

2 of 2
1

Probably just legacy, as you surmise.

And then there is the easy-to-mess-up np.array.resize (in-place) and np.array.reshape (returns an array). And there is the non-object oriented version np.reshape which again returns an array but there is no np.resize that would modify the argument in-place.

I don't know why you are saying np.reshape is non-object oriented. That doesn't even have a clear meaning.

np.array... are array functions so they understand how arrays are built. np.reshape on the other hand works on "array like" objects that roughly speaking just need to be a certain flavour of iterable. Obviously np.arrays themselves are "array like" but so are Python buffers, lists, etc, including, potentially, user made objects that numpy has never seen before. Anything that conforms to the "array like" interface. Now if you have such an iterable, and you know what an array is, then you can easily take the contents of the iterable and put them into a new array in any shape that fits. No? But now if you are asked to modify the iterable itself - where do you start? Is the iterable contiguous in memory? Or is it some kind of linked list? Or what? In fact you do not have one single clue where even one single element is in memory. So, how do you propose to modify this iterable in place?

(Personally, I do not like when functions modify an argument in place such as random.shuffle. A method should be the main way. But that is a different story)

Yes, this is a useful convention...but doing shuffling in place is also a convention. Sometimes there is not any "correct" way to do things, just trade offs.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ numpy: what's the point of producing arrays of shape (n,) instead of (n,1)? super annoying feature.
r/learnpython on Reddit: Numpy: What's the point of producing arrays of shape (N,) instead of (N,1)? Super annoying feature.
August 29, 2020 -

Whenever I make an operation that produces a 1D matrix (Sum the columns of a matrix, for instance) numpy will set its dimensions to (N,). If I'm making multiple operations, a multiplication by a (1,M) matrix for example, I would get the error "matrices are not aligned". What's the point of creating that type of "array" if you'll mostly all the time need to use reshape or expand_dims? To me it just seems like an unnecessary extra task, that I would not need to do in Matlab.

๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: Get the dimensions, shape, and size of an array | note.nkmk.me
April 23, 2025 - For a numpy.ndarray, len() returns the size of the first dimension, which is equivalent to shape[0]. It is also equal to size only for one-dimensional arrays.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ why is there no standard for typing array dimensions?
r/Python on Reddit: Why is there no standard for typing array dimensions?
3 weeks ago -

Why is there no standard for typing array dimensions? In data science, it really usefull to indicate wether something is a vector or a matrix (or a tensor with more dimensions). One up in complexity, its usefull to indicate wether a function returns something with the same size or not.

Unless I am missing something, a standard for this is lacking. Of course I understand that typing is not enforced in python, and i am not aksing for this, i just want to make more readable functions. I think numpy and scipy 'solve' this by using the docstring. But would it make sense to specifiy array dimensions & sizes in the function signature?

๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ numpy โ€บ size and shape of array in python
How to Shape and Size of Array in Python | Delft Stack
March 4, 2025 - While the shape tells you the dimensions of your array, the size gives you the total number of elements present in that array. The size attribute of a NumPy array provides this information, which is particularly useful when you need to know ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2657475 โ€บ ds-numpy-size-and-shape
DS, Numpy size and shape | Sololearn: Learn to code for FREE!
Size means the number of element in the array, so here since there are 45 presidents, and their height and age is 45 each, when we concatente them together, we will get 90. Meanwhile for the shape, it represents the number of rows and columns.
๐ŸŒ
Quora
quora.com โ€บ What-is-difference-between-shape-and-reshape-in-numpy
What is difference between shape() and reshape() in numpy? - Quora
Answer: shape function is either used to get the shape of already existing array by using arry_name.shape method. Another way to use shape is to change array dimensions of the array. But note that shape changes dimensions of the original array Reshape also changes the dimension of the array but ...
Top answer
1 of 5
23

I wouldn't worry about performance here - any differences should only be very marginal.

I'd say the more pythonic alternative is probably the one which matches your needs more closely:

a.shape may contain more information than len(a) since it contains the size along all axes whereas len only returns the size along the first axis:

>>> a = np.array([[1,2,3,4], [1,2,3,4]])
>>> len(a)
2
>>> a.shape
(2L, 4L)

If you actually happen to work with one-dimensional arrays only, than I'd personally favour using len(a) in case you explicitly need the array's size.

2 of 5
12

From the source code, it looks like shape basically uses len(): https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py

@property
def shape(self) -> Tuple[int, int]:
    return len(self.index), len(self.columns)
def __len__(self) -> int:
    return len(self.index)

Calling shape will attempt to run both dim calcs. So maybe df.shape[0] + df.shape[1] is slower than len(df.index) + len(df.columns). Still, performance-wise, the difference should be negligible except for a giant giant 2D dataframe.

So in line with the previous answers, df.shape is good if you need both dimensions, for a single dimension, len() seems more appropriate conceptually.

Looking at property vs method answers, it all points to usability and readability of code. So again, in your case, I would say if you want information about the whole dataframe just to check or for example to pass the shape tuple to a function, use shape. For a single column, including index (i.e. the rows of a df), use len().

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ i'm confused about numpy reshape order parameter (c-like vs fortran-like order): is there an eli-15 explanation?
r/learnpython on Reddit: I'm confused about numpy reshape order parameter (C-like vs Fortran-like order): is there an ELI-15 explanation?
February 9, 2022 -

I'm learning numpy and reading the documentation for np.reshape() the order parameter:

Read the elements of a using this index order, and place the elements into the reshaped array using this index order. โ€˜Cโ€™ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. โ€˜Fโ€™ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the โ€˜Cโ€™ and โ€˜Fโ€™ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. โ€˜Aโ€™ means to read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

Frankly this is like a foreign language to me, and I actually am coming from C as a programmer :?

I was hoping it would be like: if you have a 1d array C will put them in row-wise, and F will put them in column-wise. Obviously it can't be that simple as a general case, but is it that simple if I am reshaping a 1d array into a 2d array?

Anyone have a discussion that is simple at least for the simpler cases like that (I don't need a fully general discussion for nd to nd cases).

Top answer
1 of 2
3
I've been using numpy for years and have never used the order argument on the reshape function. Didn't even know that was a thing. If you have a numpy array (a) that looks like this: a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Then a.shape will return (10,) From here you can use reshape to literally reshape the array into whatever shape you want: a.reshape(5, -1) #returns array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]) You could also reshape it like so: a.reshape(-1, 5) # returns array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) In the first case, the new shape is [5, 2], and in the second case the new shape is [2, 5]. -1 indicates to infer that dimension's size based on the size of the array and the other arguments passed to reshape. -1 can only be used as one argument, obviously. And all the arguments have to equal the exact number of values in the array when multiplied together (i.e., I can't a.reshape(3, 4) on an array with 10 elements, but i could on an array with 12 elements). I wouldn't worry too much about the "order" argument unless you are going thru the things above and something doesn't make sense for some particularly esoteric use case.
2 of 2
1
If you are reshaping a 1D array to 2D, Fortran-like (F) will do column-wise and C-like (C) will do row-wise fill. To map it onto the language above if you have a 1D array A and you want to make it a 2D array B. Fortran iterates the first index first: B[1, 1] = A[1]; B[2,1] = A[2]; B[3,1] = A[3]; .... You can see that iterating the first index first results in filling column-wise. C iterates last index first, so: B[1, 1] = A[1]; B[1,2] = A[2]; B[1,3] = A[3]; .... which results in row-wise. Their language generalizes better to higher dimensional arrays. If B was 5 dimensional, Fortran would fill it as: B[1,1,1,1,1] = A[1]; B[2,1,1,1,1] = A[2]; B[3,1,1,1,1] = A[3]; .... And C would fill as: B[1,1,1,1, 1] = A[1]; B[1,1,1,1,2] = A[2]; B[1,1,1,1,3] = A[3]; .... But "row"-wise and "column"-wise aren't as clear here. The option "A" just chooses whichever version is consistent with how data is contiguous in memory for the architecture.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 60988757 โ€บ numpy-dimensions-vs-shape
numpy: dimensions vs. shape - Stack Overflow
Would it not be necessary to have shapes with something like a m x n x o shape for a three-dimensional array? ... np.array can be any dimension you wish. np.matrix is a specialized 2-D array that retains its 2-D nature through operations. ... Yes to the second question. ... I think you are confusing things here, np.array can have any number of dimensions and np.matrix only two, np.matrix is the specific case where ndim=2 and have special matrix operators. From NumPy documentation of np.matrix:
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ visualizing numpy shape.
r/learnpython on Reddit: Visualizing Numpy shape.
August 27, 2021 -

I have a 64x64 image with alpha layer, which when converted to NumPy array results in (64, 64, 4) shape. I am quite new to NumPy and have been having a hard time visualizing it. Below is the image of how I am visualizing it currently. Is this representation correct? If not can you please help me visualize it?

Image link

Top answer
1 of 4
3
Each pixel has an x, y, and RGBA value. There are 64 x positions, 64 y positions, and 4 RGBA values for each pair of x/y coordinates. I visualize it as a 'cube' of sorts, where the top face is the x/y coordinates, and the depth is the RGBA values.
2 of 4
3
I am not entirely sure what you are asking. In case of images, it is easiest to think of the data as a two-dimensional grid of RGBA-tuples that define the corresponding pixel's color and transparency. In memory, a multi-dimensional array is really just a linear, contiguous sequence of values (64*64*4=16384 many in this case, in the order of: all values in one 4-tuple of a pixel, all pixels in a row, all rows in the array (if you use NumPy's default memory layout)). The multi-dimensionality is all just fancy index arithmetic on that 1D chunk of memory. If you want, you can imagine a 3D array also as an actual 3D structure (though that's significantly less useful for images). Each number is a cube, and your array is a 64x64 square arrangement of cubes that is four layers high. Taking slices out of that array then is like taking rectangular sections out of that stack of cubes. array[0, :, :] would be selecting one row, so taking the upper most (viewed from a top-down perspective) 64 cubes wide, 4 cubes high section. array[:, 0, :] would be selecting the left most 64 cubes long, 4 cubes high section. And array[:, :, 0] would be selecting the farthest 64 by 64 layer (or nearest, doesn't really matter how you interpret the orientation of that axis).
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ understanding-shape-dimension-compatibility-numpy-can-arslan
Understanding Shape and Dimension Compatibility in NumPy
March 31, 2023 - Understanding Shape and Dimension Compatibility in NumPy NumPy is a powerful numerical computing library in Python that enables users to perform mathematical and logical operations on large arrays and matrices. In NumPy, arrays and matrices have shapes and dimensions that determine how computations
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.ndarray.shape.html
numpy.ndarray.shape โ€” NumPy v2.4 Manual
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the ...
๐ŸŒ
Reddit
reddit.com โ€บ r/numpy โ€บ numpy array is the worst part of python.
r/Numpy on Reddit: Numpy array is the worst part of python.
May 10, 2020 -

I come from HPC (c++/c/Fortran, etc), I can read and use other languages without issue. I like many parts of python, and its nice a lot of the time to not have to program everything from the ground up. I am not the biggest fan of weakly typed languages and python is slow, but it defiantly has a place and it does what it's designed to do pretty well.

However, the way numpy organises its array syntax and the general working of array is probably the most confusing poorly designed thing in the entire language, perhaps in the world of programming languages. I spend 80% of my programming time trying to get things into the right shape or work out what shape they are, and then use the correct functions to do extremely trivial appends/inserts/delete etc. Its is vague and honestly should be rewritten. There is honestly no defence of this poor syntax. There is a reason pretty much every other language deals with arrays in the same way... it makes sense.

I know that the hardcore python fanboys will tell me I am not doing things correctly, or lol, say "I must think 'pythonically'". BS, a good language does not need some gibberish word to defend the poor design.