# The Numpy Shape Function, Explained

In this tutorial, I’ll explain how to use the Numpy shape function.

I’ll explain what the function does, how the syntax works, and I’ll show you a couple of clear examples.

If you need something specific, just click on one of the following links.

## A Quick Introduction to Numpy Shape

The Numpy Shape function is pretty straight forward.

It retrieves the shape of a Numpy array.

Let me quickly explain.

### A Quick Review of Numpy Array Shapes

As you’re probably aware, Numpy is a toolkit in Python for working with Numpy arrays.

Numpy arrays are data structures that store numbers in a row-and-column format.

So structurally, they’re something like an Excel spreadsheet with numbers in it. Or like a matrix in linear algebra.

#### Numpy arrays have a “shape”

Numpy arrays have a variety of properties, like size of the array and the number of dimensions.

But one of the most important properties is the shape of the array.

What is array shape?

The shape is the number of units along each dimension of the array.

So let’s say that we have a 2D array with 2 rows and 3 columns.

The shape of the array then, is `(2,3)`.

That’s the number of rows and the number of columns.

#### np.shape returns the shape of an array

So now let’s go back to the Numpy shape function.

The np.shape function simply returns the shape of an array.

That’s sort of it.

It’s a pretty simple function.

That being said, let’s take a look at the syntax of Numpy shape, so we can see exactly how it works.

## The Syntax of Numpy Shape

Ok. Here, we’ll look at the syntax of the Numpy shape function.

#### A quick note

One quick reminder before we get started.

Everything in this syntax section, and in the examples section, assumes that you’ve imported Numpy with the alias `np`.

You can do that with the following code:

```import numpy as np
```

Remember: this enables us to call Numpy functions with the prefix `np`.

This obviously changes the syntax slightly, so it’s relevant to our syntax discusssion.

### np.shape syntax

The syntax of Numpy shape is extremely simple.

You call the function with the name `np.shape()`.

Then, inside the parenthesis, you provide the name of the Numpy array that you want to operate on.

### The Parameters and Inputs of Numpy Shape

As shown above, there is only one input to the np.shape function: the array you want to operate on.

###### `input_array` (required)

The `input_array` is the input to the function.

This input is required.

Having said that, as is common with Numpy functions, the function will not only accept Numpy arrays, but also array-like objects, such as Python lists and tuples.

### The Output of np.shape

The output of np.shape() is a tuple of integers.

These integers represent the shape of the input array.

More specifically, the integers provide the number of elements along each axis of the the array.

So for example, if the function returns the tuple `(2,3)`, that indicates that the array has 2 units along axis-0, and 3 units along axis-1.

## Examples of How to Use Numpy Shape

Now that we’ve looked at the syntax, let’s look at some examples.

Examples:

#### Run this code first

Before you run the examples, make sure that you import Numpy.

You can do that with the following code:

```import numpy as np
```

### EXAMPLE 1: Get the shape of a 1D array

First, we’ll get the shape of a 1D array.

#### Create 1D array

The first step of this example, is to create our 1-dimensional array.

To do this, we’ll use Numpy random randint to generate an array with random integers.

```np.random.seed(44)
my_1d_array = np.random.randint(size = 5, low = 0, high = 100)
```

And let’s print out the array.

```print(my_1d_array)
```

OUT:

```[20 35 45 59  3]
```

As you can see, this array has 5 values, and 1 dimension.

#### Get the Shape

Now, we’ll use Numpy shape to get the shape of the array.

```np.shape(my_1d_array)
```

OUT:

```(5,)
```
##### Explanation

As you can see, the output is a tuple with only one value: 5.

There’s only one value, because it’s a 1-dimensional array (there’s one value per dimension).

And the integer value is 5, because there are five numbers inside the array.

### EXAMPLE 2: Get the shape of a 2D array

Next, we’ll get the shape of a 2-dimensional array.

#### Create 2D array

The first step of this example is to create a 2-dimensional array.

To do this, we’ll use Numpy random randint again. But this time, we’ll specify a 2-dimensional shape (to be clear, we specify the “shape” in this case by using the `size` parameter).

```np.random.seed(77)
my_2d_array = np.random.randint(size = (2,3), low = 0, high = 100)
```

And now, let’s print out the array:

```print(my_2d_array)
```

OUT:

```[[87 95 84]
[84 37 24]]
```

This array has 6 total values, and 2 dimensions … 2 rows and 3 columns.

#### Get the Shape

Now, we’ll use np.shape to retrieve the shape of the array.

```np.shape(my_2d_array)
```

OUT:

```(2,3)
```
##### Explanation

In this case, the output of np.shape is a tuple with two numbers: `(2,3)`.

That’s the first number represents the number of rows (axis-0), and the second number represents the number of columns (axis-1).

There are 2 rows and 3 columns, so the output of Numpy shape is `(2,3)`.

Do you have any other questions about the Numpy shape function?

This tutorial should have helped you understand how to retrieve the shape of a Numpy array with `np.shape()`.

But if you want to really learn how to do data wrangling and data science in Python, there’s a lot more to learn.

If you’re ready to learn the full range of Python data science skills, then sign up for our free email list.

• NumPy
• Pandas
• Base Python
• Scikit learn
• Machine learning
• Deep learning
• … and more.

Members of our email list get our free tutorials delivered to their inbox, FREE, several times per month.

Joshua Ebner

Joshua Ebner is the founder, CEO, and Chief Data Scientist of Sharp Sight.   Prior to founding the company, Josh worked as a Data Scientist at Apple.   He has a degree in Physics from Cornell University.   For more daily data science advice, follow Josh on LinkedIn.