3.4 Problems
Problem 1. Implement the mean squared error
both with and without using NumPy functions and methods. (The vectors x and y should be represented by NumPy arrays in both cases.)
Problem 2. Compare the performances of the built-in maximum function max and NumPy’s np.max using timeit.timeit, like we did above. Try running a different number of experiments and changing the array sizes to figure out the breakeven point between the two performances.
Problem 3. Instead of implementing the general p-norm as we did earlier in this chapter in Section 3.1.1 , we can change things around to obtain the version below.
def p_norm(x: np.ndarray, p: float):
if p 1:
return (np.sum(np.abs(x)**p))**(1/p)
elif np.isinf(p):
return np.max(np.abs(x))
else:
raise ValueError("/span>p must be a float larger or equal than 1.0 or inf."
However, this doesn’t work for p = ∞. What is the problem with it?
Problem...