Engineering Python · NumPy & pandas Basics
NumPy Arrays
In Engineering Python because ML labs pass features as arrays — ndarray, shape and dtype are the vocabulary those labs assume.
NumPy's `ndarray` is a typed, shaped block of numbers. Create arrays from lists, inspect `.shape` and `.dtype`, and prefer array ops over Python loops for numeric work.
- Engineering Python
- Medium level
- 4 concepts
1ndarray from lists
`np.array([...])` builds an array. Homogeneous dtype (often float64 or int64) lets NumPy store values compactly and compute fast.
Figure. np.array([1.0, 2.0, 3.0]) stores those three values as one homogeneous block — one dtype for every cell.
np.array
import numpy as np
a = np.array([1.0, 2.0, 3.0])
print(a)What library provides ndarray?
- NumPy
- json
- io
import numpy as np
2shape and dtype
`.shape` is a tuple of sizes along each axis. `.dtype` is the element type. Rank-1 shape `(n,)` is a vector; `(n, m)` is a matrix.
Figure. X = [[1, 2], [3, 4]] has two rows and two columns, so .shape is the tuple (2, 2). .dtype is the shared element type of those four cells.
Takeaway
- Idea`.shape` is a tuple of sizes along each axis. `.dtype` is the element type. Rank-1 shape `(n,)` is a vector; `(n, m)` is a matrix.
shape dtype
import numpy as np
X = np.array([[1, 2], [3, 4]])
print(X.shape, X.dtype)What does arr.shape return for a 2D NumPy array with 3 rows and 4 columns?
- The tuple (3, 4)
- The integer 12
- The string "3x4"
- The list [4, 3]
NumPy ndarray.shape returns a tuple of array dimensions, (n_rows, n_cols).
3Why arrays before ML
Feature matrices are 2-D arrays: rows are examples, columns are features. Getting shape right is half of debugging an ML lab.
Figure. A feature matrix is two-dimensional: each row is one example, each column is one feature. Shape is that row-by-column geometry.
Takeaway
- IdeaFeature matrices are 2-D arrays: rows are examples, columns are features. Getting shape right is half of debugging an ML lab.
4Lab: make an array
Create an array and print its shape.
Figure. np.array([[1, 2, 3], [4, 5, 6]]) is two rows by three columns, so X.shape prints (2, 3).
Takeaway
- IdeaCreate an array and print its shape.
Coding lab. Create an ndarray runs in the app, with checks on your output.
Notes
- In Engineering Python because ML labs pass features as arrays — ndarray, shape and dtype are the vocabulary those labs assume.
- `np.array([...])` builds an array. Homogeneous dtype (often float64 or int64) lets NumPy store values compactly and compute fast.
- `.shape` is a tuple of sizes along each axis. `.dtype` is the element type. Rank-1 shape `(n,)` is a vector; `(n, m)` is a matrix.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
ndarrays have shape and dtype; build them from lists; rows×cols matter for ML.
- ndarray from lists
- `np.array([...])` builds an array. Homogeneous dtype (often float64 or int64) lets NumPy store values compactly and compute fast.
- shape and dtype
- `.shape` is a tuple of sizes along each axis. `.dtype` is the element type. Rank-1 shape `(n,)` is a vector; `(n, m)` is a matrix.
- Why arrays before ML
- Feature matrices are 2-D arrays: rows are examples, columns are features. Getting shape right is half of debugging an ML lab.
- Lab: make an array
- Create an array and print its shape.
Practise NumPy Arrays
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 2 quick checks with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device