Project: a sales simulator
Last updated
Time to put Stage 2 together. Before the real Tindahan data is clean, you'll build a stand-in: a year of daily rice sales for five stores, made with NumPy. It needs base demand per store, a weekly pattern, random noise and a seed, and then you'll ask it questions, each answered in one line.
By the end of this lesson you can
- Build a
(stores, days)array from a per-store row and a weekly pattern, by broadcasting - Add repeatable random noise with a seeded generator
- Answer questions with aggregations along an axis,
argmaxandreshape - Say which results come from the simulator’s design, and which are only noise
Plan it
The Tindahan stores' real sales are still messy (you listed the problems in project 1.P). While they're cleaned, the team wants something to practise charts and reports on: a simulator that makes believable sales.
Daily sales are built from three things, multiplied together:
- Base demand: how much a store usually sells. Batangas is the busiest, at 48 kg a day; Rosario the quietest, at 24.
- A weekly pattern: quieter on Tuesdays (85% of usual), busiest on Saturdays (130%).
- Noise: every day differs a little at random, about 10% either way.
The result is one array, shape (5, 364): five stores, 52 weeks of days. 364 rather than 365, so the year is exactly 52 weeks; that makes the reshaping in step 4 simpler.
Then four questions: which store sells most, on which day each store does best, which day of the week that is, and which month is busiest. Each answer should be one line.
Base demand and the weekly pattern
Run the cells.
-
Tindahan sales simulator
A year of daily rice sales for five stores, made up with NumPy, to practise on before the real data.
-
-
-
-
basehas one number per store,(5,).weeklyhas one per weekday,(7,).np.tile(weekly, 52)repeats the week 52 times, one number per day of the year:(364,).base[:, np.newaxis] * patternis lesson 2.9: a(5, 1)column times a(364,)row makes the whole(5, 364)table. Every store's base, times every day's pattern.- The last cell is Batangas's first week: 48 × 0.9 = 43.2 on Monday, up to 48 × 1.3 = 62.4 on Saturday.
That's the expected sales: what each store would sell if every week were the same. Real weeks aren't.
Random noise with a seed
Now make every day a little different. Run the cells.
rng.normal(1, 0.1, size=expected.shape)draws one random multiplier for every store and day, around 1, with a standard deviation of 0.1. So most days land within 10% of expected.- Multiplying gives the sales, rounded to 0.1 kg like a real scale.
- The seed,
2024, means the "random" year is the same on every run and every computer. Without it, every answer in step 4 would change each time someone opened the notebook (lesson 2.10). - The last cell is a sanity check: the smallest day is 15.6 kg (a quiet Rosario Tuesday) and the biggest 73.2 (a busy Batangas Saturday). Nothing negative and nothing absurd. Always look at the extremes of made-up data before you trust it.
Ask it questions
Each question is one line. Run them one at a time, and before each one say what shape the answer will have.
-
Tindahan sales simulator: questions
Each answer comes from one line of NumPy.
-
-
-
-
-
-
-
What I found
-
- Which store sells most?
sales.sum(axis=1)adds across the days (axis 1), one total per store.argmaxsays where the biggest is, andstores[...]turns the position into a name: Batangas, about 17,390 kg in the year. - Which day is each store's best?
sales.argmax(axis=1): five day numbers, one per store. - Which day of the week is that? Day 0 is a Monday, so the remainder after dividing by 7 is the weekday: 0 for Monday, up to 6 for Sunday. All five are 5: Saturday.
- Which month is busiest?
reshape(5, 13, 28)splits each store's 364 days into 13 four-week "months", andsum(axis=2)totals each one,(5, 13). Adding up the stores and takingargmaxgives month 5, the sixth.
Now one of your own: on which weekday does each store have its worst day? Write it in a new cell. (Hint: change one word in questions 2 and 3.)
Write what you found
Under the last cell of the notebook in step 4 is a What I found note. Click Edit note and write each answer in a sentence. Then think about one more thing, because it's the most important line in the notebook.
Two of the answers come from how you built the simulator: Batangas has the biggest base, and Saturday has the biggest weekly multiplier. You'd get them from any seed.
But nothing in the simulator makes any month busier than another. There's no season in it: every week has the same pattern. So "month 6 is busiest" is pure noise. Change the seed to 2025 and run all, and a different month will win. Write that down too: a finding that changes when only the random numbers change isn't a finding. You'll learn to measure this properly in Stage 8, but the habit starts here.
Finished
Press Restart and run all on the notebook in step 4. With the seed set, the answers are the same as before, on any computer.
Look at what you used:
- Arrays and shapes (2.1 to 2.4): a
(5, 364)table, andreshapeinto(5, 13, 28). - Axes and aggregations (2.4 and 2.8): totals per store, per month, and
argmax. - Broadcasting (2.9): a column of bases times a row of days.
- Random numbers with a seed (2.10): noise that's the same on every run.
- And the question every analyst should ask of any result: is this from the data, or from chance?
In Stage 3 you'll meet pandas, which puts names on the rows and columns so you don't have to remember that position 5 is Saturday. Underneath, it's still these arrays.
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: numpy.tile: repeating an array
- NumPy: Broadcasting: a column times a row
- NumPy: Random Generator: normal, and seeding with default_rng
- Harris et al., Array programming with NumPy, Nature 585, 2020: the array ideas this stage was built on