Open In App

Python: Operations on Numpy Arrays

Last Updated : 19 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

NumPy is a Python package which means ‘Numerical Python’. It is the library for logical computing, which contains a powerful n-dimensional array object, gives tools to integrate C, C++ and so on. It is likewise helpful in linear based math, arbitrary number capacity and so on. NumPy exhibits can likewise be utilized as an effective multi-dimensional compartment for generic data. NumPy Array: Numpy array is a powerful N-dimensional array object which is in the form of rows and columns. We can initialize NumPy arrays from nested Python lists and access it elements. A Numpy array on a structural level is made up of a combination of:

  • The Data pointer indicates the memory address of the first byte in the array.
  • The Data type or dtype pointer describes the kind of elements that are contained within the array.
  • The shape indicates the shape of the array.
  • The strides are the number of bytes that should be skipped in memory to go to the next element.

Operations on Numpy Array

Arithmetic Operations: 

Python3




# Python code to perform arithmetic
# operations on NumPy array
 
 
import numpy as np
 
 
# Initializing the array
arr1 = np.arange(4, dtype = np.float_).reshape(2, 2)
 
print('First array:')
print(arr1)
 
print('\nSecond array:')
arr2 = np.array([12, 12])
print(arr2)
 
print('\nAdding the two arrays:')
print(np.add(arr1, arr2))
 
print('\nSubtracting the two arrays:')
print(np.subtract(arr1, arr2))
 
print('\nMultiplying the two arrays:')
print(np.multiply(arr1, arr2))
 
print('\nDividing the two arrays:')
print(np.divide(arr1, arr2))


Output:

First array:
[[ 0.  1.]
 [ 2.  3.]]

Second array:
[12 12]

Adding the two arrays:
[[ 12.  13.]
 [ 14.  15.]]

Subtracting the two arrays:
[[-12. -11.]
 [-10.  -9.]]

Multiplying the two arrays:
[[  0.  12.]
 [ 24.  36.]]

Dividing the two arrays:
[[ 0.          0.08333333]
 [ 0.16666667  0.25      ]]

numpy.reciprocal() This function returns the reciprocal of argument, element-wise. For elements with absolute values larger than 1, the result is always 0 and for integer 0, overflow warning is issued. Example: 

Python3




# Python code to perform reciprocal operation
# on NumPy array
import numpy as np
arr = np.array([25, 1.33, 1, 1, 100])
 
print('Our array is:')
print(arr)
 
print('\nAfter applying reciprocal function:')
print(np.reciprocal(arr))
 
arr2 = np.array([25], dtype = int)
print('\nThe second array is:')
print(arr2)
 
print('\nAfter applying reciprocal function:')
print(np.reciprocal(arr2))


Output 

Our array is:
[  25.      1.33    1.      1.    100.  ]

After applying reciprocal function:
[ 0.04       0.7518797  1.         1.         0.01     ]

The second array is:
[25]

After applying reciprocal function:
[0]

numpy.power() This function treats elements in the first input array as the base and returns it raised to the power of the corresponding element in the second input array. 

Python3




# Python code to perform power operation
# on NumPy array
 
 
import numpy as np
 
 
arr = np.array([5, 10, 15])
 
print('First array is:')
print(arr)
 
print('\nApplying power function:')
print(np.power(arr, 2))
 
print('\nSecond array is:')
arr1 = np.array([1, 2, 3])
print(arr1)
 
print('\nApplying power function again:')
print(np.power(arr, arr1))


Output:

First array is:
[ 5 10 15]

Applying power function:
[ 25 100 225]

Second array is:
[1 2 3]

Applying power function again:
[   5  100 3375]

numpy.mod() This function returns the remainder of division of the corresponding elements in the input array. The function numpy.remainder() also produces the same result. 

Python3




# Python code to perform mod function
# on NumPy array
 
 
import numpy as np
 
 
arr = np.array([5, 15, 20])
arr1 = np.array([2, 5, 9])
 
print('First array:')
print(arr)
 
print('\nSecond array:')
print(arr1)
 
print('\nApplying mod() function:')
print(np.mod(arr, arr1))
 
print('\nApplying remainder() function:')
print(np.remainder(arr, arr1))


Output:

First array:
[ 5 15 20]

Second array:
[2 5 9]

Applying mod() function:
[1 0 2]

Applying remainder() function:
[1 0 2]


Previous Article
Next Article

Similar Reads

Benefit of NumPy arrays over Python arrays
The need for NumPy arises when we are working with multi-dimensional arrays. The traditional array module does not support multi-dimensional arrays. Let's first try to create a single-dimensional array (i.e one row & multiple columns) in Python without installing NumPy Package to get a more clear picture. C/C++ Code from array import * arr = ar
1 min read
Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Python | Numpy numpy.ndarray.__ge__()
With the help of numpy.ndarray.__ge__() method of Numpy, We can find that which element in an array is greater then or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ge__($self, value, /) Return: self>=value Example #1 : In this example
1 min read
Python | Numpy numpy.ndarray.__ne__()
With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__neg__()
With the help of numpy.ndarray.__neg__() method of Numpy, one can multiply each and every element of an array with -1. Hence, the resultant array having values like positive values becomes negative and negative values become positive. Syntax: ndarray.__neg__($self, /) Return: -self Example #1 : In this example we can see that after applying numpy._
1 min read
Python | Numpy numpy.ndarray.__pos__()
With the help of numpy.ndarray.__pos__() method of Numpy, one can multiply each and every element of an array with 1. Hence, the resultant array having values same as original array. Syntax: ndarray.__pos__($self, /) Return: +self Example #1 : In this example we can see that after applying numpy.__pos__(), we get the simple array that can be the sa
1 min read