Complete the code to create a ufunc from a Python function that adds 5 to a number.
import numpy as np def add_five(x): return x + 5 add_five_ufunc = np.frompyfunc(add_five, 1, [1]) result = add_five_ufunc(10) print(result)
The function add_five takes 1 input argument and returns 1 output, so nin=1 and nout=1. Here, nout is 1.
Complete the code to create a ufunc that takes two inputs and returns their product.
import numpy as np def multiply(x, y): return x * y multiply_ufunc = np.frompyfunc(multiply, [1], 1) result = multiply_ufunc(3, 4) print(result)
The function multiply takes 2 input arguments, so nin=2.
Fix the error in the code by completing the blank with the correct number of outputs for the ufunc.
import numpy as np def split_number(x): return x // 10, x % 10 split_ufunc = np.frompyfunc(split_number, 1, [1]) result = split_ufunc(34) print(result)
The function split_number returns two values, so nout must be 2.
Fill both blanks to create a ufunc that returns True if the first number is greater than the second.
import numpy as np def is_greater(x, y): return x [1] y greater_ufunc = np.frompyfunc(is_greater, [2], 1) result = greater_ufunc(5, 3) print(result)
The function compares if x is greater than y, so the operator is >. The function takes two inputs, so nin=2.
Fill both blanks to create a dictionary with the word and its length if length is greater than 3.
words = ['cat', 'elephant', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
The code creates a dictionary comprehension, so it starts with {. The value is the length of the word, so len(word). The condition checks if length is greater than 3, so >.