How to Map a Function Over a Numpy Array?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

The core library for scientific computing in Python is called NumPy. It offers a multidimensional array object with outstanding speed as well as capabilities for interacting with these arrays. NumPy contains a lot of features to store our data. One such feature is the NumPy array.

A NumPy array is a grid of different values of the same datatype, indexed by a tuple of whole numbers. It's dimensions are called rank, and the dimension of the array is called the shape of the array.

Numpy in Python allows us to carry out a wide range of operations. NumPy aids in reducing the amount of time and code required to do various jobs. One of the most basic operations when working with arrays is mapping a function over an array.

To make our life easier, NumPy has three methods to help us map a function over an array; using vectorize(), with lambda keyword and by using an array as the parameter of a function to map over a NumPy array.

Using the NumPy vectorize() Function

The NumPy vectorize() function helps us to map different functions on various data-structures that contain a sequence of objects like arrays and dictionaries. NumPy vectorize() takes in an argument, which will be the function that we want to apply in a NumPy array.

Output

Explanation

We created an array with the help of np.array(), and then created a function to get the squares of any number. Using the vectorize() function, we created a function that is ready to be mapped. Then we use the new function to get all the squares of the array.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

With the Lambda Keyword in Python

The lambda keyword helps us to create an anonymous function in Python. The functions that do not necessarily have a name but can work like a regular function are called anonymous functions. We use the lambda function to map a function to an array.

Output

Explanation

Using the lambda keyword, we create an anonymous function that simply adds 10 to a value. Then, we use the lambda function to map the function over our array.

Using an array as the parameter of a function to map over a NumPy array

In this example, we will use a function that takes in an array and use that to map over a NumPy array.

Output

Explanation

In this example, we create a function double(), which doubles a number. After that, we use our input array as a parameter and create another array that stores the double of the input array.

Examples

  • Square root of the elements of an array
  • Obtaining the Absolute Value of elements in an array

Conclusion

  • In this article, we covered the different methods by which we can map a function to an array.
  • We use the vectorize() function to achieve this, as it helps to map different functions on various data-structures like arrays and lists.
  • Apart from this, we also use the lambda keyword, as it helps us to create anonymous functions. We use these anonymous functions to map all over our array elements.
  • To conclude, we map a function on an array using the array as the parameter of a function.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more