Bird
Raised Fist0
SciPydata~20 mins

SciPy vs NumPy relationship - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SciPy vs NumPy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding the relationship between SciPy and NumPy

Which statement best describes the relationship between SciPy and NumPy?

ASciPy is built on top of NumPy and extends its functionality with additional scientific computing tools.
BNumPy is a subset of SciPy that focuses only on matrix operations.
CNumPy is built on top of SciPy to provide basic array operations.
DSciPy and NumPy are completely independent libraries with no overlap in functionality.
Attempts:
2 left
💡 Hint

Think about which library provides the core array data structure and which adds more specialized functions.

❓ Predict Output
intermediate
2:00remaining
Output of NumPy and SciPy array operations

What is the output of the following code?

SciPy
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))
A[[ 0.5 0. ] [ 0. 0.5]]
B[[ 2. -1. ] [-1.5 0.5]]
C[[-2. 1. ] [ 1.5 -0.5]]
DRaises a TypeError because linalg.inv does not accept NumPy arrays
Attempts:
2 left
💡 Hint

Recall that linalg.inv computes the inverse of a matrix.

❓ data_output
advanced
2:00remaining
Comparing mean calculation with NumPy and SciPy

Given the array data = np.array([1, 2, 3, 4, 5]), what is the output of the following code?

SciPy
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))
ARaises an AttributeError because stats.tmean is deprecated
B(3.0, 2.5)
C(2.5, 3.0)
D(3.0, 3.0)
Attempts:
2 left
💡 Hint

Both functions calculate the mean but may have different default behaviors.

❓ visualization
advanced
2:30remaining
Plotting with NumPy and SciPy integration

Which option correctly creates a plot of a sine wave using NumPy for data and SciPy for signal processing?

SciPy
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()
APlots two lines: the original sine wave and a smoothed version using Savitzky-Golay filter.
BPlots only the original sine wave without any filtering.
CRaises a NameError because signal.savgol_filter is not imported.
DPlots a cosine wave instead of sine due to a code mistake.
Attempts:
2 left
💡 Hint

Check how signal.savgol_filter is used to smooth data.

🔧 Debug
expert
2:30remaining
Identifying the error in combining SciPy and NumPy functions

What error does the following code produce?

SciPy
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)
ATypeError because optimize.root expects a list, not a function.
BNo error; prints the root near 0.
CValueError because the initial guess 0 is invalid.
DAttributeError because np.sin and np.cos are not recognized inside the function.
Attempts:
2 left
💡 Hint

Check how optimize.root works with functions and initial guesses.

Practice

(1/5)
1. What is the main difference between NumPy and SciPy?
easy
A. NumPy provides basic array and math operations; SciPy adds advanced scientific tools.
B. SciPy is for arrays; NumPy is for plotting graphs.
C. NumPy is a web framework; SciPy is a database tool.
D. SciPy replaces NumPy completely.

Solution

  1. Step 1: Understand NumPy's role

    NumPy mainly handles arrays and simple math functions.
  2. Step 2: Understand SciPy's role

    SciPy builds on NumPy and adds more advanced scientific and math tools.
  3. Final Answer:

    NumPy provides basic array and math operations; SciPy adds advanced scientific tools. -> Option A
  4. Quick Check:

    NumPy = basic math, SciPy = advanced tools [OK]
Hint: Remember: SciPy extends NumPy's math abilities [OK]
Common Mistakes:
  • Confusing SciPy as a replacement for NumPy
  • Thinking NumPy is for plotting
  • Believing SciPy is unrelated to math
2. Which import statement correctly imports both NumPy and SciPy libraries?
easy
A. import numpy scipy
B. import numpy import scipy as sp
C. import numpy as np; import scipy
D. import scipy as np import numpy as sp

Solution

  1. Step 1: Check correct syntax for importing NumPy

    Common practice is import numpy as np.
  2. Step 2: Check correct syntax for importing SciPy

    Standard import is import scipy or import scipy as sp. import numpy as np; import scipy uses valid syntax for both.
  3. Final Answer:

    import numpy as np; import scipy -> Option C
  4. Quick Check:

    Correct import syntax = import numpy as np; import scipy [OK]
Hint: Use 'as' to alias libraries properly [OK]
Common Mistakes:
  • Mixing aliases between NumPy and SciPy
  • Missing commas or semicolons
  • Incorrect import syntax like 'import numpy scipy'
3. What will be the output of this code snippet?
import numpy as np
from scipy import integrate

result = integrate.quad(lambda x: np.exp(-x**2), 0, 1)
print(round(result[0], 4))
medium
A. 1.0000
B. 0.7468
C. 0.5000
D. Error: integrate.quad not found

Solution

  1. Step 1: Understand the function being integrated

    The function is exp(-x^2), integrated from 0 to 1.
  2. Step 2: Use SciPy's integrate.quad to compute the integral

    integrate.quad returns a tuple; the first element is the integral value. The integral of exp(-x^2) from 0 to 1 is approximately 0.7468.
  3. Final Answer:

    0.7468 -> Option B
  4. Quick Check:

    Integral of exp(-x^2) from 0 to 1 ≈ 0.7468 [OK]
Hint: Remember integrate.quad returns (value, error) tuple [OK]
Common Mistakes:
  • Confusing the tuple output and printing the whole tuple
  • Using wrong integration limits
  • Expecting exact 1.0 instead of approximate value
4. Identify the error in this code using SciPy and NumPy:
import numpy as np
from scipy import optimize

x = np.array([1, 2, 3])
result = optimize.minimize(np.sum, x)
print(result.fun)
medium
A. np.sum is not a valid function for minimize; it needs a function of one variable.
B. np.array should be a list, not an array.
C. optimize.minimize requires two arguments, but only one is given.
D. No error; code runs fine.

Solution

  1. Step 1: Verify imports and variable definition

    Imports are correct; x is a valid NumPy array.
  2. Step 2: Analyze optimize.minimize call

    np.sum is not a valid objective function for minimize because it does not take a single vector argument and return a scalar in the expected way. It sums all elements immediately, so it does not behave as a function of a variable vector for optimization.
  3. Final Answer:

    np.sum is not a valid function for minimize; it needs a function of one variable. -> Option A
  4. Quick Check:

    Objective function must accept a vector input and return scalar [OK]
Hint: Objective function for minimize must accept vector input [OK]
Common Mistakes:
  • Thinking np.sum is invalid for vector optimization
  • Believing np.array must be a list
  • Misreading argument count for minimize
5. You want to solve the equation sin(x) = 0.5 using SciPy, but only have NumPy installed. What is the best approach?
hard
A. You cannot solve equations without SciPy.
B. Use SciPy's optimize.root_scalar function directly.
C. Use NumPy's np.optimize module to solve the equation.
D. Use NumPy's np.sin with a manual search or guess method to find x.

Solution

  1. Step 1: Recognize SciPy provides root-finding tools

    SciPy has functions like optimize.root_scalar to solve equations easily.
  2. Step 2: Understand NumPy's limitation

    NumPy does not have root-finding functions, so you must use manual methods like searching or guessing with np.sin.
  3. Final Answer:

    Use NumPy's np.sin with a manual search or guess method to find x. -> Option D
  4. Quick Check:

    NumPy lacks root solvers; manual search needed [OK]
Hint: SciPy adds root solvers; NumPy alone needs manual methods [OK]
Common Mistakes:
  • Assuming NumPy has optimize module
  • Expecting SciPy functions without installing SciPy
  • Thinking equations can't be solved without SciPy
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard