Which statement best describes the relationship between SciPy and NumPy?
Think about which library provides the core array data structure and which adds more specialized functions.
NumPy provides the core array data structure and basic operations. SciPy builds on NumPy by adding more advanced scientific functions like optimization, integration, and statistics.
What is the output of the following code?
import numpy as np from scipy import linalg arr = np.array([[1, 2], [3, 4]]) inv_arr = linalg.inv(arr) print(np.round(inv_arr, 2))
Recall that linalg.inv computes the inverse of a matrix.
The inverse of the matrix [[1, 2], [3, 4]] is [[-2, 1], [1.5, -0.5]]. NumPy arrays work perfectly with SciPy's linear algebra functions.
Given the array data = np.array([1, 2, 3, 4, 5]), what is the output of the following code?
import numpy as np from scipy import stats data = np.array([1, 2, 3, 4, 5]) mean_np = np.mean(data) mean_scipy = stats.tmean(data) print((mean_np, mean_scipy))
Both functions calculate the mean but may have different default behaviors.
Both np.mean and stats.tmean compute the mean of the array. Since no limits are set for tmean, it returns the same mean as np.mean.
Which option correctly creates a plot of a sine wave using NumPy for data and SciPy for signal processing?
import numpy as np import matplotlib.pyplot as plt from scipy import signal x = np.linspace(0, 2 * np.pi, 100) sine_wave = np.sin(x) filtered_wave = signal.savgol_filter(sine_wave, 11, 3) plt.plot(x, sine_wave, label='Original') plt.plot(x, filtered_wave, label='Filtered') plt.legend() plt.show()
Check how signal.savgol_filter is used to smooth data.
The code uses NumPy to create the sine wave data and SciPy's savgol_filter to smooth it. Both lines are plotted with labels.
What error does the following code produce?
import numpy as np from scipy import optimize def f(x): return np.sin(x) + np.cos(x) result = optimize.root(f, 0) print(result.x)
Check how optimize.root works with functions and initial guesses.
The code correctly defines a function using NumPy's sin and cos, then finds a root near 0 using SciPy's optimize.root. It runs without error and prints the root.