You're standing on a hillside in the Western Ghats. Take one step east and the ground rises a little. Take one step north and it drops. The hill has a different slope in each direction, and to know which way to walk, you need both.
A partial derivative is the slope of a function of several variables in one direction: how fast the output changes when you nudge one input and hold all the others still. It's the ordinary derivative you already know, applied to one input at a time. Partial derivatives are how a machine learning model learns which way to adjust each of its weights: one partial derivative per weight, computed millions of times during training.
What you need first: functions of several variables (especially slices), derivative rules, the chain rule and numerical derivatives in Python.
Key terms
- Partial derivative: the rate of change of a function with respect to one input, with every other input held fixed.
- ∂ (read "partial" or "del"): the symbol for a partial derivative. ∂f/∂x is read "partial f by partial x".
- Hold constant: treat an input as a fixed number while you differentiate with respect to another one.
- Stationary point: a point where every partial derivative is zero, such as the bottom of a valley.
A plot of land
Start with something you can picture. A rectangular plot of land is 20 m long and 15 m wide, so its area is A = length × width = 300 m².
Now lengthen it by 1 m, keeping the width at 15 m. You've added a strip 1 m by 15 m: the area grows by 15 m². Widen it by 1 m instead, keeping the length at 20 m, and you add a strip 20 m by 1 m: 20 m².
Those two numbers are the partial derivatives of the area:
- ∂A/∂length = width = 15: each extra metre of length adds 15 m², if the width stays put.
- ∂A/∂width = length = 20: each extra metre of width adds 20 m², if the length stays put.
Notice that the slope in one direction depends on the other input. A wider plot gains more from extra length. That's normal for functions of several variables, and it's why you must say where you are when you give a partial derivative.
How to find a partial derivative
The rule is short: treat every other input as a constant, and differentiate as usual.
Take f(x, y) = x² + 3*y*.
- For ∂f/∂x, pretend y is a fixed number, like 5. Then 3*y* is just a constant, and its derivative is 0. The x² part gives 2*x*. So ∂f/∂x = 2*x*.
- For ∂f/∂y, pretend x is fixed. x² is a constant (derivative 0), and 3*y* gives 3. So ∂f/∂y = 3.
At the point (2, 1): ∂f/∂x = 4 and ∂f/∂y = 3. Moving in the x direction, the function rises about 4 units per unit step; in the y direction, 3.
Check it numerically
You don't need to trust the algebra. Nudge one input by a tiny amount h, keep the other one fixed, and measure the change. That's the central difference from the derivatives module, applied one input at a time:
import numpy as np
def f(x, y):
return x ** 2 + 3 * y
def partial_x(func, x, y, h=1e-5):
return (func(x + h, y) - func(x - h, y)) / (2 * h) # only x moves
def partial_y(func, x, y, h=1e-5):
return (func(x, y + h) - func(x, y - h)) / (2 * h) # only y moves
print(round(partial_x(f, 2, 1), 4), round(partial_y(f, 2, 1), 4))
4.0 3.0
Both match: 4 and 3.
More examples
The same rule works with every derivative rule you know. Here, the other variable is treated exactly like a number such as 7:
| Function | ∂/∂x | ∂/∂y | How |
|---|---|---|---|
| x²y | 2*xy* | x² | y is a constant multiplier; then x² is |
| x²y + y³ | 2*xy* | x² + 3*y*² | Each term separately |
| 5*x* − 2*y* + 7 | 5 | −2 | Constants vanish |
| e^(xy) | y e^(xy) | x e^(xy) | Chain rule: the inside xy has slope y (or x) |
| (x + 2*y*)² | 2(x + 2*y*) | 4(x + 2*y*) | Chain rule: the inside has slope 1 (or 2) |
Predict: at the point (1, 2), what are the two partial derivatives of x²y + y³? Work them out from the table, then check:
def g(x, y):
return x ** 2 * y + y ** 3
print(round(partial_x(g, 1, 2), 4), round(partial_y(g, 1, 2), 4))
4.0 13.0
From the table: 2*xy* = 2 × 1 × 2 = 4, and x² + 3*y*² = 1 + 12 = 13. The function is much more sensitive to y than to x at this point.
Reading the notation
| Written | Read aloud | Means |
|---|---|---|
| ∂f/∂x | "partial f by partial x" | Slope of f in the x direction, other inputs fixed |
| ∂L/∂w | "partial L by partial w" | How the loss changes when weight w changes a little |
| f*ₓ* | "f sub x" | Another way to write ∂f/∂x |
| d*f*/d*x* | "d f by d x" | An ordinary derivative, for a function of one input |
The curly ∂ is a reminder that other inputs exist and are being held still. If a function has only one input, ∂ and d mean the same thing.
Partial derivatives are slopes of slices
In functions of several variables you took slices of a function: hold every input fixed except one, and you get an ordinary curve. A partial derivative is the slope of that slice. Here's the study-hours model from that lesson: predicted marks = w × hours + b, with its mean squared error L(w, b).
import matplotlib.pyplot as plt
hours = np.array([2, 3, 5, 6, 8, 9, 11, 12])
marks = np.array([45, 52, 50, 60, 68, 75, 74, 84])
def loss(w, b):
return np.mean((marks - (w * hours + b)) ** 2)
w0, b0 = 3.0, 40.0
dw, db = partial_x(loss, w0, b0), partial_y(loss, w0, b0)
fig, axes = plt.subplots(1, 2, figsize=(11, 3.8))
ws = np.linspace(1.5, 5, 200)
axes[0].plot(ws, [loss(w, b0) for w in ws], color="#1c1917", linewidth=2.5)
axes[0].plot(ws, loss(w0, b0) + dw * (ws - w0), color="#c2410c", linewidth=2)
axes[0].set_xlabel(f"w (b fixed at {b0:.0f})")
axes[0].set_title(f"Slice along w: slope ∂L/∂w = {dw:.1f}")
bs = np.linspace(28, 52, 200)
axes[1].plot(bs, [loss(w0, b) for b in bs], color="#1c1917", linewidth=2.5)
axes[1].plot(bs, loss(w0, b0) + db * (bs - b0), color="#0f766e", linewidth=2)
axes[1].set_xlabel(f"b (w fixed at {w0:.0f})")
axes[1].set_title(f"Slice along b: slope ∂L/∂b = {db:.1f}")
for ax in axes:
ax.plot(w0 if ax is axes[0] else b0, loss(w0, b0), "o", color="#1c1917", markersize=8)
ax.set_ylim(0, 160)
ax.set_ylabel("loss")
plt.tight_layout()
plt.show()
Here partial_x and partial_y are reused on loss, because they work for any function of two inputs: "x" is just the first input (w) and "y" the second (b). Both slopes are negative at (3, 40): increasing w would lower the loss, and so would increasing b. The w slope is far steeper, because changing the slope of the line moves the predictions for students with many hours a lot.
The partial derivatives of the loss
For a model, you want formulas, not just numbers. Write the prediction for student i as ŷᵢ = w xᵢ + b (read "y-hat i"), and the loss as the mean of (yᵢ − ŷᵢ)². Differentiate with respect to w, holding b fixed:
- Each term is (yᵢ − ŷᵢ)², a square of an inside expression. By the chain rule: 2(yᵢ − ŷᵢ) × (slope of the inside).
- The inside is yᵢ − w xᵢ − b. With b and the data fixed, its slope with respect to w is −xᵢ.
- So each term gives −2xᵢ(yᵢ − ŷᵢ), and averaging:
∂L/∂w = −2 × mean of xᵢ(yᵢ − ŷᵢ)
For b, step 2 changes: the slope of the inside with respect to b is −1. So:
∂L/∂b = −2 × mean of (yᵢ − ŷᵢ)
Read them aloud: "the partial of the loss by w is minus twice the average of hours times error", and "the partial by b is minus twice the average error". Check both against the numerical values:
def loss_partials(w, b):
error = marks - (w * hours + b)
return -2 * np.mean(hours * error), -2 * np.mean(error)
formula_w, formula_b = loss_partials(w0, b0)
print(f"formula: dL/dw = {formula_w:.3f}, dL/db = {formula_b:.3f}")
print(f"numerical: dL/dw = {dw:.3f}, dL/db = {db:.3f}")
formula: dL/dw = -51.250, dL/db = -5.000
numerical: dL/dw = -51.250, dL/db = -5.000
They agree. The formulas also say something useful in words. ∂L/∂b is driven by the average error: if the line is too low on average (positive errors), the partial is negative, so raising b helps. ∂L/∂w weights each error by the hours, so students far from zero hours count more.
At the bottom, every partial is zero
In maxima and minima, the lowest point of a curve was where the slope is zero. With several inputs, the bottom of the valley is where every partial derivative is zero: no direction along any input goes downhill. Check at the best-fitting line, which NumPy's polyfit finds directly:
best_w, best_b = np.polyfit(hours, marks, deg=1) # the least-squares straight line
print(f"best line: w = {best_w:.3f}, b = {best_b:.3f}")
print("partials there:", [f"{p:.4f}" for p in loss_partials(best_w, best_b)])
best line: w = 3.707, b = 37.554
partials there: ['-0.0000', '-0.0000']
Both partials are zero. (The minus signs are rounding: a tiny negative number such as −0.00000000001 shows as −0.0000.) In fact, setting the two formulas to zero and solving the pair of equations is exactly how polyfit and linear regression's exact solution (the "normal equations") find the best line.
Experiment: compute loss_partials(5, 30). Predict the signs first: is the line at w = 5, b = 30 too steep or too shallow? Then explain each sign from the formula.
print([round(float(p), 2) for p in loss_partials(5, 30)])
[50.75, 3.0]
Both partials are now positive, so the loss would fall if w and b were decreased. A slope of 5 is too steep: the predictions for students with many hours are far too high, so the hours-weighted errors are strongly negative, and minus that is positive.
Where machine learning uses partial derivatives
- Every weight gets one. A linear regression with 10 features has 11 partial derivatives of the loss (10 weights and the intercept); a neural network can have billions. Each one says "nudge this weight up, and the loss changes by about this much".
- Training uses them to choose directions. Gradient descent moves each weight against its own partial derivative. Collected into one vector, the partials are called the gradient.
- Backpropagation is an efficient way to compute every partial derivative of a network's loss at once, using the chain rule.
- Sensitivity: in a trained model, the partial derivative of the prediction with respect to an input shows how sensitive the prediction is to that feature.
Common mistakes
- Forgetting to hold the others fixed. When finding ∂/∂x of x²y, y is a constant multiplier. Treating it as a variable leads to wrong extra terms.
- Dropping constants that aren't constants. In ∂/∂x of x²y, y stays in the answer (2*xy*). Only terms with no x at all vanish.
- Sign errors in the loss. The inside (yᵢ − w xᵢ − b) has slope −xᵢ with respect to w, giving the minus sign in front. Check against a numerical derivative.
- Mixing up ∂ and d. Use ∂ when the function has several inputs and you're holding the others still.
Practice
- Find both partial derivatives of f(x, y) = 4*x*³ − 2*xy* + y².
Show the answer
∂f/∂x = 12*x*² − 2*y* (y is held fixed, so −2*xy* has slope −2*y*). ∂f/∂y = −2*x* + 2*y* (x is held fixed).
- The volume of a box is V = length × width × height. At 10 × 5 × 2 cm, what is ∂V/∂height, and what does it mean?
Show the answer
∂V/∂height = length × width = 50. Raising the height by 1 cm, with length and width unchanged, adds about 50 cm³: a new layer 10 cm by 5 cm by 1 cm.
- Predict, then check: at (w, b) = (4, 35), the line is a little steeper than the best one (w ≈ 3.71) and its intercept is lower (b ≈ 37.55). What signs do you expect for the two partials of the study-hours loss?
Show the answer
print([round(float(p), 2) for p in loss_partials(4, 35)])
[-0.25, -1.0]
Both are negative, which surprises most people. The best line is about w = 3.71, b = 37.55, so at (4, 35) the slope is a bit too steep and the intercept too low. You might expect ∂L/∂w to say "decrease w". But a partial derivative holds the other input fixed. With b stuck at 35, every prediction is too low on average, and a steeper line lifts the predictions, so increasing w slightly does help. A partial derivative only tells you the slope in one direction from where you are; it doesn't tell you where the minimum is. Combining both partials into one direction is the job of the gradient.
- A friend says "∂L/∂b = −2 × mean error, so if the average error is zero, the line is perfect". What's wrong?
Show the answer
An average error of zero only makes ∂L/∂b zero. The line could still have the wrong slope: too high for some students and too low for others, cancelling out on average. Both partials must be zero for the best line.
- Use the chain rule to find ∂/∂w of (y − w x)², treating x and y as fixed numbers. Then check it numerically at w = 2, x = 3, y = 10.
Show the answer
2(y − wx) × (−x) = −2*x*(y − wx). At the given values: −2 × 3 × (10 − 6) = −24.
one_term = lambda w: (10 - w * 3) ** 2
h = 1e-5
print(round((one_term(2 + h) - one_term(2 - h)) / (2 * h), 3))
-24.0
- Why does the w slice of the study-hours loss have a much steeper slope than the b slice?
Show the answer
Changing w by 1 changes each prediction by that student's hours (2 to 12 marks), while changing b by 1 changes every prediction by exactly 1 mark. Bigger changes in predictions mean bigger changes in the loss, so the loss is far more sensitive to w. The formulas show it: ∂L/∂w multiplies each error by the hours.
Interview questions
What is a partial derivative?
The derivative of a multivariable function with respect to one variable while all other variables are held constant. Geometrically, it's the slope of the function along one coordinate axis. In machine learning, the partial derivative of the loss with respect to a weight tells you how the loss responds to a small change in that weight.
Derive the partial derivatives of mean squared error for simple linear regression.
With ŷᵢ = *w*xᵢ + b and L = (1/n) Σ(yᵢ − ŷᵢ)², the chain rule gives ∂L/∂w = −(2/n) Σ xᵢ(yᵢ − ŷᵢ) and ∂L/∂b = −(2/n) Σ(yᵢ − ŷᵢ). Setting both to zero gives the normal equations, whose solution is the least-squares line.
Key takeaways
- A partial derivative is the slope in one input's direction, with every other input held fixed.
- To find one, treat the other inputs as constants and use the usual rules.
- It's the slope of a slice; it usually depends on where you are, including the other inputs' values.
- For a line's MSE: ∂L/∂w = −2 × mean(x × error) and ∂L/∂b = −2 × mean(error).
- At a minimum, every partial derivative is zero; models are trained by using the partials to move each weight downhill.
Frequently asked questions
What's the difference between a derivative and a partial derivative?
A derivative is for a function of one input. A partial derivative is for a function of several inputs, measuring the change with respect to one of them while the others stay fixed. The calculation rules are the same.
How do I pronounce ∂?
Usually "partial" or "del" ("partial f by partial x"). Some people say "dee" or "curly dee". All are understood.
Do I need to compute partial derivatives by hand for machine learning?
Not for everyday work: libraries such as PyTorch compute them automatically. But understanding them lets you read papers, debug training, and know why gradient descent works.
What to learn next
- The gradient explained visually: put all the partial derivatives into one arrow.
- Gradient descent explained: use the partials to train a model.
- Revisit maxima and minima with derivatives.