Bird
Raised Fist0
NumPydata~20 mins

Why linear algebra matters in NumPy - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Linear Algebra Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Matrix multiplication output
What is the output of the following code that multiplies two matrices using NumPy?
NumPy
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
result = np.dot(A, B)
print(result)
A
[[2 0]
 [3 2]]
B
[[2 4]
 [3 8]]
C
[[1 2]
 [3 4]]
D
[[4 4]
 [10 8]]
Attempts:
2 left
💡 Hint
Remember matrix multiplication sums the products of rows and columns.
❓ data_output
intermediate
1:30remaining
Vector dot product result
What is the result of the dot product between vectors a and b?
NumPy
import numpy as np
a = np.array([1, 3, -5])
b = np.array([4, -2, -1])
dot_product = np.dot(a, b)
print(dot_product)
A3
B-3
C0
D10
Attempts:
2 left
💡 Hint
Multiply corresponding elements and add them up.
❓ visualization
advanced
3:00remaining
Visualizing eigenvectors and eigenvalues
Which plot correctly shows eigenvectors of matrix M scaled by their eigenvalues?
NumPy
import numpy as np
import matplotlib.pyplot as plt
M = np.array([[2, 1], [1, 2]])
eigenvalues, eigenvectors = np.linalg.eig(M)
origin = np.array([0, 0])
plt.quiver(*origin, eigenvalues * eigenvectors[0, :], eigenvalues * eigenvectors[1, :], angles='xy', scale_units='xy', scale=1, color=['r','b'])
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.grid()
plt.show()
ARed and blue arrows from origin showing directions of eigenvectors scaled by eigenvalues
BScatter plot of random points unrelated to eigenvectors
CBar chart of eigenvalues only
DLine plot of eigenvalues over index
Attempts:
2 left
💡 Hint
Eigenvectors are directions; eigenvalues scale these directions.
🧠 Conceptual
advanced
1:30remaining
Why is matrix inversion important in data science?
Which option best explains why matrix inversion is important in data science?
AIt is used to multiply matrices faster than normal multiplication.
BIt helps solve systems of linear equations, which is key in regression and optimization.
CIt converts matrices into vectors for easier analysis.
DIt removes noise from data by filtering matrix elements.
Attempts:
2 left
💡 Hint
Think about solving equations like Ax = b.
🔧 Debug
expert
2:00remaining
Identify the error in matrix multiplication code
What error does the following code raise?
NumPy
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8], [9, 10]])
result = np.dot(A, B)
print(result)
ATypeError: unsupported operand type(s) for *: 'int' and 'list'
BIndexError: index out of bounds
CValueError: shapes (2,3) and (2,2) not aligned: 3 (dim 1) != 2 (dim 0)
DNo error, prints a 2x2 matrix
Attempts:
2 left
💡 Hint
Check if the number of columns in A matches number of rows in B.

Practice

(1/5)
1.

Why is linear algebra important in data science when using numpy?

easy
A. It replaces the need for any programming language.
B. It is used only for creating visualizations.
C. It helps handle and transform large sets of numbers efficiently.
D. It is only useful for text data processing.

Solution

  1. Step 1: Understand the role of linear algebra

    Linear algebra allows us to work with vectors and matrices, which represent many numbers at once.
  2. Step 2: Connect to numpy's purpose

    NumPy uses linear algebra to efficiently perform operations on large numerical data sets.
  3. Final Answer:

    It helps handle and transform large sets of numbers efficiently. -> Option C
  4. Quick Check:

    Linear algebra = efficient number handling [OK]
Hint: Linear algebra = fast math with many numbers [OK]
Common Mistakes:
  • Thinking linear algebra is only for visuals
  • Believing it replaces programming
  • Assuming it only works with text
2.

Which of the following is the correct way to create a 2x2 matrix using numpy?

import numpy as np
matrix = ?
easy
A. np.array([[1, 2], 3, 4])
B. np.array([[1, 2], [3, 4]])
C. np.array(1, 2, 3, 4)
D. np.matrix([1, 2, 3, 4])

Solution

  1. Step 1: Recall numpy array syntax for matrices

    A 2x2 matrix requires a list of lists, each inner list is a row.
  2. Step 2: Check each option's structure

    np.array([[1, 2], [3, 4]]) uses nested lists correctly; others do not form a proper 2x2 matrix.
  3. Final Answer:

    np.array([[1, 2], [3, 4]]) -> Option B
  4. Quick Check:

    Nested lists = matrix shape [OK]
Hint: Use nested lists for matrix shape [OK]
Common Mistakes:
  • Using flat lists instead of nested
  • Missing brackets around rows
  • Confusing np.matrix with np.array
3.

What is the output of this code?

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
result = np.dot(A, B)
print(result)
medium
A. [[1 2] [3 4]]
B. [[2 0] [1 2]]
C. [[3 4] [4 6]]
D. [[4 4] [10 8]]

Solution

  1. Step 1: Understand matrix multiplication with np.dot

    np.dot multiplies matrices by summing products of rows and columns.
  2. Step 2: Calculate each element of result

    First row, first column: 1*2 + 2*1 = 4; first row, second column: 1*0 + 2*2 = 4; second row, first column: 3*2 + 4*1 = 10; second row, second column: 3*0 + 4*2 = 8.
  3. Final Answer:

    [[4 4] [10 8]] -> Option D
  4. Quick Check:

    Matrix multiplication = [[4 4], [10 8]] [OK]
Hint: Multiply rows by columns, sum products [OK]
Common Mistakes:
  • Adding matrices instead of multiplying
  • Confusing element-wise with dot product
  • Mixing up row and column indices
4.

Find the error in this code snippet that tries to multiply two matrices:

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8], [9, 10]])
result = np.dot(A, B)
print(result)
medium
A. Matrix dimensions do not align for multiplication.
B. np.dot is not the correct function for multiplication.
C. Arrays A and B must be the same shape.
D. The print statement syntax is incorrect.

Solution

  1. Step 1: Check shapes of matrices A and B

    A is 2x3, B is 2x2; for multiplication, columns of A must equal rows of B.
  2. Step 2: Identify mismatch

    Since A has 3 columns and B has 2 rows, multiplication is not possible.
  3. Final Answer:

    Matrix dimensions do not align for multiplication. -> Option A
  4. Quick Check:

    Columns A != Rows B = Error [OK]
Hint: Check matrix shapes before multiplying [OK]
Common Mistakes:
  • Ignoring shape mismatch
  • Using wrong function for multiplication
  • Assuming same shape needed for dot
5.

You have a dataset with 3 features and 4 samples stored as a 4x3 matrix. You want to center the data by subtracting the mean of each feature. Which numpy operation correctly achieves this?

import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
# What next?
hard
A. data - np.mean(data, axis=0)
B. data - np.mean(data, axis=1)
C. np.mean(data, axis=0) - data
D. np.mean(data, axis=1) - data

Solution

  1. Step 1: Understand data shape and centering

    Data shape is 4 samples x 3 features; centering means subtracting feature means from each sample.
  2. Step 2: Calculate mean along correct axis

    Axis=0 computes mean for each feature (column), which is needed to center features.
  3. Step 3: Subtract feature means from data

    Subtracting np.mean(data, axis=0) from data centers each feature.
  4. Final Answer:

    data - np.mean(data, axis=0) -> Option A
  5. Quick Check:

    Center features by subtracting column means [OK]
Hint: Subtract mean along columns (axis=0) to center features [OK]
Common Mistakes:
  • Using axis=1 subtracts row means, not features
  • Subtracting data from mean reverses centering
  • Confusing samples and features axes