Read Shape, Dtype, and Axis Meaning
Read an array's stored structure and state what each axis means in the problem before selecting or calculating.
An array stores values in a numerical layout. Before computing with it, inspect both the layout and what each direction means in the problem.
Read Shape, Dimensions, and Size
Use the same measurement table from the previous lessons:
shape: (3, 2)
dimensions: 2
elements: 6
These three attributes answer different questions:
| Attribute | Result | Meaning |
|---|---|---|
shape | (3, 2) | length along each axis, in axis order |
ndim | 2 | number of axes |
size | 6 | total number of elements |
The shape is a tuple because an array can have more than one axis. Multiplying the axis lengths gives the size: .
Do not use size as a substitute for shape. Arrays with shapes (3, 2) and
(2, 3) both contain six values, but their layouts differ.
Q1. Distinguish shape from size
For an array with shape (3, 2), which statement is correct?
Select one choice, then check.
HintAsk two separate questions
The number of entries in (3, 2) is the number of axes. The product
is the number of elements.
SolutionTwo axes contain six elements
ndim is 2 because the shape has two axis lengths. size is 6 because
.
Give Every Axis a Meaning
Axis numbers describe storage directions. The program or dataset description must supply their meaning.
For this table, we choose:
| Axis | Length | Meaning |
|---|---|---|
0 | 3 | observations |
1 | 2 | sensors within each observation |
The shape can therefore be read as
(observations, sensors) = (3, 2)
Axis 0 is the first position in the shape tuple, not “the horizontal axis” in
every program. Axis 1 is the second position. Whether rows mean observations,
people, images, or days depends on the data contract. Here each row is one
observation and its two entries are the two sensor readings.
Writing the semantic names beside the shape prevents a common error: code can be valid Python and still operate along the wrong real-world direction. Later lessons will select and summarize values along these axes, so the names should be settled first.
Q2. Name axes from the data contract
A table has shape (120, 3). The dataset description says that each row is one
observation and each column is one sensor. Which axis naming is correct?
Select one choice, then check.
HintAlign the shape with rows and columns
(120, 3) means 120 positions along axis 0 and 3 positions along axis 1.
SolutionRows are observations here
Axis 0 means observations and has length 120. Axis 1 means sensors and
has length 3. Those meanings come from the dataset description, not from
NumPy alone.
Inspect and Convert the Dtype Deliberately
An array's dtype describes the representation used for all its elements:
print(readings.dtype)
For these decimal readings, NumPy normally creates a floating-point array. A
specific environment may display a precision in the name, such as float64.
NumPy must choose one dtype for the complete array. If numerical input mixes integers and decimal values, it infers a type that can represent both:
[18. 20.5 22. ]
float64
The integer-looking values are represented in the same floating-point dtype as
20.5. The exact dtype name can depend on the input and environment, so inspect
it rather than assuming.
Sometimes validated numerical values arrive as text:
This is a text array, not a numerical one. If the program has already established
that every item is valid numerical text, astype(float) can make the required
conversion explicit:
[18.5 20. 21.5]
float64
astype returns an array with the requested dtype. It is not a repair for
unknown or malformed data: converting "missing" to float raises an error.
Validate the boundary first, then convert for a stated numerical reason.
Later numerical work will rely on the dtype when it interprets a computation. Here the important habit is smaller: inspect the dtype, explain why it exists, and convert only when the program needs a different representation.
Q3. Convert validated numerical text
The three strings have already been validated as decimal readings. Convert the array to floating-point values without changing its shape.
Editable Python
Ready to run.
HintAsk for a floating-point dtype
Use numeric_readings = text_readings.astype(float). The method returns the
converted array.
SolutionConvert after validation
numeric_readings = text_readings.astype(float)
The resulting dtype has floating-point kind f, its shape remains (3,),
and its last value is the number 21.5.
shape records the length of every axis, ndim counts those axes, size
counts all elements, and dtype records their shared numerical
representation. Give each axis a domain meaning and treat conversion as an
explicit decision. With that structure clear, positions can select values
without guessing what a row or column represents.