Reshaping array
We can use reshape
method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM).
The only required condition is: a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)
numpy.reshape(array, shape, order = ‘C’) : Shapes an array without changing data of array.
# Python Program illustrating
# numpy.reshape() method
import numpy as geek
array = geek.arange(8)
print("Original array : \n", array)
# shape array with 2 rows and 4 columns
array = geek.arange(8).reshape(2, 4)
print("\narray reshaped with 2 rows and 4 columns : \n", array)
# shape array with 2 rows and 4 columns
array = geek.arange(8).reshape(4 ,2)
print("\narray reshaped with 2 rows and 4 columns : \n", array)
# Constructs 3D array
array = geek.arange(8).reshape(2, 2, 2)
print("\nOriginal array reshaped to 3D : \n", array)
Output :
Original array :
[0 1 2 3 4 5 6 7]
array reshaped with 2 rows and 4 columns :
[[0 1 2 3]
[4 5 6 7]]
array reshaped with 2 rows and 4 columns :
[[0 1]
[2 3]
[4 5]
[6 7]]
Original array reshaped to 3D :
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
Returns an array with evenly spaced elements as per the interval. The interval mentioned is half opened i.e. [Start, Stop) To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.
arange returns evenly spaced values within a given interval. step size is specified.
linspace returns evenly spaced values within a given interval. num no. of elements are returned.
arange([start,] stop[, step,][, dtype]) : Returns an array with evenly spaced elements as per the interval. The interval mentioned is half opened i.e. [Start, Stop)
# Python Programming illustrating
# numpy.arange method
import numpy as geek
print("A\n", geek.arange(4).reshape(2, 2), "\n")
print("A\n", geek.arange(4, 10), "\n")
print("A\n", geek.arange(4, 20, 3), "\n")
Output :
A
[[0 1]
[2 3]]
A
[4 5 6 7 8 9]
A
[ 4 7 10 13 16 19]
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) : Returns number spaces evenly w.r.t interval. Similiar to arange but instead of step it uses sample number.
# Python Programming illustrating
# numpy.linspace method
import numpy as geek
# restep set to True
print("B\n", geek.linspace(2.0, 3.0, num=5, retstep=True), "\n")
# To evaluate sin() in long range
x = geek.linspace(0, 2, 10)
print("A\n", geek.sin(x))
Output :
B
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
A
[ 0. 0.22039774 0.42995636 0.6183698 0.77637192 0.8961922
0.9719379 0.99988386 0.9786557 0.90929743]
Flatten array: We can use flatten method to get a copy of array collapsed into one dimension. It accepts order argument. Default value is ‘C’ (for row-major order). Use ‘F’ for column major order.
numpy.ndarray.flatten(order = ‘C’) : Return a copy of the array collapsed into one dimension.
# Python Program illustrating
# numpy.flatten() method
import numpy as geek
array = geek.array([[1, 2], [3, 4]])
# using flatten method
array.flatten()
print(array)
#using fatten method
array.flatten('F')
print(array)
Output :
[1, 2, 3, 4]
[1, 3, 2, 4]
Numpy | Array Creation
In this article, we are going to explore numpy array creation technique.
Table of Content
- Array creation using List :
- Array creation using array functions
- Methods for array creation in Numpy
Contact Us