Complete worked example
python
import numpy as np
def inspect_example(rows):
data = np.array(rows, dtype=np.float64, copy=True)
if data.ndim != 2 or 0 in data.shape:
raise ValueError("expected a non-empty matrix")
return data[:2, :1].copy()- Reasoning
- The worked expression uses a row slice and a column slice together, then copies the selection before returning it. For a three-by-two input, the result has shape (2, 1). For a one-row input, it has shape (1, 1). This is an explicit interface choice, not a universal rule that every selected column should stay two-dimensional. Trace each colon separately: the first pair addresses rows and the second addresses columns. Negative indexes count from the end, but this function does not need them. Treat ellipsis and inserted axes as later tools, not as shortcuts that obscure the coordinate system in a first exercise.