In this article, we will learn how to apply a method over a NumPy Array in Python.
Table Of Contents
Given a NumPy array we need to apply the function to each and every element of the array.
For Example: Applying an add() function to a NumPy Array, which adds 10 to the given number,
Given array = [1, 2, 3, 4, 5] After adding 10 to each element of array: [11, 12, 13, 14, 15]
There are multiple ways to to apply the function to each and every element of a NumPy Array. Lets discuss all the methods one by one with proper approach and a working code example.
Apply a function over a NumPy Array using vectorized function
The numpy module has a vectorize class. It takes a python function as an argument and returns a vectorized function. This vectorized function takes a NumPy Array as argument and calls the earlier assigned function to each element of the array. Then returns a NumPy Array containing the result.
Syntax of vectorize
Frequently Asked:
- How to save Numpy Array to a CSV File using numpy.savetxt() in Python
- Python: numpy.reshape() function Tutorial with examples
- What does numpy.eye() do in Python?
- How to remove multiple elements from a NumPy array?
numpy.vectorize(pyfunc)
- Parameters:
- pyfunc = Python function or method.
- Returns:
- Returns a vectorized function.
First create a function which you want to apply over the array, then follow the following approach:
The Approach:
- Import numpy library and create numpy array.
- Create a function that you want to appply on each element of NumPy Array. For example function with name add().
- Pass this add() function to the vectorize class. It returns a vectorized function.
- Pass the NumPy Array to the vectorized function.
- The vectorized function will apply the the earlier assigned function ( add() ) to each element of the array and returns a NumPy Array containing the result.
- Print the Array.
Source Code
import numpy as np # A function to be applied to the array def add(num): return num + 10 # creating numpy array arr = np.array([1, 2, 3, 4, 5]) # printing the original array print(" The original array : " , arr) # Apply add() function to array. addTen = np.vectorize(add) arr = addTen(arr) # printing the array after applying function print(" The array after applying function : " , arr)
Output:
The original array : [1 2 3 4 5] The array after applying function : [11 12 13 14 15]
Apply a function over a NumPy Array using map() function
The python map() function takes function and an iterable as parameters. It then applies the given function on all elements of the given iterable and returns a mapped object. We can iterate over this mapped object to get all the result values or we can directly convert it into a list.
Syntax of map() function
map(function, iterator)
- Parameters:
- function = Python function or method.
- iterator = List, set, tuple.
- Returns:
- Returns an iterator.
First create a function which you want to apply over the array, and follow the following approach,
Approach:
- Import numpy library and create numpy array.
- Create a function to add a number to the functional parameter.
- Pass this function and the array to the map() function. It will return a mapped object by applying function to each element of iterator.
- Convert mapped object to list
- Convert it into an array and print it.
Source Code
import numpy as np # function to be applied to the array def add(num): return num+10 # creating numpy array arr = np.array([1, 2, 3, 4, 5]) # printing the original array print(" The original array : " , arr) # Apply add() function to array. arr = np.array(list(map(add, arr))) # printing the array after applying function print(" The array after applying function : " , arr)
Output:
The original array : [1 2 3 4 5] The array after applying function : [11 12 13 14 15]
Apply a function over a NumPy Array using Using for Loop
We can iterate over a NumPy array and apply the given function on each element one by one.
Approach:
- Import numpy library and create numpy array.
- Using a for loop and range() method iterate over the array.
- Apply the given funtion to the each element of the array
- Print the array.
Source Code
import numpy as np # function to be applied to the array def add(num): return num+10 # creating numpy array arr = np.array([1, 2, 3, 4, 5]) # printing the original array print(" The original array : " , arr) # Apply add() function to array. for i in range(0,len(arr)): arr[i] = add(arr[i]) # printing the array after applying function print(" The array after applying function : " , arr)
Output:
The original array : [1 2 3 4 5] The array after applying function : [11 12 13 14 15]
Apply a function over a NumPy Array using List Comprehension
The List comprehensions are used for creating new lists from iterables like tuples, strings, arrays, lists, They offer very small syntax. Now to apply a function all over the array. Use the List Comprehension to iterate over the array and apply the given function to each element of the numpy array.
Approach:
- Import numpy library and create numpy array.
- Using List Comprehension to iterate the array.
- Apply the given funtion to the each element of the array and get all results in a list.
- Convert it into NumPy Array and print it.
Source Code
import numpy as np # A function to be applied to the array def add(num): return num+10 # creating numpy array arr = np.array([1, 2, 3, 4, 5]) # Printing the original array print(" The original array : " , arr) # Apply add() function to array. arr = np.array([add(num) for num in arr]) # printing the array after applying function print(" The array after applying function : " , arr)
Output:
The original array : [1 2 3 4 5] The array after applying function : [11 12 13 14 15]
Summary
Great! you made it, We have discussed All possible methods to apply a method over all elements of a NumPy Array in Python. Happy learning.