Bird
Raised Fist0
SciPydata~20 mins

Matrix creation and operations in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Matrix Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of matrix multiplication with SciPy
What is the output of the following code that multiplies two sparse matrices?
SciPy
import numpy as np
from scipy.sparse import csr_matrix

A = csr_matrix(np.array([[1, 0, 2], [0, 3, 0]]))
B = csr_matrix(np.array([[0, 3], [1, 0], [4, 5]]))
C = A.dot(B)
print(C.toarray())
A
[[8 13]
 [0 0]]
B
[[8 10]
 [3 0]]
C
[[8 13]
 [3 0]]
D
[[8 13]
 [0 3]]
Attempts:
2 left
💡 Hint
Remember that matrix multiplication sums the products of row elements of A with column elements of B.
❓ data_output
intermediate
1:30remaining
Shape and type of a sparse matrix
After running this code, what are the shape and type of the matrix M?
SciPy
from scipy.sparse import lil_matrix

M = lil_matrix((3, 4))
M[1, 2] = 5
M[2, 3] = 10
print(M.shape, type(M))
A(3, 4) <class 'numpy.ndarray'>
B(4, 3) <class 'numpy.ndarray'>
C(4, 3) <class 'scipy.sparse.lil_matrix'>
D(3, 4) <class 'scipy.sparse.lil_matrix'>
Attempts:
2 left
💡 Hint
The shape is set when creating the matrix. The type depends on the sparse format used.
🔧 Debug
advanced
1:30remaining
Identify the error in matrix addition
What error will this code raise when adding two sparse matrices of different shapes?
SciPy
from scipy.sparse import csr_matrix

A = csr_matrix([[1, 2], [3, 4]])
B = csr_matrix([[5, 6, 7], [8, 9, 10]])
C = A + B
AValueError: matrix dimension mismatch
BValueError: matrices are not aligned
CTypeError: unsupported operand type(s) for +
DNo error, addition succeeds
Attempts:
2 left
💡 Hint
Check if the shapes of the matrices are compatible for addition.
❓ visualization
advanced
2:00remaining
Visualizing a sparse matrix
Which option correctly creates a visualization of the nonzero elements of a sparse matrix using matplotlib?
SciPy
import matplotlib.pyplot as plt
from scipy.sparse import coo_matrix

row = [0, 1, 2, 0]
col = [0, 2, 2, 1]
data = [4, 5, 7, 9]
M = coo_matrix((data, (row, col)), shape=(3, 3))

plt.figure()
# Fill in the missing code here
plt.show()
Aplt.spy(M)
Bplt.imshow(M.toarray())
Cplt.scatter(M.row, M.col, s=100, c='red')
Dplt.plot(M.data)
Attempts:
2 left
💡 Hint
Use a function designed to show sparsity patterns.
🧠 Conceptual
expert
2:30remaining
Memory efficiency of sparse matrix formats
Which sparse matrix format is generally most memory efficient for fast arithmetic operations on large, sparse matrices?
ALIL (List of Lists)
BCSR (Compressed Sparse Row)
CCOO (Coordinate format)
DDOK (Dictionary of Keys)
Attempts:
2 left
💡 Hint
Consider which format is optimized for arithmetic and slicing.

Practice

(1/5)
1. What does the scipy.linalg.inv() function do when applied to a square matrix?
easy
A. Transposes the matrix
B. Calculates the determinant of the matrix
C. Calculates the inverse of the matrix
D. Creates a zero matrix of the same size

Solution

  1. Step 1: Understand the function purpose

    The scipy.linalg.inv() function is designed to find the inverse of a square matrix if it exists.
  2. Step 2: Differentiate from other matrix operations

    Determinant calculation uses scipy.linalg.det(), transpose uses .T, and zero matrix creation uses numpy.zeros().
  3. Final Answer:

    Calculates the inverse of the matrix -> Option C
  4. Quick Check:

    Inverse = inv() [OK]
Hint: Inverse means flipping matrix; use inv() for that [OK]
Common Mistakes:
  • Confusing inverse with determinant
  • Thinking inv() transposes matrix
  • Assuming inv() creates zero matrix
2. Which of the following is the correct way to create a 3x3 identity matrix using NumPy?
easy
A. scipy.identity(3)
B. scipy.eye(3)
C. scipy.linalg.eye(3)
D. np.identity(3)

Solution

  1. Step 1: Identify the correct module and function

    The identity matrix function is in numpy (imported as np) as identity().
  2. Step 2: Check other options for correctness

    scipy.eye() and scipy.linalg.eye() do not exist; scipy.identity() is not valid.
  3. Final Answer:

    np.identity(3) -> Option D
  4. Quick Check:

    Identity matrix = np.identity() [OK]
Hint: Use np.identity() for identity matrix [OK]
Common Mistakes:
  • Using scipy.eye() which does not exist
  • Calling identity() from wrong module
  • Confusing identity with eye from numpy
3. What is the output of the following code?
import numpy as np
from scipy.linalg import det
A = np.array([[1, 2], [3, 4]])
print(round(det(A), 2))
medium
A. -2.00
B. 2.00
C. 10.00
D. -10.00

Solution

  1. Step 1: Calculate determinant of matrix A

    Determinant formula for 2x2 matrix [[a,b],[c,d]] is ad - bc = 1*4 - 2*3 = 4 - 6 = -2.
  2. Step 2: Round the result to 2 decimals

    Rounding -2 to 2 decimals remains -2.00.
  3. Final Answer:

    -2.00 -> Option A
  4. Quick Check:

    Determinant = -2.00 [OK]
Hint: Determinant 2x2 = ad - bc [OK]
Common Mistakes:
  • Calculating ad + bc instead of ad - bc
  • Forgetting negative sign
  • Not rounding output
4. Identify the error in this code snippet:
import numpy as np
from scipy.linalg import inv
B = np.array([[1, 2], [2, 4]])
B_inv = inv(B)
print(B_inv)
medium
A. Matrix B is singular and cannot be inverted
B. Syntax error in array creation
C. inv() function is not imported correctly
D. Matrix B is not square

Solution

  1. Step 1: Check matrix properties

    Matrix B is square (2x2), but rows are linearly dependent (second row is twice the first).
  2. Step 2: Understand invertibility

    Such a matrix is singular (determinant zero), so inverse does not exist, causing an error when calling inv().
  3. Final Answer:

    Matrix B is singular and cannot be inverted -> Option A
  4. Quick Check:

    Singular matrix = no inverse [OK]
Hint: Check determinant; zero means no inverse [OK]
Common Mistakes:
  • Thinking non-square matrix causes error here
  • Assuming inv() import is wrong
  • Missing linear dependence in matrix
5. You have two matrices:
M = np.array([[1, 2], [3, 4]])
N = np.array([[2, 0], [1, 2]])

Which code correctly computes the product P = M x N using scipy?
hard
A. from scipy.linalg import dot P = dot(M, N)
B. import numpy as np P = np.dot(M, N)
C. from scipy.linalg import matmul P = matmul(M, N)
D. from scipy.linalg import inv P = inv(M) * N

Solution

  1. Step 1: Identify correct matrix multiplication function

    Scipy.linalg does not have dot or matmul functions; numpy provides np.dot() for matrix multiplication.
  2. Step 2: Check options for correctness

    import numpy as np P = np.dot(M, N) uses np.dot(M, N) which correctly multiplies matrices M and N. from scipy.linalg import inv P = inv(M) * N incorrectly uses inverse and element-wise multiplication.
  3. Final Answer:

    import numpy as np P = np.dot(M, N) -> Option B
  4. Quick Check:

    Matrix product = np.dot() [OK]
Hint: Use np.dot() for matrix multiplication [OK]
Common Mistakes:
  • Using inv() instead of multiplication
  • Assuming scipy.linalg has dot or matmul
  • Using * for matrix multiplication
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