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.
- 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.
- 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.
- Scientific and mathematical Python-based packages are using NumPy arrays
- Element-by-element operations are the “default mode” when an ndarray is involved.
- 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.
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:
- ndarray.ndim-Number of axes (dimensions) of the array.
- 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.
- ndarray.size-The total number of elements of the array. This is equal to the product of the elements of shape.
- 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
- 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).
- ndarray.data the buffer containing the actual elements of the array.
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 )
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.
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.
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.
1 2 3 | import numpy as np ndarray=np.arange(1,15,2,int) print(ndarray) |
[ 1 3 5 7 9 11 13]
1 2 3 4 | import numpy as np ndarray=np.arange(1,41,1,int) ndarray.shape=(5,8) print(ndarray) |
[[ 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.
1 2 3 | import numpy as np ndarray=np.arange(1,15.0,2) print(ndarray) |
[ 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.
1 2 3 4 | import numpy as np numarray=np.arange(1,16) print(numarray) print(type(numarray)) |
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] <class 'numpy.ndarray'>
array() is an in-built method in numpy to create an array with the given list of elements.
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
1 2 3 4 | import numpy as np arr=np.array([1.5,3.2,4.7]) print(arr) print(arr.dtype) |
[1.5 3.2 4.7] float64
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) |
[[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.
1 2 3 | import numpy as np arr=np.array(1,2,3,4) print(arr) |
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.
1 2 3 | import numpy as np arr=np.array([(1,2,3),(4,5,6),(7,8,9)]) print(arr) |
[[1 2 3] [4 5 6] [7 8 9]]
The data type of the array elements can be explicitly mentioned while the array creation.
1 2 3 | import numpy as np arr=np.array([(1,2),(4,5),(7,8)],dtype=complex) print(arr) |
[[1.+0.j 2.+0.j] [4.+0.j 5.+0.j] [7.+0.j 8.+0.j]]
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.
Numpy.zeros((<shape of the array>)
- Shape of the array denotes the length of the array in every dimension separated by comma.
1 2 3 | import numpy as np arr=np.zeros((3,3)) print(arr) |
[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
1 2 3 | import numpy as np arr=np.zeros((3,3),dtype=int) print(arr) |
[[0 0 0] [0 0 0] [0 0 0]]
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.
Numpy.ones((<shape of the array>)
- Shape of the array denotes the length of the array in every dimension separated by comma.
1 2 3 | import numpy as np arr=np.ones((3,3),dtype=int) print(arr) |
[[1 1 1] [1 1 1] [1 1 1]]
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.
Numpy.empty((<shape of the array>)
- Shape of the array denotes the length of the array in every dimension separated by comma.
1 2 3 | import numpy as np arr=np.empty((3,3),dtype=int) print(arr) |
[[476741369956 103 0] [416611827811 116 0] [416611827826 420906795106 498216206441]]
linspace() is a array creation method in Numpy library. It is creating an array with evenly spaced values within specified interval.
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.
1 2 3 | import numpy as np arr=np.linspace(1,50,50) print(arr) |
[ 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.]
reshape() function is used to give a new shape to an array without changing its data.
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.
1 2 3 | import numpy as np numarray=np.arange(1,17).reshape(4,4) print(numarray) |
[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]]
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.
1 2 3 | import numpy as np arr=np.arange(15).reshape(3,5) print(arr) |
[[ 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.
1 2 3 | import numpy as np arr=np.arange(10000).reshape(100,100) print(arr) |
[[ 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:
numpy_object.set_printoptions(threshold=sys.maxsize)
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) |
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.
array_name[index]
1 2 3 | import numpy as np arr=np.array([10,20,30,40,50]) print(arr[2]) |
30
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 (,).
array_name[dimension,index]
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]) |
Element at Dimension 1 and index 2: 8
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.
array_name[negative_index]
1 2 3 | import numpy as np arr=np.arange(1,16) print("The element at index -2:",arr[-2]) |
The element at index -2: 14
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.
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.
Array3=Array1+Array2
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) |
First Array [[100 110] [120 130]] Second Array [[1 2] [3 4]] Third Array [[101 112] [123 134]]
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.
Array3=Array1-Array2
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) |
[100 110 120 130 140] [1 2 3 4 5] [ 99 108 117 126 135]
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.
Array3=Array1*Array2
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) |
Result of matrix1* matrix2: [[100 220] [360 520]]
The product of two matrices can be obtained by using @ operator.
Matrix3=Matrix1*Matrix2
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) |
Result of matrix1@matrix2: [[430 640] [510 760]]
The product of two matrices can be obtained by using dot() function also. dot() function returns the dot product result of two matrices.
Matrix3=Matrix1.dot(Matrix2)
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) |
Product of matrix1 and matrix 2 using dot (): [[430 640] [510 760]]
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.
Array_name<operator>value
1 2 3 4 | import numpy as np array1=np.arange(100,150,10) print(array1) print(array1<130) |
[100 110 120 130 140] [ True True True False False]
1 2 3 | import numpy as np array1=np.arange(100,200,10) print(array1!=100) |
[False True True True True True True True True True]
Application of compound assignment operators on different types of arrays result in casting error.
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) |
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'
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() function returns sum of all the elements in the array.
array_name.sum()
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()) |
[[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15]] Sum of the given array elements is: 120
min() function returns the smallest element in the array.
array_name.min()
1 2 3 4 | import numpy as np arr=np.arange(1,16).reshape(3,5) print(arr) print("Minimum element is:",arr.min()) |
[[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15]] Minimum element is: 1
max() function returns the largest element in the array.
array_name.max()
1 2 3 4 | import numpy as np arr=np.arange(1,16).reshape(3,5) print(arr) print("Maximum element is:",arr.max()) |
[[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15]] Maximum element is: 15
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.
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)) |
[[ 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]
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.
arrayname.cumsum(axis=0(column)/1(row))
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)) |
[[ 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.
1 2 3 4 | import numpy as np arr=np.arange(1,16).reshape(3,5) print(arr) print("Cumulative sum is:",arr.cumsum()) |
[[ 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]
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.
Compound assignment operator += adds some fixed value to the existing contents of the array. So it modifies the input array.
Array1+=value
1 2 3 4 | import numpy as np array1=np.arange(100,150,10) array1+=2 print(array1) |
[102 112 122 132 142]
-= operator can be used to subtract some value from each element in the array.
Array1-=value
1 2 3 4 5 | import numpy as np array1=np.arange(100,200,10) print(array1) array1-=10 print(array1) |
[100 110 120 130 140 150 160 170 180 190] [ 90 100 110 120 130 140 150 160 170 180]
The compound assignment operator *= can be used to multiply all the elements of a matrix by a value.
Matrix*=value
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) |
[[10 20] [30 40]] Matrix after multiplication by 2: [[20 40] [60 80]]
+= operator can be used to add one matrix with another matrix.
Matrix1+=Matrix2
The contents of Matrix2 are added with the contents of Matrix1. Matrix1 contains the result addition and Matrix2 is not modified.
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) |
matrix1: [[10 20] [30 40]] matrix2: [[1 2] [3 4]] matrix1 after addition with matrix2: [[11 22] [33 44]]
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.
numpy.concatenate((array1,array2,...),axis)
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) |
[1 2 3 4 5 6 7 8]
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) |
[[1 2] [3 4] [5 6] [7 8]]
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) |
[[1 2 5 6] [3 4 7 8]]
Splitting 1-D array requires the array_split() to be called with array to split and the number of splits.
numpy.array_split(array_name,number_of_splits)
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) |
[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.
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) |
[array([1, 2]), array([3, 4]), array([5]), array([6])]
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.
numpy.array_split(array_to_split,number_of_splits,axis=value)
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) |
[array([[2, 4], [6, 8]]), array([[10, 12], [14, 16]])]
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.
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) |
[array([[ 2, 4], [ 6, 8], [10, 12], [14, 16]]), array([[ 5], [ 9], [13], [17]])]
The array_split() function returns the array of sub-arrays. The subarrays can be accessed using the index in the resultant array.
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]) |
[array([1, 2]), array([3, 4]), array([5]), array([6])] [3 4] [5]
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
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.
new_array=old_array
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=' ') |
1530348586224 1 2 3 4 5 6 7 8 9 10 1530348586224 1 2 3 4 5 6 7 8 9 10
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.
new_array=old_array.view()
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=' ') |
1522094374144 1 2 3 4 5 6 7 8 9 10 1522094264848 1 2 11 4 5 6 7 8 9 10
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.
new_array=old_array.copy()
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=' ') |
2706079874304 1 2 3 4 5 6 7 8 9 10 2706079765008 1 2 3 4 5 6 7 8 9 10
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.
numpy.where(array_name==element_to_search)
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) |
(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.
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) |
(array([0, 2, 4, 6, 8]),)
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.
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() 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.
numpy.searchsorted(array_name,value)
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) |
The element is at index: 7
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.
numpy.sort(array_name)
Sorting is the process of arranging elements in the array in an ordered fashion according to ascending, descending or alphabetical order.
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) |
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]
sort() can arrange the strings in alphabetical order.
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) |
Array before sorting: ['Pineapple' 'Mango' 'Orange'] Array after sorting: ['Mango' 'Orange' 'Pineapple']
Applying sort() function on a 2-D array sorts both the arrays.
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) |
Array before sorting: [[1 0 5] [3 2 4] [6 8 7]] Array after sorting: [[0 1 5] [2 3 4] [6 7 8]]
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.
Mean_varaible=numpy.mean(array)
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) |
Mean of given weights is: 74.3
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.
Median_variable=numpy.median(array)
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) |
Median of given weights is: 74.5
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.
Standard_deviation=numpy.std(array)
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) |
Standard Deviation of given weights is: 15.134397906755327
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]
Find the mean of the given array.
(67+75+82+38+90+74+69+82+71+97)/10=74.5
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
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
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.
array_name.var(array)
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) |
Variance of the given array is: 229.05
0 Comments
Please don't Add spam links,
if you want backlinks from my blog contact me on rakeshmgs.in@gmail.com