# Python NumPy shape with examples

In this Python tutorial, we will discuss Python NumPy Shape with a few examples like below:

• Python numpy shape dimensions
• Python numpy shape 0
• Python numpy shape function
• Python numpy shape 1
• Python numpy shape vs reshape
• Python numpy shape vs size
• Python numpy np.asarray shape
• Python numpy change array shape
• Python numpy shape tuple
• Python numpy arange shape

## Python NumPy Shape

• In this section, we will discuss Python NumPy shape.
• NumPy arrays have a function called shape that always returns a tuple with each index having the number of adjacent elements.
• The Numpy array shape property is to find the shape of an array.
• In this method we can easily use the numpy.shape() function.

Syntax:

Here is the Syntax of numpy.shape()

``````numpy.shape
(
arr
)``````
• It consists of few parameters.
• arr: input array
• Returns: The values of the shape function always give the length of the adjacent np. array.

Example:

Let’s take an example to check how to implement Python NumPy shape

``````import numpy as np

arr2= np.array([[4, 2, 3, 2, 1, 8],
[5, 4,6,7,8,9]])
res = np.shape(arr2)

print(res)``````

In the above example the array returns (2,6) which means that the array has 2 dimensions, and each dimension has 6 values.

Here is the Screenshot of the following given code

## Python numpy shape dimensions

• In this section, we will discuss Python NumPy shape dimensions.
• The number of dimensions of np. ndarray can be represented as an integer value int with attribute ndim.
• In this method we can easily use the function ndarray.ndim.
• The number of axes (dimensions) of the array.
• The dimension of a matrix is the no. of rows and columns in a matrix. A matrix can be structured as a list of lists or an array of arrays.

Example:

Let’s take an example to check how to implement the NumPy shape dimension.

``````import numpy as np

a = np.array([[2,3],[3,4]])
b = a.ndim
print(b)``````

In the above code first, we will create an array by using the function numpy. array. Now create a variable and assign a function n.dim to check the dimension of the given array.

Here is the Screenshot of the following given code

## Python numpy shape 0

• In this section, we will discuss Python NumPy shape 0.
• Shape is n.shape is a tuple that always gives dimensions of the array. The shape is a tuple that gives you an indication of the no. of dimensions in the array.
• The shape function for numpy arrays returns the dimensions of the array.
• If Y has u rows and v columns, then Y.shape is (u,v). So Y.shape is v.

Example:

Let’s take an example to check how to implement numpy shape 0

``````import numpy as np

a = np.array([[2,3],[3,4]])
b = np.shape(a)
c = a.shape
print(c)``````

In the above example, we have to use the function np. shape to give

Here is the Screenshot of the following given code

## Python numpy shape function

• In this section, we will discuss Python NumPy shape function.
• The numpy module provides a shape function to represent the shape and size of an array. The shape of an array is the no. of elements in each dimension.
• In NumPy, we will use a function called shape that returns a tuple, the elements of the tuple give the lengths of the array dimensions.
• The shape attribute always returns a tuple that represents the length of each dimension. The 1-d array is a row vector and its shape is a single value sequence followed by a comma. One-d arrays don’t have rows and columns, so the shape function returns a single value tuple.

Syntax:

Here is the Syntax of the Python numpy shape function

``````numpy.shape
(
arr
)``````
• It consists of few parameters.
• arr: input array
• Returns: The values of the shape tuple give the lengths of the adjacent array dimensions.

Example:

Let’s take an example to check how to implement the Python NumPy shape function

``````import numpy as np

arr = np.array([2,3,4,5,6,7,8])
print(arr)

print('Array Shape = ', np.shape(arr))``````

In the above code first, we import a NumPy library and create a 1-D array, it is a tuple with one value instead of an integer value. A tuple with one element has a sequence comma.

Here is the Screenshot of the following given code

## Python numpy shape 1

• In this section, we will discuss Python NumPy shape 1
• In numpy, some of the functions return in shape(R,1) but some return (R,).
• This will make matrix multiplication more complex since an explicit reshape is required.
• Shape is n.shape is a tuple that always gives dimensions of the array. The shape function is a tuple that gives you an arrangement of the number of dimensions in the array.
• If Y has w rows and z columns, then Y.shape is (w,z). So Y.shape is z.

Example:

Let’s take an example to check how to implement Python NumPy shape 1

``````import numpy as np

a = np.array([[2,3],[3,4]])
b = np.shape(a)
print('Column shape=', a.shape)
print('Array Shape = ', np.shape(a))``````

In the above code, we will import a NumPy library and create an array using the function numpy. array. Now we can easily use the function np. shape and pass the value 1 as a parameter. The output will display the columns in an array.

Here is the Screenshot of the following given code

## Python numpy shape vs reshape

• In this section, we will discuss Python NumPy shape vs reshape.
• Np. reshape will copy the data if it cannot make a proper view, whereas getting the shape will raise an error instead of copying the data.
• The shape function will always give a tuple of array dimensions and can easily be used to change the dimensions of an array.
• The reshape function gives a new shape to an array without changing its value. It creates an array and does not update the original array itself.
• The shape function always returns a tuple that tells us the length of each dimension while in the case of reshape function It returns a new value on the existing data if possible either than create a full copy of the original array.

Example:

``````import numpy as np

a = np.array([2,3,4,5])
print('Array Reshape',np.reshape(a,(2,2)))# Reshape function

print('Array shape',np.shape(a)) # shape function``````

In the above code, we will import a NumPy library and create an array using the function numpy. array. Now, we can use an np.reshape() function to display the new array in the form of a 2*2 matrix. Along with that, we will equate with the function and it will return the array of dimensions in the form shape.

Here is the Screenshot of the following given code

## Python numpy shape vs size

• In this section, we will discuss Python NumPy shape vs size.
• Shape compares to the size of the dimensions of an n-darray. Size regarding arrays relates to the number of elements that are stored in the array.
• The Np .size() function has few arguments. First is an array, which required a parameter need to give an array. The second is an axis, by default an argument. The axis stores no value, according to the condition you can update it whereas the Np.shape() function consists of two parameters input array and it will always return elements of the shape function give the lengths of the corresponding array dimensions.
• In np. size() function counts the number of elements along a given axis and in the case of numpy.shape() function returns a function with each indices having the number of adjacents elements.

Example:

``````import numpy as np

a = np.array([[2,3,4,5],[4,5,6,7]])

print('Array shape',np.shape(a)) # shape function

# for numpy size function
b = np.array([[4,7,8,9],[1,2,3,4]])
print('Array size',np.size(b)) # size function``````

In the above example, we will import a NumPy library and create an array using the function numpy. array. Now, we will use the function np. shape to identify the dimension of an array.

In this example, you can also check that we create an array after getting the dimension of an array and use the function np. size() and it will display the size of an array.

Here is the Screenshot of the following given code

## Python numpy np.asarray shape

• In this section, we will discuss the Python NumPy np.asarray function.
• The asarray()function is used when we want to convert an input to an array. The input could be a list, tuple, ndarray, etc.
• The input data can be in the form of Lists, Tuples, lists of tuples, tuples of lists, etc.
• This function is always available in the numpy module present in the standard library of python.

Syntax:

Here is the Syntax of numpy.asarray() function.

``````numpy.asarray
(
arr,
dtype=None,
order=None,
)``````
• It consists of few parameters
• arr: The Input data, in any form that can be converted to an array. This includes lists, tuples, tuples of tuples, tuples of lists, and numpy dimension arrays.
• dtype: It is a dtype argument that is optional in the syntax. If we do not declare it in the syntax, it is defined by default from the input data.
• Order: This is also an optional parameter in the syntax. It also decides whether to use row or column-major memory representation.
• Return: if the input is already a numpy dimension array with equating dtype and order. If the array is a subclass of numpy dimension array, then a base class numpy dimension array is returned.

Example:

Let’s take an example to check how to implement np.asarray shape

``````import numpy as np

a = np.array([[2,3,4,5],[4,5,6,7]])
b = np.asarray(a)
print(b)``````

In the above code, we will import a NumPy library and create an array using the numpy. array function. Now create a variable and assign a function that is numpy. asarray. The Input data, in any form that can be converted to a numpy array.

Here is the Screenshot of the following given code

## Python numpy change array shape

• In this section, we will discuss the Python NumPy change array shape.
• In this method we can easily use the function numpy.reshape().
• It will give a new shape to an array without removing its data.
• The shape of the array can also be changed using the reshape() function. If the specified dimension is bigger than the original array, The extra spaces in the array will be filled with repeated copies of the original numpy array.
• Reshaping a numpy array simply means exchange the shape of the given array, shape basically tells the number of values and dimensions of the array, by reshaping an array we can add or delete dimensions or exchange the number of elements in each dimension.

Syntax:

Here is the Syntax of numpy.reshape() function

``````numpy.reshape
(
arr,
newshape,
order='C'
)``````
• It consists of few parameters
• arr: Array to be reshaped.
• newshape: The numpy shape should be compactable with the new original shape. If an integer value, is given then the result will be a 1-Dimensional array of that length.
• Returns: reshaped_arr ( This will be a new object if it is possible; otherwise, it will be a copy).

Example:

``````import numpy as np

a = np.array([[2,3,4],[4,5,6]])
b = np.shape(a)
print("array shape",b)
c= np.reshape(a,(3,2))
print("array reshape",c)``````

In the above code first, we will create a numpy array and use the function numpy. shape. After that, it will represent the result in the form of ndarray dimension.

Here is the Screenshot of the following given code

## Python numpy shape tuple

• In this section, we will discuss the Python NumPy shape tuple.
• The shape of a simple tuple or list can be obtained with the built-in len() method. len() function will return an integer that describes the no. of objects in the given list.
• Tuples and lists are objects that store multiple values and it is a simple task to determine the no. of elements (or shapes) in a tuple or list object.
• In this method we can easily use the function len() to get the shape of a tuple.
• The number of elements contains in the object is never calculated, so len() function helps the user to provide the number of elements.
• The len() function takes only one argument: an iterable object.

Example:

``````import numpy as np

a = (2,3,4,5,6,7)
b = len(a)
print("Shape of the tuple",b)``````

Here is the Screenshot of the following given code

## Python numpy arange shape

• In this section, we will discuss the Python NumPy arange shape.
• In this example we can easily use the function numpy.arange() and numpy.shape().
• The numpy.arange() is a Numpy method that returns the numpy dimension array object containing evenly spaced values within the given range. The np. arange() function takes a few arguments that include start step stop and dtype and returns evenly spaced values within a given interval.

Syntax:

``````numpy.arange
(
start,
stop,
step,
dtype
)``````
• It consists of few parameters.
• start: Its an optional parameter. Start of an interval.
• Stop: end of an interval
• step: Its an optional parameter.

Example:

``````import numpy as np

arr = np.arange(2,10,3)
arr2 = np.shape(arr)
print(arr2)``````

Here is the Screenshot of the following given code

You may like the following Python tutorials:

In this Python tutorial, we learned Python NumPy shape with a few examples.

• Python numpy shape dimensions
• Python numpy shape 0
• Python numpy shape function
• Python numpy shape 1
• Python numpy shape vs reshape
• Python numpy shape vs size
• Python numpy np.asarray shape
• Python numpy change array shape
• Python numpy shape tuple
• Python numpy arange shape