Array shapes and dtypes
Quick answer
An ndarray is NumPy’s multidimensional array of elements with a common dtype (data type). For tabular ML, use rows for samples and columns for features. Shape records the length of each axis, not its meaning. A (3, 2) array holds three samples with two features each under this convention. NumPy is an external dependency: the examples below run locally with Python 3.13.5 and NumPy 2.3.5, not a verified ByteMind editor.
Read a feature matrix
The first column is temperature and the second is vibration. np.array converts a rectangular nested list to an array; dtype selects its storage type. ndim counts axes and size counts elements.
import numpy as npx = np.array([[20, 4], [22, 5], [24, 6]], dtype=np.float64)print(x.shape, x.ndim, x.size, x.dtype)print(x[1, 0])Expected output (local NumPy 2.3.5):
(3, 2) 2 6 float6422.0There are six numbers and two axes. Index 1 refers to the second sample; column 0 is temperature.
Choose division and reshape deliberately
The / operator performs true division, whereas // floors the quotient (including for negative values). reshape changes dimensions without changing the element count; astype explicitly converts storage type.
import numpy as npx = np.array([5, -5, 8, 0], dtype=np.int32)print((x / 2).tolist())print((x // 2).tolist())print(x.astype(np.float64).reshape(2, 2).tolist())Expected output (local NumPy 2.3.5):
[2.5, -2.5, 4.0, 0.0][2, -3, 4, 0][[5.0, -5.0], [8.0, 0.0]]A reshape changes how values are indexed, not their total count. Conversion to integers can discard fractions; fixed-width arithmetic can overflow.
Common mistakes
An array shaped (n,) is not the same as (n, 1); a one-dimensional transpose does not make a column. Ragged lists do not form rectangular feature matrices; do not force dtype=object to hide inconsistent records. NumPy integer widths are fixed, unlike Python integers. Avoid accidental integer output buffers for fractional results. astype may allocate a new buffer.
Practice and next steps
Predict shape, ndim and size for four rows with three features. Then create the matrix using float64 and check the result. Next, select samples and features without accidentally modifying the source.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Array shapes and dtypes — knowledge check
Apply array reasoning to new synthetic feature tables.
A feature matrix has 6 samples and 4 features. What is its shape?
What is the size of a (2, 3, 4) ndarray?
What is (-5) // 2?
Which shape represents a column with three values?
Why avoid dtype=object for irregular numeric feature rows?