NumPy Universal Functions : Exercises, Practice, and Solutions
NumPy Universal Functions (ufuncs) [ 20 exercises with solution]
Practice NumPy universal functions ("ufuncs") with the following exercises and solutions. Learn to create, use, and understand "ufuncs" and their broadcasting behavior.
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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:
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics