0% found this document useful (0 votes)
5 views10 pages

NumPy Array Reshaping

The document provides an overview of reshaping NumPy arrays, explaining how to change the shape of an array by adding or removing dimensions. It includes examples of reshaping from 1-D to 2-D and 3-D, as well as guidelines on the conditions for reshaping and the use of an unknown dimension. Additionally, it discusses flattening arrays and mentions other functions for manipulating array shapes.

Uploaded by

s.mohamahm28
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

NumPy Array Reshaping

The document provides an overview of reshaping NumPy arrays, explaining how to change the shape of an array by adding or removing dimensions. It includes examples of reshaping from 1-D to 2-D and 3-D, as well as guidelines on the conditions for reshaping and the use of an unknown dimension. Additionally, it discusses flattening arrays and mentions other functions for manipulating array shapes.

Uploaded by

s.mohamahm28
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

31/01/2026, 23:36 NumPy Array Reshaping

 Tutorials  References  Exercises  Get Certified Sign In

QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC

NumPy Array Reshaping


❮ Previous Next ❯

Reshaping arrays
Reshaping means changing the shape of an array.

The shape of an array is the number of elements in each dimension.

By reshaping we can add or remove dimensions or change number of elements in each


dimension.

Reshape From 1-D to 2-D

Example Get your own Python Server

Convert the following 1-D array with 12 elements into a 2-D array.

The outermost dimension will have 4 arrays, each with 3 elements:


[Link] 1/10
31/01/2026, 23:36 NumPy Array Reshaping

importTutorials
numpy as np
 References  Exercises  Get Certified Sign In

arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC

newarr = [Link](4, 3)

print(newarr)

Try it Yourself »

Reshape From 1-D to 3-D

Example
Convert the following 1-D array with 12 elements into a 3-D array.

The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements:

import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = [Link](2, 3, 2)

print(newarr)

Try it Yourself »

ADVERTISEMENT

[Link] 2/10
31/01/2026, 23:36 NumPy Array Reshaping

 Tutorials  References  Exercises  Get Certified Sign In

QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC

REMOVE ADS

Can We Reshape Into any Shape?


Yes, as long as the elements required for reshaping are equal in both shapes.

We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot
reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.

Example
Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension (will
raise an error):

import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8])

newarr = [Link](3, 3)

print(newarr)

Try it Yourself »

Returns Copy or View?

[Link] 3/10
31/01/2026, 23:36 NumPy Array Reshaping

 Tutorials 
Example
References  Exercises  Get Certified Sign In

QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC


Check if the returned array is a copy or a view:

import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8])

print([Link](2, 4).base)

Try it Yourself »

The example above returns the original array, so it is a view.

Unknown Dimension
You are allowed to have one "unknown" dimension.

Meaning that you do not have to specify an exact number for one of the dimensions in the
reshape method.

Pass -1 as the value, and NumPy will calculate this number for you.

Example
Convert 1D array with 8 elements to 3D array with 2x2 elements:

import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8])

newarr = [Link](2, 2, -1)

print(newarr)

[Link] 4/10
31/01/2026, 23:36 NumPy Array Reshaping

Try it Yourself »
Tutorials  References  Exercises  Get Certified Sign In

QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC

Note: We can not pass -1 to more than one dimension.

Flattening the arrays


Flattening array means converting a multidimensional array into a 1D array.

We can use reshape(-1) to do this.

Example
Convert the array into a 1D array:

import numpy as np

arr = [Link]([[1, 2, 3], [4, 5, 6]])

newarr = [Link](-1)

print(newarr)

Try it Yourself »

Note: There are a lot of functions for changing the shapes of arrays in numpy flatten ,
ravel and also for rearranging the elements rot90 , flip , fliplr , flipud etc. These fall
under Intermediate to Advanced section of numpy.

[Link] 5/10
31/01/2026, 23:36 NumPy Array Reshaping

 Tutorials  References  Exercises 


?
Get Certified Sign In

QL PYTHON JAVA PHP HOW TO


Exercise
[Link] C C++ C# BOOTSTRAP REAC

Consider the following code:


import numpy as np
arr = [Link]([[1, 2, 3], [4, 5, 6]])
newarr = [Link](6)
print(newarr)

What will be the printed result?

[[][1 2 3 4 5 6]

[[1 2 3 4 5 6]]]

[1 2 3 4 5 6]

Submit Answer »

❮ Previous Sign in to track progress Next ❯

[Link] 6/10
31/01/2026, 23:36 NumPy Array Reshaping

 Tutorials  References  Exercises  Get Certified Sign In

QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC

COLOR PICKER

 

REMOVE ADS

[Link] 7/10
31/01/2026, 23:36 NumPy Array Reshaping

Python - Global Variables - [Link]


 Tutorials  References  Exercises  Get Certified Sign In

QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC

Click on ► to watch the video

[Link] 8/10
31/01/2026, 23:36 NumPy Array Reshaping

 Tutorials  References  Exercises  Get Certified Sign In

QL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REAC

-->
 PLUS SPACES

GET CERTIFIED FOR TEACHERS

BOOTCAMPS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
AngularJS Reference
jQuery Reference

[Link] 9/10
31/01/2026, 23:36 NumPy Array Reshaping

Top Examples Get Certified


 Tutorials  References  Exercises  Get Certified Sign In
HTML Examples HTML Certificate
QL PYTHON JAVA CSSPHP Examples HOW TO [Link] C C++ CSSC# Certificate
BOOTSTRAP REAC
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and
privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 10/10

You might also like