Web Technologies - NumPy

Back to Course

Lesson Description

Lession - #300 NumPy Array Reshape

Reshaping arrays

Reshaping implies changing the shape of an array.
The shape of an array is the quantity of components in each aspect.
By reshaping we can add or eliminate aspects or change number of components in each aspect.
Reshape From 1-D to 2-D
Convert the accompanying 1-D array with 12 components into a 2-D array.
The peripheral aspect will have 4 arrays, each with 3 components:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]>
newarr = arr.reshape(4, 3>

Reshape From 1-D to three dimensional

Convert the accompanying 1-D array with 12 components into a three dimensional array .
The furthest aspect will have 2 arrays that contains 3 arrays , each with 2 components:

Flattening the arrays

Straightening array implies changing over a complex cluster into a 1D array.
We can utilize reshape(- 1>
to do this.
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]]>
newarr = arr.reshape(-1>

import numpy => Bring the file numpy from numpy import * => Bring the file numpy, open the file & extract all the function & variable of that file(*>
The nparange is a Numpy method that returns the ndarray object containing evenly spaced values within the given range.
initializing array in python · Using for loop, range(>
function and append(>
method of list. Intialize empty array; Intialize array with default values .
sortedArr = npsort(arr, kind='heapsort'>
. print('Sorted Array : ', sortedArr>
. print('*** Sorting 2D numpy array along axis ***'>
The use of nploadtxt is best illustrated using an example. Consider the following text file of data relating to a (fictional>
population of students.
nprandom.rand. Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1>