NumPy – reshape

NumPy - reshape

In this chapter, we will discuss NumPy – reshape. This function gives a new shape to an array without changing the data. It accepts the following parameters −

numpy.reshape(arr, newshape, order')

Where,

Sr.No.Parameter & Description
1arrArray to be reshaped
2newshapeint or tuple of int. New shape should be compatible to the original shape
3order for C style, ‘F’ for Fortran style, ‘A’ means Fortran like order if an array is stored in Fortran-like contiguous memory, C style otherwise

Example Of NumPy reshape

import numpy as np
a = np.arange(8)
print 'The original array:'
print a
print '\n'

b = a.reshape(4,2)
print 'The modified array:'
print b

Its output would be as follows −

The original array:
[0 1 2 3 4 5 6 7]

The modified array:
[[0 1]
 [2 3]
 [4 5]
 [6 7]]

Next Topic – Click Here

This Post Has 5 Comments

Leave a Reply