DSLR
A Sorting Hat, in gradient descent
Sixteen hundred students, thirteen courses, one house to find. The project starts with a describe tool written by hand, then with the plots that show which courses separate the houses and which ones say the same thing twice. The logistic regression only comes after, on the columns that were kept.
- Area
- 42
- data
- machine learning
- Stack
- Python, Machine learning
- Grade
- 125%
- Date
- 2025
Look before you learn
Sixteen hundred students, thirteen courses, one house to recover. The instinct would be to throw all thirteen columns at a model and let it sort things out. The project asks for the opposite: start with a describe tool, written by hand, giving for each course the count, the mean, the standard deviation, the minimum, the three quartiles and the maximum.
Written by hand means written by hand: no pandas describe, no numpy mean. A mean, a standard deviation, a quantile interpolated between its two neighbours. Three more measures join them, none of them required but all useful later: the coefficient of variation, which compares spreads expressed in different units, the interquartile range, which ignores the extremes, and the skewness, which says which way a distribution leans.
Two columns for one
Pairing the courses up brings out an anomaly. Astronomy and Defence Against the Dark Arts do not form a cloud: they form a line. A student strong in one is weak in the other, exactly, up to a constant factor.
The two columns carry the same information. Keeping one is enough, and keeping both would hand the model the same variable twice, with the numerical trouble that comes with it.

A course that separates nothing
The other question is the mirror image: which courses are useless. One histogram per house, overlaid on the same course, answers at a glance. For Care of Magical Creatures the four distributions almost perfectly cover each other. All four houses score the same, so the score says nothing about the house.
What we want is the opposite: a course where the four curves pull apart, and a pair of courses where the houses gather into groups instead of lining up.

The next two figures are those same two tools, pointed this time at what separates best. The ranking is not a matter of impression: measuring the spread of the four house means against the overall spread of the course, Divination comes first at 1.18, while Care of Magical Creatures falls to 0.065 and Arithmancy to 0.029. The previous two figures showed the bottom of that ranking; the next two show the top.
On the left, Herbology paired with Divination: three clearly separated clusters, where Astronomy and Defence gave nothing but a line. On the right, Divination alone is enough to set Slytherin entirely apart from the other three houses. Four courses are kept in the end: Charms, Herbology, Divination and History of Magic.


Normalise, and remember the scale
The scores do not live on the same scale: some sit between minus three and three, Astronomy runs into the hundreds. Gradient descent over columns of such different magnitudes zigzags, far too fast along one and far too slow along the other. Every column is therefore centred on its mean and divided by its standard deviation.
The detail that matters comes next: those means and standard deviations are saved alongside the weights. Prediction has to normalise the test set with the training statistics, and on no account with its own, or the weights would apply to numbers that no longer mean the same thing.
One binary regression, four times
A logistic regression answers yes or no, and there are four houses. The answer is to pose four binary problems: Ravenclaw against the rest, Slytherin against the rest, and so on. Four independent models, each trained on the same students but with different labels.
Each one computes a linear combination of the four scores, plus a bias carried by a column of ones appended to the data, then pushes the result through a sigmoid that brings it back between zero and one. The distance from the truth is measured by cross-entropy, and that is what gradient descent drives down.
The descent
The gradient of a logistic regression has a remarkably simple shape: the transposed data matrix, multiplied by the gap between predictions and truth. One line of arithmetic, repeated at most ten thousand times.
Two settings make the descent less brutal. The step shrinks as the iterations go by, fast enough for the end to be precise, slowly enough for the start to make progress. And training stops by itself when the cost has not measurably dropped for a hundred iterations in a row: carrying on would only burn time.
The cost is recorded at every iteration, which makes it possible to draw one curve per house. That is the only way to see whether the model is learning, whether it has converged, or whether it is oscillating because the step is too large.

Predicting
Prediction comes last. The four models each return a probability, and the house kept is the one claiming the highest. Missing scores are replaced by the mean of the course, the training mean, so that an incomplete student can still be sorted.
On the thirty percent of students held out before training, each binary model is right more than ninety-nine percent of the time. On the full test set, the final prediction recovers the right house for ninety-nine percent of the students.
The number is not the interesting part. The interesting part is that it holds with four courses out of thirteen, picked by eye off a few plots, and a hundred lines of arithmetic with no machine learning library anywhere.
