Skip to content

Classic ML from scratch: logistic regression and k-means

  • Coding
  • Specialist topic or differentiator
  • About 1 h 30 min to learn
  • 3 vetted resources
Build a plan that includes this

What this topic covers

A NumPy drill: write logistic regression and k-means without a library. Interviewers use it to check that you can turn the maths into vectorised code and spot numerical traps. For logistic regression, a strong answer writes a sigmoid that won’t overflow, derives the binary cross-entropy gradient as Xᵀ(p − y)/n, and runs the update loop. For k-means, it picks a sensible initialisation (random points or k-means++), alternates assignment and mean updates, handles an empty cluster, and stops when the centroids move less than a tolerance.

You’re ready when you can

What interviewers check, from the resources below.

  • Writes a sigmoid that doesn’t overflow for large negative inputs
  • Clips probabilities before taking the log in the loss
  • Gradient is Xᵀ(p − y)/n with no Python loop over samples
  • Can derive the gradient on a whiteboard
  • Can spot where the reference k-means would loop forever on tiny float changes
  • Rewrites the stop check with a tolerance on centroid movement
  • Explains why k-means++ initialisation usually beats picking random points
  • Computes all point-to-centroid distances with broadcasting
  • Handles a cluster that ends up with no points
  • Stops on a tolerance or the iteration cap

What to study, in order

Each link opened and checked by hand; the date says when.

  1. Start here

    Train Logistic Regression with Gradient Descent (opens in a new tab)

    Deep-ML · 40 min · Free · checked 21 Oct 2026

    Why
    “Write logistic regression in NumPy” is a common warm-up in ML coding rounds. This drill makes you train it with binary cross-entropy and checks your numbers against tests.
    What to read
    Solve it in NumPy in under 30 minutes without the solution: sigmoid, loss per iteration, gradient, update. Then solve #17 (K-Means Clustering) the same way.
    What to skip
    Don’t add regularisation or fancy optimisers; a clean, vectorised loop is what’s checked.
  2. Free alternative

    Machine Learning From Scratch (opens in a new tab)

    GitHub · Erik Linder-Norén · 30 min · Free · checked 21 Oct 2026

    Why
    Short, readable NumPy versions of both algorithms. Use it to check your own code after you’ve written it.
    What to read
    Read mlfromscratch/supervised_learning/logistic_regression.py and mlfromscratch/unsupervised_learning/k_means.py. Compare them with your versions line by line.
    What to skip
    Skip the rest of the repo. Note two gaps to fix in an interview: the k-means stop check uses exact equality with no tolerance, and it uses random initialisation rather than k-means++.
  3. Go deeper

    K-Means Clustering (opens in a new tab)

    Deep-ML · 30 min · Free · checked 21 Oct 2026

    Why
    K-means is the other classic from-scratch prompt. This drill gives you the starting centroids, so you can focus on the loop itself.
    What to read
    Solve it in NumPy without the solution: vectorised distances, assignment, mean update, stop when nothing moves. Then say out loud how you’d initialise without given centroids.
    What to skip
    Don’t worry about speed on large data; correct centroids, rounded as asked, are what’s graded.

Companies that test Coding most

Put Classic ML from scratch in your plan

Tell us your interview date and hours a day; it lands on the right day, in the right order.

Build my plan