Making arrays
Last updated
Most arrays come from data, but many start as something simpler: a row of zeros to fill in, a range of steps, a grid of ones. In this lesson you'll meet the functions that make arrays, load one from a CSV file, and learn which of them to trust when the steps are decimals.
By the end of this lesson you can
- Make arrays with
np.array,zeros,ones,fullandeye - Choose between
arangeandlinspacefor a range of numbers - Load a table of numbers from a CSV file with
np.loadtxt
Arrays from scratch
Step through the functions you'll use most, then run them for real.
np.zeros(5): five zeros. They’re floats (0.), because most arrays end up holding measurements.When do you need these? zeros is a place to put results you'll work out one at a time, full sets a starting value for every store, and ones and eye turn up in the maths of lesson 2.11. Notice the shape for ones is a tuple, (2, 3): rows first, then columns. Every NumPy function that takes a shape takes it this way.
Change np.full(4, 45) to np.full((2, 4), 45) and run it.
Ranges: arange and linspace
There are two ways to make a range of numbers, and they ask different questions.
np.arange(start, stop, step)works like Python'srange: you give the step, and the stop is not included.np.arange(0, 10, 2)is0, 2, 4, 6, 8.np.linspace(start, stop, num)asks how many numbers you want, and includes both ends.np.linspace(0, 1, 5)is0, 0.25, 0.5, 0.75, 1.
Use arange for whole-number steps (days 0 to 6, store numbers). Use linspace when the steps are decimals, or when you need an exact number of points, such as 50 points along a chart's axis. Step 4 shows why.
From a CSV file
Real arrays come from data. np.loadtxt reads a file of numbers into an array. The file here has a header row and a text column, which an array can't mix with numbers, so you tell it what to skip:
delimiter=",": the values are separated by commas.skiprows=1: skip the header row.usecols=(1, 2, 3): read only columns 1 to 3, leaving out column 0, the store names.
A 3 × 3 array: one row per store, one column per day. loadtxt makes floats unless you say otherwise, so 42 arrives as 42..
This is fine for a small, tidy file of numbers. Real files have text, dates and gaps in them, and for those you'll use pandas from Stage 3, which keeps each column's type.
Wrong way: the extra end point
You want the prices 1.0, 1.1 and 1.2 (VAT multipliers, say). arange with a step of 0.1, stopping before 1.3, should give three numbers. Run it.
Four numbers, with 1.3 at the end, which arange was meant to leave out. The reason is lesson 1.1's: 0.1 can't be stored exactly. Working out how many steps fit, NumPy gets , rounds that up to 4, and makes four numbers. With other values the same rounding can lose the last number instead.
The fix is the last cell: np.linspace(1, 1.2, 3) says exactly how many numbers you want, so there's nothing to round. NumPy's own documentation gives the same advice: with a step that isn't a whole number, use linspace.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: Array creation: every common way to make an array
- NumPy: numpy.arange: the warning about non-integer steps
- NumPy: numpy.linspace: a number of points, ends included
- NumPy: numpy.loadtxt: delimiter, skiprows and usecols