https://www.rakeshmgs.in/search/label/Template
https://www.rakeshmgs.in


RakeshMgs

Learn Complete NumPy | Learn NumPy Programming Language for Begginers

Updated:

1. Introduction

Numpy is a module that is designed to create and manipulate arrays. Python has its own array module. But the array module is not flexible enough to carry out the array manipulations required to be in scientific and mathematical applications.

At the core of the NumPy package, is the ndarray object which encapsulates n-dimensional arrays of homogeneous data types.
Important differences between NumPy arrays and the standard Python sequences:
  1. NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original.
  2. Elements in a NumPy array are all required to be of the same data type thus same size in memory. One can have arrays of objects, thereby allowing for arrays of different sized elements.
  3. Scientific and mathematical Python-based packages are using NumPy arrays
  4. Element-by-element operations are the “default mode” when an ndarray is involved.
  5. NumPy fully supports an object-oriented approach. Ndarray is a class, possessing numerous methods and attributes.

NumPy ndarray class to become the de-facto language of multi-dimensional data interchange used in Python. NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.

 
2. ndarray attributes

Every array in numpy is an object of ndarray class. The properties of an array can be manipulated by accessing the ndarray attributes. The more important attributes of an ndarray object are:

  1. ndarray.ndim-Number of axes (dimensions) of the array.
  2. ndarray.shape- Dimensions of the array. A tuple of integers indicating the size of the array in each dimension. E.g:Matrix with n rows and m columns will have the shape (n,m). The length of the shape tuple is therefore the number of axes, ndim.
  3. ndarray.size-The total number of elements of the array. This is equal to the product of the elements of shape.
  4. ndarray.dtype-The object describing the type of the elements in the array. Additionally NumPy provides data types of its own. E.g.:numpy.int32, numpy.int16, and numpy.float64
  5. ndarray.itemsize-Size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while type complex32 has itemsize 4 (=32/8).
  6. ndarray.data the buffer containing the actual elements of the array.
3. Numpy data types

Apart from the built-in data types of Python, NumPy has some extra data types, and the data types are referred with one character, like i for integers, u for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent them:

  • i - integer
  • b - boolean
  • u - unsigned integer
  • f - float
  • c - complex float
  • m - timedelta
  • M - datetime
  • O - object
  • S - string
  • U - unicode string
  • V - fixed chunk of memory for other type ( void )

4.1. Introduction

Array is a collection data of similar data type elements. Arrays are the ways of representing large volume data in the scientific or mathematical applications. Numpy provides the following methods to create arrays:

  • array()
  • arange()
  • zeros()
  • ones()
  • empty()
  • linspace()

This section explains the various ways to create arrays using above methods.


4.2. Array creation using arange()

NumPy arange() is one of the array creation routines based on numerical ranges. It creates an instance of ndarray with evenly spaced values and returns the reference to it.

Syntax:
np.arange(start,end,step,dtype)
  • Start-First value in the array
  • End-Last value in the array (Value of end does not include in the values generated).Value generated will be upto end-1
  • Step-Difference between each consecutive values in the array. Default is 1.
  • dtype-Type of elements of output array. Default is None.
Example 1
1
2
3
import numpy as np
ndarray=np.arange(1,15,2,int)
print(ndarray)

Output:
[ 1  3  5  7  9 11 13]
Example 2
1
2
3
4
import numpy as np
ndarray=np.arange(1,41,1,int)
ndarray.shape=(5,8)
print(ndarray)

Output:
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]
 [17 18 19 20 21 22 23 24]
 [25 26 27 28 29 30 31 32]
 [33 34 35 36 37 38 39 40]]

In case the array type is omitted, the arange() will deduce type of the array elements from the types of start, stop and step values. In the below example, since dtype is omitted arange() deduces the dtype from the other parameters. Since one of the parameters is of float type, all the elements in the resultant array are of type float.

Example
1
2
3
import numpy as np
ndarray=np.arange(1,15.0,2)
print(ndarray)

Output:
[ 1.  3.  5.  7.  9. 11. 13.]

If the step value is omitted, arange() assumes 1 as default step value. That means it creates the array with consecutive numbers from the start value.

Example
1
2
3
4
import numpy as np
numarray=np.arange(1,16)
print(numarray)
print(type(numarray))

Output:
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]
<class 'numpy.ndarray'>


4.3. Creation of array using array() function

array() is an in-built method in numpy to create an array with the given list of elements.

Syntax:
array(data_type,value_list) 
  • data_type-Data type of the elements in the array
  • value_list-List of elements to be the part of the array
Example 1
1
2
3
4
import numpy as np
arr=np.array([1.5,3.2,4.7])
print(arr)
print(arr.dtype)

Output:
[1.5 3.2 4.7]
float64
Example 2
1
2
3
4
import numpy as np
arr=np.array([1,2,3,4,5,6,7,8]).reshape(2,4)
print(arr)
print(arr.dtype)

Output:
[[1 2 3 4]
 [5 6 7 8]]
 int64

Frequent error occurs when calling array() is calling array() with multiple arguments, instead a single list of values.

Example
1
2
3
import numpy as np
arr=np.array(1,2,3,4)
print(arr)

Output:
Traceback (most recent call last):
  File "pycharm.py", line 2, in <module>
    arr=np.array(1,2,3,4)
TypeError: array() takes from 1 to 2 positional arguments but 4 were given

Array transforms sequences of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.

Example
1
2
3
import numpy as np
arr=np.array([(1,2,3),(4,5,6),(7,8,9)])
print(arr)

Output:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

The data type of the array elements can be explicitly mentioned while the array creation.

Example
1
2
3
import numpy as np
arr=np.array([(1,2),(4,5),(7,8)],dtype=complex)
print(arr)

Output:
[[1.+0.j 2.+0.j]
 [4.+0.j 5.+0.j]
 [7.+0.j 8.+0.j]]

4.4. Array creation using zeros()

The elements of the arrays are unknown while the array size will be known in prior. NumPy offers several functions to create arrays with placeholder elements for elements going to be added in future.

zeros() function creates an array full of zeros.

Syntax:
Numpy.zeros((<shape of the array>)
  • Shape of the array denotes the length of the array in every dimension separated by comma.
Example
1
2
3
import numpy as np
arr=np.zeros((3,3))
print(arr)

Output:
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Array full of zeros with a particular data type specified can be created.

Example
1
2
3
import numpy as np
arr=np.zeros((3,3),dtype=int)
print(arr)

Output:
[[0 0 0]
 [0 0 0]
 [0 0 0]]

4.5. Array creation using ones()

The elements of the arrays are unknown while the array size will be known in prior. NumPy offers several functions to create arrays with placeholder elements for elements going to be added in future.

ones() function creates an array full of ones with given shape.

Syntax:
Numpy.ones((<shape of the array>)
  • Shape of the array denotes the length of the array in every dimension separated by comma.
Example
1
2
3
import numpy as np
arr=np.ones((3,3),dtype=int)
print(arr)

Output:
[[1 1 1]
 [1 1 1]
 [1 1 1]]


4.6. Array creation using empty()

The elements of the arrays are unknown while the array size will be known in prior. NumPy offers several functions to create arrays with placeholder elements for elements going to be added in future.

empty() function creates an empty array in the given dimension with random values which vary for every call.

Syntax:
Numpy.empty((<shape of the array>)
  • Shape of the array denotes the length of the array in every dimension separated by comma.
Example
1
2
3
import numpy as np
arr=np.empty((3,3),dtype=int)
print(arr)

Output:
[[476741369956          103            0]
 [416611827811          116            0]
  [416611827826 420906795106 498216206441]]

4.7. Array creation using linspace()

linspace() is a array creation method in Numpy library. It is creating an array with evenly spaced values within specified interval.

Syntax:
numpy.linspace(start,stop,num,endpoint,retstep,dtype,axis)
  • start: The first value of the sequence.
  • end: The last value of the sequence unless the endpoint is set to False.
  • num: The number of samples needed to generate within the interval. The default value is 50.
  • endpoint: If the endpoint is set to false, then the end value is not included in the sequence.
  • retstep : If the retstep is true then (samples, step) is returned. **Step refers to the spacing between the values in the interval.
  • dtype: The data type of elements output array. The datatype is determined from the data types of the parameters if not specified.
  • axis: The axis in the result to store the samples.
Example
1
2
3
import numpy as np
arr=np.linspace(1,50,50)
print(arr)

Output:
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36.
 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50.]

5. Shaping the array using reshape() function

reshape() function is used to give a new shape to an array without changing its data.

Syntax:
numpy.reshape(a, newshape, order='C')
  • a-array to be reshaped
  • newshape-set of integers or tuple which determines the new shape of the array.
Example
1
2
3
import numpy as np
numarray=np.arange(1,17).reshape(4,4)
print(numarray)

Output:
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]


6. Displaying Arrays

The elements in the list are printed in the following layout:

  • Last axis is printed from left to right.
  • Second-but-last is printed from top to bottom.
  • Rest of the axes are printed from top to bottom with an empty line between the slices.
Example
1
2
3
import numpy as np
arr=np.arange(15).reshape(3,5)
print(arr)

Output:
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

If the array size is very large then only the corners of the array are printed and the central parts of the array is skipped.

Example
1
2
3
import numpy as np
arr=np.arange(10000).reshape(100,100)
print(arr)

Output:
[[   0    1    2 ...   97   98   99]
 [ 100  101  102 ...  197  198  199]
 [ 200  201  202 ...  297  298  299]
 ...
 [9700 9701 9702 ... 9797 9798 9799]
 [9800 9801 9802 ... 9897 9898 9899]
 [9900 9901 9902 ... 9997 9998 9999]]

To enable numpy to print the entire array, it is necessary to forcibly change the print option to print the array in its maximum size by calling set_printoptions() method as follows:

Syntax:
numpy_object.set_printoptions(threshold=sys.maxsize)
Example
1
2
3
4
5
import numpy as np
from numpy import sys
arr=np.arange(10000).reshape(100,100)
np.set_printoptions(threshold=sys.maxsize)
print(arr)

7.1. Array Indexing
An array element can be accessed using the index of the array element. Index is a numerical identifier allotted for every element in the array which denotes the position of the element in the given array.


7.2. Accessing elements in 1-D array

The first element in the 1-D array has index 0, second element has index 1 and so on. Accessing an element in 1-D array requires the index of the element to be accessed within square brackets.

Syntax:
array_name[index]
Example
1
2
3
import numpy as np
arr=np.array([10,20,30,40,50])
print(arr[2])

Output:
30


7.3. Accessing element in 2-D array

To access an element in a 2-D array the integers representing the dimension and the index of the element to be accessed are mentioned within square brackets separated by comma (,).

Syntax:
array_name[dimension,index]
Example
1
2
3
import numpy as np
arr=np.arange(1,16).reshape(3,5)
print("Element at Dimension 1 and index 2:",arr[1,2])

Output:
Element at Dimension 1 and index 2: 8

7.4. Negative indexing

An element in the array can be accessed from the end using negative index. The elements in the array are indexed from the end with the starting index -1.

Syntax:
array_name[negative_index]
Example
1
2
3
import numpy as np
arr=np.arange(1,16)
print("The element at index -2:",arr[-2])

Output:
The element at index -2: 14

8.1. Basic operations on arrays

A bunch of operations can be performed on numpy arrays. The addition, subtraction, multiplication of matrices using normal arithmetic or compound assignment operators are explained in this section.

The application of arithmetic operators on arrays carries out the required operations element wise and returns the resultant array.

8.2. Addition of two arrays

Addition of two arrays can be performed by applying arithmetic operator ‘+’. It adds the operand arrays element wise and returns a third array as result.

Syntax:
Array3=Array1+Array2
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import numpy as np
array1=np.arange(100,140,10).reshape(2,2)
array2=np.array([1,2,3,4]).reshape(2,2)
array3=array1+array2
print("First Array")
print(array1)
print("Second Array")
print(array2)
print("Third Array")
print(array3)

Output:
First Array
[[100 110]
 [120 130]]
Second Array
[[1 2]
 [3 4]]
Third Array
[[101 112]
 [123 134]]

8.3. Subtraction of two arrays

Subtraction of two arrays can be performed by applying arithmetic operator ‘-’. It subtracts the second operand array from the first operand array element wise and returns a third array as result.

Syntax:
Array3=Array1-Array2
Example
1
2
3
4
5
6
7
import numpy as np
array1=np.arange(100,150,10)
array2=np.array([1,2,3,4,5])
array3=array1-array2
print(array1)
print(array2)
print(array3)

Output:
[100 110 120 130 140]
[1 2 3 4 5]
[ 99 108 117 126 135]


8.4. Multiplication of two arrays

Multiplication of two arrays can be performed by applying arithmetic operator ‘*’. It multiplies the operand arrays element wise and returns a third array as result.

Syntax:
Array3=Array1*Array2
Example
1
2
3
4
5
6
import numpy as np
array1=np.arange(100,140,10).reshape(2,2)
array2=np.array([1,2,3,4]).reshape(2,2)
array3=array1*array2
print("Result of matrix1*matrix2:")
print(array3)

Output:
Result of matrix1* matrix2:
[[100 220]
 [360 520]]

8.5. Product of two matrices using '@' operator

The product of two matrices can be obtained by using @ operator.

Syntax:
Matrix3=Matrix1*Matrix2
Example
1
2
3
4
5
6
import numpy as np
array1=np.arange(100,140,10).reshape(2,2)
array2=np.array([1,2,3,4]).reshape(2,2)
array3=array1@array2
print("Result of matrix1@matrix2:")
print(array3)

Output:
Result of matrix1@matrix2:
[[430 640]
 [510 760]]


8.6. Product of two matrices using dot() function

The product of two matrices can be obtained by using dot() function also. dot() function returns the dot product result of two matrices.

Syntax:
Matrix3=Matrix1.dot(Matrix2)
Example
1
2
3
4
5
6
import numpy as np
array1=np.arange(100,140,10).reshape(2,2)
array2=np.array([1,2,3,4]).reshape(2,2)
array3=array1.dot(array2)
print("Product of matrix1 and matrix 2 using dot ():")
print(array3)

Output:
Product of matrix1 and matrix 2 using dot ():
[[430 640]
 [510 760]]

8.7. Application of relational operators on arrays

Relational operators are used to compare two values. All the relational operators of Python (>,<,>=,<=,== and !=) can be applied on numpy arrays. The relational operators compare each element in the array with a specified value and returns list of Boolean values (True or False) as a result of comparison of every element in the array with the value to be compared.

Syntax:
Array_name<operator>value
Example 1
1
2
3
4
import numpy as np
array1=np.arange(100,150,10)
print(array1)
print(array1<130)

Output:
[100 110 120 130 140]
[ True  True  True False False]
Example 2
1
2
3
import numpy as np
array1=np.arange(100,200,10)
print(array1!=100)

Output:
[False  True  True  True  True  True  True  True  True  True]


8.8. Applying compound assignment operators on different types of arrays

Application of compound assignment operators on different types of arrays result in casting error.

Example
1
2
3
4
5
import numpy as np
arr1 = np.ones((3, 3), dtype=int)
arr2=np.arange(1,10.0)
arr1+=arr2
print(arr1)

Error:
Traceback (most recent call last):
  File "C:/Users/Soni's-PC/PycharmProjects/untitled/pycharm.py", line 6, in <module>
    arr1+=arr2
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' output from dtype
('float64') to dtype('int32') with casting rule 'same_kind'

8.9. Unary operations

Unary operations like calculating the sum of all the elements in the array are implemented as functions in NumPy. These functions can operate on whole array and returns a single result if the axis is not specified.

sum()

sum() function returns sum of all the elements in the array.

Syntax:
array_name.sum()
Example
1
2
3
4
import numpy as np
arr=np.arange(1,16).reshape(3,5)
print(arr)
print("Sum of the given array elements is:",arr.sum())

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
Sum of the given array elements is: 120
min()

min() function returns the smallest element in the array.

Syntax:
array_name.min()
Example
1
2
3
4
import numpy as np
arr=np.arange(1,16).reshape(3,5)
print(arr)
print("Minimum element  is:",arr.min())

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
Minimum element is: 1
max()

max() function returns the largest element in the array.

Syntax:
array_name.max()
Example
1
2
3
4
import numpy as np
arr=np.arange(1,16).reshape(3,5)
print(arr)
print("Maximum element is:",arr.max())

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
Maximum element is: 15

8.10. Unary operations on arrays with axis specification

Unary functions operate on the whole array by default. But the specification of axis parameter in the unary functions method call enables the processing of the particular axis of the array. These functions processing every axis individually and return the result specific to that axis. axis 0 denotes the columns and axis 1 represents rows.

Example
1
2
3
4
5
6
import numpy as np
arr=np.arange(1,16).reshape(3,5)
print(arr)
print("Sum of elements in every column is:",arr.sum(axis=0))#axis 0 denotes the column
print("Minimum element in every row is:",arr.min(axis=1))#axis 1 denotes the row
print("Maximum element in every column is:",arr.max(axis=0))

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
Sum of elements in every column is: [18 21 24 27 30]
Minimum element in every row is: [ 1  6 11]
Maximum element in every column is: [11 12 13 14 15]

8.11. Cumulative sum of elements in the array

numpy.cumsum() function is used to calculate the cumulative sum of every axis in the array. The cumulative sum along the specified axis will be calculated and returned if the axis is specified.

Syntax:
arrayname.cumsum(axis=0(column)/1(row))
Example 1
1
2
3
4
5
import numpy as np
arr=np.arange(1,16).reshape(3,5)
print(arr)
print("Cumulative sum of each column is:",arr.cumsum(axis=0))
print("Cumulative sum of each row is:",arr.cumsum(axis=1))

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
Cumulative sum of each column is: [[ 1  2  3  4  5]
 [ 7  9 11 13 15]
 [18 21 24 27 30]]
Cumulative sum of each row is: [[ 1  3  6 10 15]
 [ 6 13 21 30 40]
 [11 23 36 50 65]]

If the axis is not specified then the cumulative sum along row by row will be calculated.

Example 2
1
2
3
4
import numpy as np
arr=np.arange(1,16).reshape(3,5)
print(arr)
print("Cumulative sum is:",arr.cumsum())

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
Cumulative sum is: [  1   3   6  10  15  21  28  36  45  55  66  78  91 105 120]


9.1. Compound assignment operations

Compound assignment operators modify the operand array instead of creating a new copy of the array. Compound assignment operators are the combination of arithmetic and assignment operators. It takes a copy of the array, modify it and assign it back to the original array. So in contrast to applying arithmetic operators on arrays, it modifies the input operand array.

9.2 Adding values to array elements using +=

Compound assignment operator += adds some fixed value to the existing contents of the array. So it modifies the input array.

Syntax:
Array1+=value
Example
1
2
3
4
import numpy as np
array1=np.arange(100,150,10)
array1+=2
print(array1)

Output:
[102 112 122 132 142]

9.3. Subtracting values from the array elements using -=

-= operator can be used to subtract some value from each element in the array.

Syntax:
Array1-=value
Example
1
2
3
4
5
import numpy as np
array1=np.arange(100,200,10)
print(array1)
array1-=10
print(array1)

Output:
[100 110 120 130 140 150 160 170 180 190]
[ 90 100 110 120 130 140 150 160 170 180]


9.4. Multiplying a matrix by a value using *=

The compound assignment operator *= can be used to multiply all the elements of a matrix by a value.

Syntax:
Matrix*=value
Example
1
2
3
4
5
6
import numpy as np
matrix1=np.array([10,20,30,40]).reshape(2,2)
print(matrix1)
print("Matrix after multiplication by 2:")
matrix1*=2
print(matrix1)

Output:
[[10 20]
 [30 40]]
Matrix after multiplication by 2:
[[20 40]
 [60 80]]


9.5. Adding two matrices using +=

+= operator can be used to add one matrix with another matrix.

Syntax:
Matrix1+=Matrix2

The contents of Matrix2 are added with the contents of Matrix1. Matrix1 contains the result addition and Matrix2 is not modified.

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import numpy as np
matrix1=np.array([10,20,30,40]).reshape(2,2)
print("matrix1:")
print(matrix1)
matrix2=np.arange(1,5).reshape(2,2)
print("matrix2:")
print(matrix2)
matrix1+=matrix2
print("matrix1 after addition with matrix2:")
print(matrix1)

Output:
matrix1:
[[10 20]
 [30 40]]
matrix2:
[[1 2]
 [3 4]]
matrix1 after addition with matrix2:
[[11 22]
 [33 44]]

10. Joining arrays

Joining arrays is the process of concatenating two or more arrays and to form a new array. In numpy the arrays are joined based on axes. Numpy has an in-built function concatenate() which accepts two are more arrays in a tuple as argument and the axis. 0 is considered as axis if axis is not passed explicitly.

Syntax:
numpy.concatenate((array1,array2,...),axis)
Example 1
1
2
3
4
5
import numpy as np
arr1=np.arange(1,5)
arr2=np.arange(5,9)
arr3=np.concatenate((arr1,arr2))
print(arr3)

Output:
[1 2 3 4 5 6 7 8]
Example 2
1
2
3
4
5
import numpy as np
arr1=np.array([[1,2],[3,4]])
arr2=np.array([[5,6],[7,8]])
arr3=np.concatenate((arr1,arr2),axis=0)
print(arr3)

Output:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
Example 3
1
2
3
4
5
import numpy as np
arr1=np.array([[1,2],[3,4]])
arr2=np.array([[5,6],[7,8]])
arr3=np.concatenate((arr1,arr2),axis=1)
print(arr3)

Output:
[[1 2 5 6]
 [3 4 7 8]]

11.1. Array splitting
Splitting process splits one array into multiple smaller arrays. Numpy has a in-built array_split() function which accepts the array to split and the number of splits.

11.2. Splitting 1-D Arrays

Splitting 1-D array requires the array_split() to be called with array to split and the number of splits.

Syntax:
numpy.array_split(array_name,number_of_splits)
Example 1
1
2
3
4
import numpy as np
arr1=np.array([1,2,3,4,5,6])
arr2=np.array_split(arr1,3)
print(arr2)

Output:
[array([1, 2]), array([3, 4]), array([5, 6])]

If the array has less number of elements than required, then the split will adjust the array elements from the end accordingly.

Example 2
1
2
3
4
import numpy as np
arr1=np.array([1,2,3,4,5,6])
arr2=np.array_split(arr1,4)
print(arr2)

Output:
[array([1, 2]), array([3, 4]), array([5]), array([6])]

11.3. Splitting 2-D Arrays

Splitting 2-D arrays is similar to splitting 1-D arrays. array_split() function takes the 2-D array to split as argument and return the array of split arrays.

Syntax:
numpy.array_split(array_to_split,number_of_splits,axis=value)
Example 1
1
2
3
4
import numpy as np
arr1=np.array([[2,4],[6,8],[10,12],[14,16]])
arr2=np.array_split(arr1,2)
print(arr2)

Output:
[array([[2, 4],
       [6, 8]]), array([[10, 12],
       [14, 16]])]
Splitting the 2-D arrays along the rows

The 2-D arrays can be splitted along the rows by calling array_split() function with array name, number of splits and axis=1 where 1 denotes the row axis in 2-D arrays.

Example
1
2
3
4
import numpy as np
arr1=np.array([[2,4,5],[6,8,9],[10,12,13],[14,16,17]])
arr2=np.array_split(arr1,2,axis=1)
print(arr2)

Output:
[array([[ 2,  4],
       [ 6,  8],
	   [10, 12],
	   [14, 16]]), array([[ 5],
	   [ 9],
	   [13],
	   [17]])]

11.4. Accessing subarrays using index

The array_split() function returns the array of sub-arrays. The subarrays can be accessed using the index in the resultant array.

Example
1
2
3
4
5
6
import numpy as np
arr1=np.array([1,2,3,4,5,6])
arr2=np.array_split(arr1,4)
print(arr2)
print(arr2[1])
print(arr2[2])

Output:
[array([1, 2]), array([3, 4]), array([5]), array([6])]
[3 4]
[5]

12.1. Array copying

NumPy supports copying the content of one array into another array. There are three ways of copying arrays:

  • Using assignment operator
  • Shallow copy
  • Deep copy
12.2. Using assignment operator

Copy of array can be created by using = operator. The assignment operator does not create another copy of the array in the memory. Instead it creates another reference to the original copy of the array.

Syntax:
new_array=old_array
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import numpy as np
a=np.array([1,2,3,4,5,6,7,8,9,10])
b=a
print(id(a))
for i in a:
    print(i,end=' ')
print()
print(id(b))
for i in b:
    print(i,end=' ')

Output:
1530348586224
1 2 3 4 5 6 7 8 9 10 
1530348586224
1 2 3 4 5 6 7 8 9 10

12.3. Shallow copy

Shallow copy of an array is created by creating a new collection object, fill it with references to the elements of original array. The copies of child objects are not created instead copies of references are created. So any change made to original array will reflect in the copy.

Shallow copy of an array can be created by view() function.

Syntax:
new_array=old_array.view()
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import numpy as np
a=np.array([1,2,3,4,5,6,7,8,9,10])
b=a.view()
print(id(a))
for i in a:
    print(i,end=' ')
print()
print(id(b))
b[2]=11
for i in a:
    print(i,end=' ')

Output:
1522094374144
1 2 3 4 5 6 7 8 9 10 
1522094264848
1 2 11 4 5 6 7 8 9 10

12.4. Deep copy

Deep copy of an array is created by creating a new collection object and to populate the new collection object with copies of original array objects. Changes made to the copy array will not reflect in the original array. copy() method in NumPy is doing the role of creating a deep copy of the given array.

Syntax:
new_array=old_array.copy()
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import numpy as np
a=np.array([1,2,3,4,5,6,7,8,9,10])
b=a.copy()
print(id(a))
for i in a:
    print(i,end=' ')
print()
print(id(b))
b[2]=11
for i in a:
    print(i,end=' ')

Output:
2706079874304
1 2 3 4 5 6 7 8 9 10 
2706079765008
1 2 3 4 5 6 7 8 9 10

13.1. Search

Search is the process of finding if an element is present in the given array. where() method in Numpy performs the role searching. It accepts an array in which the search is to be carried out and the element to search and returns the tuple of indexes where the specified element is found.

Syntax:
numpy.where(array_name==element_to_search)
Example 1
1
2
3
4
import numpy as np
arr=np.array([2,4,6,4,8,4,10])
index=np.where(arr==4)
print(index)

Output:
(array([1, 3, 5]),)

where() method can be customized to check for the condition to be satisfied. The following snippet uses the where method to find the indexes where the odd elements are available.

Example 2
1
2
3
4
import numpy as np
arr=np.array([1,2,3,4,5,6,7,8,9,10])
index=np.where(arr%2==1)
print(index)

Output:
(array([0, 2, 4, 6, 8]),)

13.2. Binary search

Binary search is the process of searching the given element in a sorted array in a fashion that every time the array size is reduced to half in every round.

The following is the binary search algorithm:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
start=0
end=number_of_elements-1
Repeat until start<=end
{
 mid=(start+end)/2	
 if ele==array[mid]
  print "search successful"
 else if ele>array[mid]
  start=mid+1
 else
  end=mid-1
}
searchsorted()

searchsorted() method in numpy carries out the binary search on the given sorted array and returns the appropriate index at which the new element to be there in the array in sorted order.

Syntax:
numpy.searchsorted(array_name,value)
Example
1
2
3
4
import numpy as np
arr=np.array([1,2,3,4,5,6,7,8,9,10])
index=np.searchsorted(arr,8)
print("The element is at index:",index)

Output:
The element is at index: 7

14.1. Sorting an array

sort() function in numpy arranging the elements in the array in the ascending, descending or alphabetical orders. It returns a copy of original array in sorted fashion and leave the original array unchanged.

Syntax:
numpy.sort(array_name)
14.2. Sorting 1-D Array

Sorting is the process of arranging elements in the array in an ordered fashion according to ascending, descending or alphabetical order.

Example
1
2
3
4
5
6
7
import numpy as np
arr1=np.array([2,1,4,3,6,5,8,7,10,9])
print("Array before sorting:")
print(arr1)
arr1=np.sort(arr1)
print("Array after sorting:")
print(arr1)
Output:
Array before sorting:
[ 2  1  4  3  6  5  8  7 10  9]
Array after sorting:
[ 1  2  3  4  5  6  7  8  9 10]

14.3. Sorting strings in alphabetical order

sort() can arrange the strings in alphabetical order.

Example
1
2
3
4
5
6
7
import numpy as np
fruits=np.array(["Pineapple","Mango","Orange"])
print("Array before sorting:")
print(fruits)
fruits=np.sort(fruits)
print("Array after sorting:")
print(fruits)

Output:
Array before sorting:
['Pineapple' 'Mango' 'Orange']
Array after sorting:
['Mango' 'Orange' 'Pineapple']

14.4. Sorting 2-D array

Applying sort() function on a 2-D array sorts both the arrays.

Example
1
2
3
4
5
6
7
import numpy as np
arr=np.array([[1,0,5],[3,2,4],[6,8,7]])
print("Array before sorting:")
print(arr)
arr=np.sort(arr)
print("Array after sorting:")
print(arr)

Output:
Array before sorting:
[[1 0 5]
 [3 2 4]
 [6 8 7]]
Array after sorting:
[[0 1 5]
 [2 3 4]
 [6 7 8]]

15. Mean

Mean is the average of a set of values in an array. Numpy provides a function to find the mean of all the values in the given array.

Syntax:
Mean_varaible=numpy.mean(array)
Example
1
2
3
4
import numpy as np
weight=np.array([67,75,82,38,90,74,69,80,71,97])
m=np.mean(weight)
print("Mean of given weights is:",m)

Output:
Mean of given weights is: 74.3

16. Median

Median is value at the middle of all the elements in the array after the elements are sorted.

Consider the following sorted array:

38,67,69,71,74,75,80,82,90,97

The median of an array with odd number of elements is at index (n-1)/2 and the median of an array with even number of elements is at two indexes (n-1)/2 and n/2. When two medians are there for an array the average of the medians are taken as the median of the array. So the medians are 74 and 75. Average of medians is 74.5.

NumPy has a built-in method median() to find the median of the given array.

Syntax:
Median_variable=numpy.median(array)
Example
1
2
3
4
import numpy as np
weight=np.array([67,75,82,38,90,74,69,80,71,97])
middle=np.median(weight)
print("Median of given weights is:",middle)

Output:
Median of given weights is: 74.5


17. Standard Deviation

Standard Deviation is the number that describes how spread the values of array are from the mean. Low Standard Deviation denotes that most of the values are close to the mean. Large Standard Deviation denotes that most of the values are spread over a high range.

Consider the speed of seven motor bikes:

Bike_speed=[86,87,88,86,87,85,86]

The Standard Deviation of above array is 0.9, which denotes that all the values are within the range of 0.9 from the mean value 86.4

NumPy provides a in-built method std() to find the Standard Deviation of given array.

Syntax:
Standard_deviation=numpy.std(array)
Example
1
2
3
4
import numpy as np
weight=np.array([67,75,82,38,90,74,69,82,71,97])
sd=np.std(weight)
print("Standard Deviation of given weights is:",sd)

Output:
Standard Deviation of given weights is: 15.134397906755327


18. Variance

Variance is a number that expresses the spread of values from the mean. Square root of variance yields Standard Deviation. The square of the Standard Deviation is considered as variance.

The following are the steps to be followed to find the variance of the given array.

Consider the following array as sample:

weight=[67,75,82,38,90,74,69,82,71,97]
  1. Find the mean of the given array.

    (67+75+82+38+90+74+69+82+71+97)/10=74.5
    
  2. For each value in the array find the difference from the mean.

    67-74.5=-7.5
    75-74.5=0.5
    82-74.5=7.5
    38-74.5=-36.5
    90-74.5=15.5
    74-74.5=-0.5
    69-74.5=-5.5
    82-74.5=7.5
    71-74.5=-3.5
    97-74.5=22.5
    
  3. For each difference find the square value.

    -7.5=56.25
    0.5=0.25
    7.5=56.25
    -36.5=1332.25
    15.5=240.25
    -0.5=0.25
    -5.5=30.25
    7.5=56.25
    -3.5=12.25
    22.5=506.25
    
  4. Variance is the average of these squared differences.

    -7.5=56.25
    0.5=0.25
    7.5=56.25
    -36.5=1332.25
    15.5=240.25
    -0.5=0.25
    -5.5=30.25
    7.5=56.25
    -3.5=12.25
    22.5=506.25
    

NumPy has a in-built method var() to calculate variance.

Syntax:
array_name.var(array)
Example
1
2
3
4
import numpy as np
weight=np.array([67,75,82,38,90,74,69,82,71,97])
variance=weight.var()
print("Variance of the given array is:",variance)

Output:
Variance of the given array is: 229.05




I Hope You Like this Post 


आपको आर्टिकल कैसा लगा? अपनी राय अवश्य दें
Please don't Add spam links,
if you want backlinks from my blog contact me on rakeshmgs.in@gmail.com