NumPy Universal Functions : Exercises, Practice, and Solutions
This resource offers a total of 20 NumPy Universal Functions (ufuncs) problems for practice.
[An Editor is available at the bottom of the page to write and execute the scripts.]
Practice NumPy universal functions ("ufuncs") with the following exercises and solutions. Learn to create, use, and understand "ufuncs" and their broadcasting behavior.
1. Custom Ufunc: Add 10
Write a Numpy program that creates a custom ufunc that adds 10 to every element in a NumPy array. Use this ufunc on a 1D array of integers.
Click me to see the sample solution
2. Ufunc: Square Root Calculation
Write a NumPy program that uses the np.sqrt ufunc to compute the square root of each element in a 2D NumPy array.
Click me to see the sample solution
3. Ufunc: Add 1D Array to 2D Array Rows
Write a NumPy program that creates a 2D NumPy array and a 1D array. Use the np.add ufunc to add the 1D array to each row of the 2D array.
Click me to see the sample solution
4. Ufunc: Element-wise Multiplication of 2D Arrays
Write a NumPy program that uses the np.multiply ufunc to perform element-wise multiplication of two 2D arrays of the same shape.
Click me to see the sample solution
5. Ufuncs: Trigonometric Functions on Angles
Write a NumPy program that applies np.sin, np.cos, and np.tan ufuncs to a 1D NumPy array of angles (in radians) and print the results.
Click me to see the sample solution
6. Ufunc: Conditional Array Selection with np.where
Write a NumPy program that uses the np.where ufunc to create a new array from two existing arrays based on a condition applied to a third array.
Click me to see the sample solution
7. Custom Ufunc: Quadratic Expression
Write a NumPy program that creates a custom ufunc that computes x^2 + 2x + 1 for each element in a NumPy array.
Click me to see the sample solution
8. Ufunc Chain: exp, log, and sqrt Transformations
Write a NumPy program that creates a NumPy array and applies a sequence of ufuncs (np.exp, np.log, and np.sqrt) to transform the array.
Click me to see the sample solution
9. Ufunc: Sum Reduction with np.add.reduce
Write a NumPy program that uses the np.add.reduce ufunc to compute the sum of all elements in a 1D array.
Click me to see the sample solution
10. Ufunc: Cumulative Product with np.multiply.accumulate
Write a NumPy program that uses the np.multiply.accumulate ufunc to compute the cumulative product of elements in a 1D array.
Click me to see the sample solution
11. Ufunc: Outer Subtraction with np.subtract.outer
Write a NumPy program that uses the np.subtract.outer ufunc to compute the outer subtraction of two 1D arrays.
Click me to see the sample solution
12. Ufunc: Dot Product Verification with Custom Ufunc
Write a NumPy program that creates two 1D arrays and uses np.dot to compute the dot product. Verify the result using a custom ufunc.
Click me to see the sample solution
13. Ufunc: 3D Array Division by 1D Array
Write a NumPy program that creates a 3D array and a 1D array and uses np.divide to divide each 2D slice of the 3D array by the 1D array.
Click me to see the sample solution
14. Custom Ufunc: 3x + 4y Operation
Write a Numpy program that defines a custom ufunc that computes 3x + 4y for elements x and y from two arrays, and apply it to 2D arrays.
Click me to see the sample solution
15. Ufunc Performance Comparison with Python Loop
Write a NumPy program that compares the performance of a custom ufunc to a standard Python loop for element-wise addition on large arrays.
Click me to see the sample solution
16. Ufunc In-Place Addition Using Out Parameter
Write a NumPy program that uses np.add with the out parameter to perform in-place addition on a NumPy array.
Click me to see the sample solution
17. Ufunc: Replace NaN with Specified Number
Write a NumPy program that creates an array with NaN values and uses np.nan_to_num to replace NaN with a specified number.
Click me to see the sample solution
18. Custom Ufunc for Complex Number Operations
Write a NumPy program that creates a custom ufunc to operate on complex numbers in a NumPy array.
Click me to see the sample solution
19. Ufunc: Logical AND on Boolean Arrays
Write a NumPy program that uses np.logical_and to combine two boolean arrays based on element-wise logical AND operation.
Click me to see the sample solution
20. Ufunc: Maximum Reduction Along Axis
Write a NumPy program that uses np.maximum.reduce to find the maximum element along a specified axis of a 2D array.
Click me to see the sample solution
Python-Numpy Code Editor:
More to Come !Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics