Create your own universal function in NumPy Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report Universal functions in NumPy are simple mathematical functions. It is just a term that we gave to mathematical functions in the Numpy library. Numpy provides various universal functions that cover a wide variety of operations. However, we can create our universal function in Python. In this article, we will see how to create our own universal function using NumPy. Create Our Own Universal Function in NumPyBelow are the ways by which we can create our universal function in NumPy: Using frompyfunc() FunctionUsing numpy.vectorize() FunctionCreating Our Own Universal Function Using frompyfunc() methodNumpy.frompyfunc() function allows to creation of an arbitrary Python function as Numpy ufunc (universal function). In this example, a custom Python function fxn that calculates the modulo 2 operation is converted into a NumPy universal function using np.frompyfunc. Python3 # using numpy import numpy as np # creating own function def fxn(val): return (val % 2) # adding into numpy mod_2 = np.frompyfunc(fxn, 1, 1) # creating numpy array arr = np.arange(1, 11) print("arr :", *arr) # using function over numpy array mod_arr = mod_2(arr) print("mod_arr :", *mod_arr) Output : arr : 1 2 3 4 5 6 7 8 9 10mod_arr : 1 0 1 0 1 0 1 0 1 0Create Own Universal Function Using np.vectorize() FunctionIn this example, the Python function circle_area computes the area of a circle given its radius. Using np.vectorize(), a vectorized universal function circle_ufunc is created from this Python function. When applied to an array radius_values containing radii, the ufunc computes the areas for each radius, producing an array areas which is then printed. Python3 import numpy as np def circle_area(radius): return np.pi * radius**2 # Create a vectorized version of the function circle_ufunc = np.vectorize(circle_area) # Test the ufunc with a single value radius_values = np.array([1, 2, 3, 4]) areas = circle_ufunc(radius_values) print(areas) Output: [ 3.14159265 12.56637061 28.27433388 50.26548246] Create Quiz Comment D deepanshu_rustagi Follow 2 Improve D deepanshu_rustagi Follow 2 Improve Article Tags : Python Python-numpy Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like