Compute the rank of a matrix with NumPy's matrix_rank and return a Python int
Quick Overview
Write a NumPy function that returns the rank of a 2-D matrix as a plain Python int using np.linalg.matrix_rank. It tests how numerical rank comes from singular values, how the tolerance behaves on noisy or nearly singular data, and why the return type matters.
Compute the rank of a matrix with NumPy's matrix_rank and return a Python int
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write a function `rank(A)` that returns the rank of a 2-D array-like `A`, meaning the number of linearly independent rows (equivalently, columns). Compute it with `np.linalg.matrix_rank` and return a plain Python `int`, not a NumPy scalar.
```python
A = [[1, 2], [2, 4]]
rank(A) # -> 1 (rows are linearly dependent)
B = [[1, 0], [0, 1]]
rank(B) # -> 2
```
```hint Floating point
A matrix that is singular on paper rarely produces exact zeros once rounding enters. Think about which quantity a library can compare against a threshold.
```
```hint Return type
Check the type of the value `matrix_rank` returns before you hand it back.
```
### Constraints & Assumptions
- `A` is a 2-D array-like of real numbers, such as a list of lists or a NumPy array.
- The function returns a native Python `int`.
### What a Strong Answer Covers
- Calls `np.linalg.matrix_rank` and converts the result to a Python `int`.
- Explains how `matrix_rank` decides the rank and what its default tolerance depends on.
- Explains why exact zero tests fail in floating point.
- Says when to set `tol` explicitly and how to choose it.
### Follow-up Questions
- How does `matrix_rank` decide rank, and what is the `tol` parameter?
- Why return `int(...)` instead of the raw result?
- Why is `np.linalg.det(A) == 0` a poor test for a singular matrix?
- How would you get the exact rank of a matrix of integers or fractions?
Overview: Write a NumPy function that returns the rank of a 2-D matrix as a plain Python int using np.linalg.matrix_rank. It tests how numerical rank comes from singular values, how the tolerance behaves on noisy or nearly singular data, and why the return type matters.
Write a function rank(A) that returns the rank of a 2-D array-like A, meaning the number of linearly independent rows (equivalently, columns). Compute it with np.linalg.matrix_rank and return a plain Python int, not a NumPy scalar.
A = [[1, 2], [2, 4]]
rank(A) # -> 1 (rows are linearly dependent)
B = [[1, 0], [0, 1]]
rank(B) # -> 2
Constraints & Assumptions
A
is a 2-D array-like of real numbers, such as a list of lists or a NumPy array.
The function returns a native Python
int
.
What a Strong Answer Covers Guidance
Calls
np.linalg.matrix_rank
and converts the result to a Python
int
.
Explains how
matrix_rank
decides the rank and what its default tolerance depends on.
Explains why exact zero tests fail in floating point.
Says when to set
tol
explicitly and how to choose it.
Follow-up Questions Guidance
How does
matrix_rank
decide rank, and what is the
tol
parameter?
Why return
int(...)
instead of the raw result?
Why is
np.linalg.det(A) == 0
a poor test for a singular matrix?
How would you get the exact rank of a matrix of integers or fractions?