Creating Arrays
Build arrays without typing every value: zeros, ones, full, arange, linspace, eye, and random.
You rarely type array values by hand. Real work needs a thousand zeros to fill in, or a hundred evenly spaced points for a chart axis. NumPy has a builder for each of those.
import numpy as np
print(np.zeros(3)) # [0. 0. 0.] three zeros
print(np.ones(3)) # [1. 1. 1.] three ones
print(np.full(3, 7)) # [7 7 7] three of any value
print(np.arange(0, 6)) # [0 1 2 3 4 5] like range(), as an arraynp.zeros(n) and np.ones(n) make arrays pre-filled with 0s or 1s; np.full(n, value) fills with any value you pick. np.arange works just like Python's built-in range() but hands you an array instead of a lazy range object. (You can pass a shape like np.ones((2, 3)) to build a grid, we'll dig into shapes next lesson; here we'll keep to simple 1D arrays.)np.arange(start, stop, step) counts like range(). What does this print?import numpy as np
print(np.arange(0, 10, 2))- A[0 2 4 6 8]
- B[0 2 4 6 8 10]
- C[2 4 6 8 10]
- D[0 1 2 3 4 5 6 7 8 9]
Now the builder that trips everyone up. np.linspace(start, stop, num) gives you num evenly spaced points between two ends, and unlike arange, it INCLUDES the stop.
print(np.linspace(0, 1, 5)) # [0. 0.25 0.5 0.75 1. ]The two look similar but ask different questions:
np.arange(start, stop, step): what STEP SIZE? The stop is exclusive. Great for integer sequences.np.linspace(start, stop, num): how MANY points? The stop is inclusive. Perfect for a smooth chart axis where you want an exact number of ticks including both ends.
Memory hook: aRANGE = step (like range), linSPACE = number of points.
linspace asks for a COUNT of points and includes both ends. What prints?import numpy as np
print(np.linspace(0, 1, 5))- A[0. 0.25 0.5 0.75 1. ]
- B[0. 0.2 0.4 0.6 0.8]
- C[0. 0.25 0.5 0.75]
- D[0 1 2 3 4]
You want exactly 100 evenly spaced points from 0 to 1 to draw a smooth curve, with both 0 and 1 included. Which do you use?
- Anp.linspace(0, 1, 100), because it takes a COUNT of points and includes the endpoint
- Bnp.arange(0, 1, 100), because it takes a count of points
- Cnp.arange(0, 1, 0.01), the only way to get decimals
- Dnp.zeros(100), then fill it in by hand
A few more builders you'll reach for constantly:
np.eye(3)makes an identity matrix, a square grid with 1s down the diagonal and 0s everywhere else (a staple of linear-algebra math)np.random.rand(3)gives 3 random decimals between 0 and 1;np.random.randint(0, 10, 5)gives 5 random whole numbers from 0 up to (not including) 10- Watch the type:
np.zeros(3)andnp.ones(3)return floats by default,[0. 0. 0.], not[0 0 0]
Random builders are everywhere in AI: shuffling data, initialising a model's starting weights, sampling.
Fill in the builder that makes exactly 4 evenly spaced points from 0 to 30, including 30. Write just the function name (without np.).
axis = np.____(0, 30, 4) # [ 0. 10. 20. 30.]np.zeros(n),np.ones(n), andnp.full(n, value)build pre-filled arrays without typing every element, andzeros/onesare floats by defaultnp.arange(start, stop, step)is likerange(): it takes a STEP and the stop is exclusivenp.linspace(start, stop, num)takes a COUNT of points and the stop is inclusive, perfect for chart axesnp.eye(n)is the identity matrix;np.random.rand/randintmake random arrays (seed them for reproducibility)- aRANGE = step, linSPACE = number of points, don't mix them up
Next up: the lesson the old course skipped, what a dimension actually is and how to read an array's shape.