NumPy · Chapter 16 of 18
Linear algebra
By the end of this chapter you can tell from @, predict the shape of a matrix product, turn a group sum into one matrix times a vector, solve a small system with np.linalg.solve, fit a straight-line model to data with np.linalg.lstsq and a design matrix, measure how far the data sits from the model, and say in a line what det, matrixrank, eig and svd are for. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real work
- 4 tasks
- NumPy
- In the all labs plan
The first 2 chapters of NumPy are free. This one comes with the all labs plan.
Why you need this
Two questions came in this week. Meenal wants each inverter's output in every block, because the exchange pays the plant but the bank that financed block D wants its own three inverters' share. The plant has 12 inverters of 200 panels each, and 96 blocks a day: a loop over 2,400 panels per block, or a mask per inverter, works, but it is twelve operations where one would do.
Rukmini asks a harder one: "How much should we have made today?" A cloudy day makes less, and that is nobody's fault. A sunny day that makes less is dust or a fault, and Bhanwar should go and look. To tell them apart Imran needs a model: a formula that says what the plant makes for a given sun and heat, fitted to 30 days of blocks. You cannot get one by sorting, masking or averaging. You need a line through 2,880 points in the best place, which is a question of linear algebra, and NumPy answers it in one call.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
inverters.py: inverter totals with one matrix product
Write two functions in inverters.py.
inverter_map(inverter_of, n_inverters): inverter_of is an int array with one number a panel, the inverter it belongs to (0 is INV01), in panel order (P0001 first). Return an array of shape (n_inverters, number of panels) with 1 where panel j belongs to inverter i and 0 everywhere else. inverter_map(np.array([1, 0, 1]), 2) is [[0. 1. 0.] [1. 0. 1.]]. The Check tries the plant's 2,400 panels with 12 inverters, 6 panels with 3 inverters, and 3 panels that all belong to inverter 0 of 2 (so inverter 1's row is all zeros).
inverter_totals_kw(mapping, grid): mapping is a map like that; grid is one block of the day in watts, shape (rows, cols), whose panels read row by row are in the map's column order. Return each inverter's total in kW, a float array of shape (n_inverters,), with a matrix product. The Check tries the plant's (12, 2400) map with blocks 48 and 30 of the day (shape (40, 60)), and a map of 2 inverters with a (2, 2) grid.
- Task 2
model.py: fit the plant's model
Imran's model of the plant is plant_kw = a + b × irradiance + c × ambient, fitted by least squares. Write two functions in model.py.
fit_plant(irradiance, ambient, plant_kw): three 1-D arrays of the same length, one value a block. Return the least squares coefficients as an array of 3 floats in the order a (the constant), b (per W/m²), c (per °C). The Check fits the 30 days of blocks_30d.csv (2,880 blocks) and 5 blocks that lie exactly on 5 + 2 × irradiance − 3 × ambient, for which the answer is [ 5. 2. -3.].
predict_kw(coef, irradiance, ambient): coef is an array [a, b, c]; irradiance and ambient are 1-D arrays of the same length. Return the model's kW for each block, a 1-D float array of that length. The Check tries the last day's 96 blocks, 4 blocks, and coef [5, 2, -3] on 5 blocks.
- Task 3
below_model.py: the days below the model
Write worst_days(coef, irradiance, ambient, plant_kw, k) in below_model.py. coef is the model's [a, b, c] (as in task 2); irradiance, ambient and plant_kw are 1-D arrays of whole days, 96 blocks a day, the first day's 96 blocks first. A block's miss is its plant_kw minus the model's kW (a + b × irradiance + c × ambient); a day's shortfall in kWh is the sum of its 96 misses times 0.25.
Return the indexes of the k days with the lowest shortfall (furthest below the model), worst first, as an int array of k numbers (day 0 is the first day). The Check tries the 30 days of blocks_30d.csv with their own fit and k = 3 and k = 5, and 4 made-up days with k = 2.
- Task 4 · interview question
Interview: contract.py, * or @
DhoopGrid sells part of its energy on a contract with one price for each of the day's 96 blocks, the same every day. Meenal's daily_revenue(kwh, price_rs_mwh) takes kwh of shape (days, 96), each block's energy in kWh, and price_rs_mwh of shape (96,), each block's price in rupees a MWh. For 30 days it returns an array of shape (30, 96) instead of 30 revenues.
Fix it so it returns each day's revenue in rupees: the sum over the day's blocks of kWh times price, divided by 1000 (a MWh is 1000 kWh), a float array of shape (days,). The Check tries 30 days, 2 days and 1 day of 96 blocks, and 2 days of 3 blocks with 3 prices.
Imran will ask: * or @ here, and what do the shapes say?
What you will learn
- * against @: elementwise multiplication against the matrix product.
- The matrix product's shapes: (m, n) @ (n, p) is (m, p), and the inner sizes must agree.
- A matrix times a vector: a group sum, a weighted sum, in one product; np.dot and np.matmul.
- np.linalg.solve: the exact answer to a small system, and why not np.linalg.inv.
- np.linalg.lstsq: the best fit when there are more equations than unknowns, with a design matrix built by np.column_stack.
- Residuals and np.linalg.norm: how far the data is from the fit.
- det, matrix_rank, eig and svd: what each one tells you, in one section.
More in Real work
Build "Linear algebra" yourself
DhoopGrid runs a solar plant in the Thar desert with 2,400 panels. You turn its raw readings into answers. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of NumPy are free. This one comes with the all labs plan.