Chapter 5: NumPy Creating Arrays
NumPy Creating Arrays — written as if a patient teacher is sitting next to you, showing examples on the screen, explaining why each method exists, when you should prefer one over another, and what beginners usually get wrong.
Let’s go slowly and thoroughly through the most important ways to create NumPy arrays.
|
0 1 2 3 4 5 6 |
import numpy as np |
1. The most common starting point: np.array()
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# From a Python list a = np.array([10, 20, 30, 40, 50]) print(a) # [10 20 30 40 50] # From nested lists → 2D array (matrix) b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(b) # [[1 2 3] # [4 5 6] # [7 8 9]] |
Important things to notice:
- All elements must be of compatible types (NumPy will usually convert them)
- You can mix integers and floats → becomes float
|
0 1 2 3 4 5 6 7 8 |
c = np.array([1, 2, 3.5, 4]) print(c) # [1. 2. 3.5 4. ] print(c.dtype) # float64 |
Common beginner mistake:
|
0 1 2 3 4 5 6 7 |
wrong = np.array(1, 2, 3, 4) # ← TypeError! correct = np.array([1, 2, 3, 4]) # ← must be inside a list |
2. Special creator functions – very frequently used
These are the functions you will type every single day.
np.zeros() – empty starting point, initialization
|
0 1 2 3 4 5 6 7 8 9 10 |
z1 = np.zeros(8) # 1D array of 8 zeros z2 = np.zeros((4, 6)) # 4 rows × 6 columns z3 = np.zeros((2, 3, 4)) # 3D array print(z2.dtype) # float64 (default) |
Integer version (very common):
|
0 1 2 3 4 5 6 |
zi = np.zeros((5, 5), dtype=int) # or dtype=np.int32, dtype='int8' etc. |
np.ones()
|
0 1 2 3 4 5 6 7 |
o1 = np.ones((3, 7)) o2 = np.ones(10, dtype=int) |
np.full() – fill with any value
|
0 1 2 3 4 5 6 7 8 |
f1 = np.full((4, 5), 99) # fill with integer 99 f2 = np.full((2, 8), 3.14159) # fill with float f3 = np.full((3, 3, 3), -1, dtype=np.int16) |
np.eye() – identity matrix (very useful in linear algebra)
|
0 1 2 3 4 5 6 7 |
eye3 = np.eye(3) # 3×3 identity eye5 = np.eye(5, dtype=int) |
|
0 1 2 3 4 5 6 7 8 |
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] |
3. Sequence generators – extremely common
np.arange() – like Python range(), but returns array
|
0 1 2 3 4 5 6 7 8 9 |
a = np.arange(10) # 0 .. 9 b = np.arange(5, 15) # 5 .. 14 c = np.arange(0, 10, 2) # 0,2,4,6,8 d = np.arange(1, 2, 0.1) # 1.0, 1.1, 1.2, ..., 1.9 |
Warning students always forget:
|
0 1 2 3 4 5 6 7 |
print(np.arange(0, 1, 0.3)) # [0. 0.3 0.6 0.9] ← last value 1.0 is missing due to floating point precision! |
Safer alternatives when using floats:
|
0 1 2 3 4 5 6 7 8 |
print(np.arange(0, 1.01, 0.3)) # better # or much more reliable: print(np.linspace(0, 1, 11)) # ← see next section |
np.linspace() – “linear space” – most used for plotting & testing
|
0 1 2 3 4 5 6 7 8 9 10 11 |
# Start, stop, number of points lin1 = np.linspace(0, 10, 11) # 0,1,2,...,10 lin2 = np.linspace(0, 1, 21) # 0.0, 0.05, 0.10, ..., 1.0 lin3 = np.linspace(-5, 5, 100) # very common for function plotting print(lin2) |
Key differences – memorize this table
| Feature | arange | linspace |
|---|---|---|
| Includes endpoint? | No (like range) | Yes |
| Step / num points | You give step size | You give number of points |
| Good for floats? | Can be dangerous | Designed for floats, very safe |
| Most common use | integer indices, loops | plotting, generating test points |
4. Random number generation – you will use these constantly
|
0 1 2 3 4 5 6 |
np.random.seed(42) # ← very important for reproducible experiments! |
Uniform random numbers [0, 1)
|
0 1 2 3 4 5 6 7 |
u1 = np.random.rand(5) # 5 numbers u2 = np.random.rand(4, 6) # 4×6 matrix |
Standard normal (Gaussian) distribution – mean=0, std=1
|
0 1 2 3 4 5 6 7 |
n1 = np.random.randn(1000) # very common for synthetic data n2 = np.random.randn(100, 3) # 100 samples, 3 features |
Random integers
|
0 1 2 3 4 5 6 7 |
dice = np.random.randint(1, 7, size=20) # 1 to 6 inclusive matrix_rand = np.random.randint(-10, 11, (5, 5)) # -10 to 10 |
Random choice from existing array (very useful)
|
0 1 2 3 4 5 6 7 |
names = np.array(['Alice', 'Bob', 'Charlie', 'Dana']) random_names = np.random.choice(names, size=10) |
5. Quick realistic examples you will actually write
Example 1: Preparing feature matrix (machine learning style)
|
0 1 2 3 4 5 6 7 8 9 10 |
n_samples = 1000 n_features = 25 X = np.zeros((n_samples, n_features)) # placeholder # ... later fill with real data ... |
Example 2: Time axis for plotting
|
0 1 2 3 4 5 6 7 |
t = np.linspace(0, 10, 1000) # 1000 points from 0 to 10 seconds signal = np.sin(2 * np.pi * 0.5 * t) # 0.5 Hz sine wave |
Example 3: Random test data
|
0 1 2 3 4 5 6 7 |
# 500 students, 4 subjects scores = np.random.randint(40, 101, size=(500, 4)) |
Example 4: Image placeholder
|
0 1 2 3 4 5 6 7 |
# 256×256 grayscale image img = np.zeros((256, 256), dtype=np.uint8) |
Summary – Which creation function when?
| You want to… | Best choice(s) |
|---|---|
| Create from existing data | np.array() |
| Initialize empty array with zeros | np.zeros(), np.zeros(…, dtype=int) |
| Initialize with ones | np.ones() |
| Fill with same value | np.full() |
| Need identity matrix | np.eye() |
| Want integer sequence (0,1,2,3…) | np.arange() |
| Want evenly spaced points (especially floats) | np.linspace() |
| Need random numbers between 0 and 1 | np.random.rand() |
| Need Gaussian / normal random numbers | np.random.randn() |
| Need random integers | np.random.randint() |
Would you like to go deeper into any of these topics next?
- Detailed differences between view vs copy when creating arrays
- Setting specific dtypes (int8, float32, uint16, bool…)
- Creating structured arrays (like mini data frames)
- Very common patterns in machine learning / data science
- Common mistakes & debugging tips when creating arrays
Just tell me what you want to focus on now! 😊
