Dataset Viewer
Auto-converted to Parquet Duplicate
slug
stringlengths
6
37
title
stringlengths
9
61
difficulty
stringclasses
3 values
signature
stringlengths
9
74
starter_python
stringlengths
83
190
tests
listlengths
4
10
url
stringlengths
37
68
ml-stable-softmax
Softmax that never overflows
Easy
softmax(x)
import numpy as np def softmax(x): # x: list of floats. Return a list (or 1-D array) of probabilities. pass
[ { "args": [ [ 1, 2, 3 ] ], "out": [ 0.0900305732, 0.2447284711, 0.6652409558 ] }, { "args": [ [ 0, 0, 0, 0 ] ], "out": [ 0.25, 0.25, 0.25, 0.25 ] }, { ...
https://geodesicpartners.co.uk/ml-stable-softmax
ml-cross-entropy-from-logits
Cross-entropy loss and its gradient from logits
Medium
cross_entropy(logits, labels)
import numpy as np def cross_entropy(logits, labels): # logits: N x C list of lists, labels: list of N ints. # Return [loss, grad] with loss a float and grad an N x C list. pass
[ { "args": [ [ [ 2, 1, 0 ] ], [ 0 ] ], "out": [ 0.4076059644, [ [ -0.3347590442, 0.2447284711, 0.0900305732 ] ] ] }, { "args": [ [ [ ...
https://geodesicpartners.co.uk/ml-cross-entropy-from-logits
ml-bce-with-logits
Binary cross-entropy from logits
Medium
bce_with_logits(z, y)
import numpy as np def bce_with_logits(z, y): # z: list of floats (logits), y: list of 0/1 labels. Return the mean loss as a float. pass
[ { "args": [ [ 0 ], [ 1 ] ], "out": 0.6931471806 }, { "args": [ [ 2, -1 ], [ 1, 0 ] ], "out": 0.2200948493 }, { "args": [ [ -800 ], [ 1 ] ], ...
https://geodesicpartners.co.uk/ml-bce-with-logits
ml-ridge-regression
Ridge regression in closed form
Easy
ridge(X, y, lam)
import numpy as np def ridge(X, y, lam): # X: n x d list of lists, y: list of n floats, lam: float >= 0. # Return the d weights as a list. pass
[ { "args": [ [ [ 1, 0 ], [ 0, 1 ], [ 1, 1 ] ], [ 1, 2, 3 ], 0 ], "out": [ 1, 2 ] }, { "args": [ [ [ ...
https://geodesicpartners.co.uk/ml-ridge-regression
ml-linear-regression-gradient-descent
Linear regression by gradient descent
Medium
fit_linear_gd(X, y, lr, steps)
import numpy as np def fit_linear_gd(X, y, lr, steps): # X: n x d, y: n targets. Return [w_1, ..., w_d, b]. pass
[ { "args": [ [ [ 1 ], [ 2 ], [ 3 ] ], [ 2, 4, 6 ], 0.1, 1 ], "out": [ 1.8666666667, 0.8 ] }, { "args": [ [ [ 1 ...
https://geodesicpartners.co.uk/ml-linear-regression-gradient-descent
ml-logistic-regression
Logistic regression with an L2 penalty
Medium
fit_logistic(X, y, lr, steps, lam)
import numpy as np def fit_logistic(X, y, lr, steps, lam): # X: n x d, y: n labels in {0, 1}. Return [w_1, ..., w_d, b]. pass
[ { "args": [ [ [ 1 ], [ -1 ] ], [ 1, 0 ], 1, 1, 0 ], "out": [ 0.5, 0 ] }, { "args": [ [ [ 0, 0 ], [ 1, ...
https://geodesicpartners.co.uk/ml-logistic-regression
ml-mlp-forward-pass
Forward pass of a two-layer network
Easy
mlp_forward(X, W1, b1, W2, b2)
import numpy as np def mlp_forward(X, W1, b1, W2, b2): # Return the N x C matrix of probabilities. pass
[ { "args": [ [ [ 1, 2 ] ], [ [ 1, 0 ], [ 0, 1 ] ], [ 0, 0 ], [ [ 1, 0 ], [ 0, 1 ...
https://geodesicpartners.co.uk/ml-mlp-forward-pass
ml-mlp-backpropagation
Backpropagation through a two-layer network
Hard
mlp_grads(X, y, W1, b1, W2, b2)
import numpy as np def mlp_grads(X, y, W1, b1, W2, b2): # Return [dW1, db1, dW2, db2], each with its parameter's shape. pass
[ { "args": [ [ [ 1, 2 ] ], [ 0 ], [ [ 1, 0 ], [ 0, 1 ] ], [ 0, 0 ], [ [ 1, 0 ], [ ...
https://geodesicpartners.co.uk/ml-mlp-backpropagation
ml-adam-optimizer
The Adam optimiser, with bias correction
Medium
adam(theta, grads, lr, beta1, beta2, eps)
import numpy as np def adam(theta, grads, lr, beta1, beta2, eps): # theta: list of floats; grads: list of gradient vectors, applied in order. pass
[ { "args": [ [ 1 ], [ [ 0.5 ] ], 0.1, 0.9, 0.999, 1e-8 ], "out": [ 0.9000000020000001 ] }, { "args": [ [ 1, -2 ], [ [ 100, -0.001 ] ...
https://geodesicpartners.co.uk/ml-adam-optimizer
ml-inverted-dropout
Inverted dropout, forward and backward
Easy
dropout(X, mask, p, dout)
import numpy as np def dropout(X, mask, p, dout): # Return [out, dX]. pass
[ { "args": [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 1, 0 ], [ 1, 1 ] ], 0.5, [ [ 1, 1 ], [ ...
https://geodesicpartners.co.uk/ml-inverted-dropout
ml-gradient-clipping
Gradient clipping by global norm
Easy
clip_grad_norm(grads, max_norm)
import numpy as np def clip_grad_norm(grads, max_norm): # grads: list of arrays of any shape. Return [clipped_grads, total_norm]. pass
[ { "args": [ [ [ 3 ], [ 4 ] ], 1 ], "out": [ [ [ 0.59999988 ], [ 0.79999984 ] ], 5 ] }, { "args": [ [ [ 0.1, 0.2 ...
https://geodesicpartners.co.uk/ml-gradient-clipping
ml-learning-rate-schedule
Linear warm-up with cosine decay
Easy
lr_schedule(steps, base_lr, min_lr, warmup, total)
import math def lr_schedule(steps, base_lr, min_lr, warmup, total): # Return the learning rate for each step, as a list. pass
[ { "args": [ [ 0, 1, 2, 3 ], 1, 0, 4, 10 ], "out": [ 0.25, 0.5, 0.75, 1 ] }, { "args": [ [ 4, 7, 10 ], 1, 0, 4, 10 ], "out": [ ...
https://geodesicpartners.co.uk/ml-learning-rate-schedule
ml-layer-normalization
Layer normalisation
Easy
layer_norm(X, gamma, beta, eps)
import numpy as np def layer_norm(X, gamma, beta, eps): # X: N x D. Normalise each row over its D features. pass
[ { "args": [ [ [ 1, 2, 3 ] ], [ 1, 1, 1 ], [ 0, 0, 0 ], 0 ], "out": [ [ -1.2247448714, 0, 1.2247448714 ] ] }, { "args": [...
https://geodesicpartners.co.uk/ml-layer-normalization
ml-batch-normalization
Batch normalisation in training mode
Medium
batch_norm_train(X, gamma, beta, running_mean, running_var, momentum, eps)
import numpy as np def batch_norm_train(X, gamma, beta, running_mean, running_var, momentum, eps): # Return [out, new_running_mean, new_running_var]. pass
[ { "args": [ [ [ 1, 10 ], [ 3, 30 ] ], [ 1, 1 ], [ 0, 0 ], [ 0, 0 ], [ 1, 1 ], 0.1, 0 ], "out": [...
https://geodesicpartners.co.uk/ml-batch-normalization
ml-scaled-dot-product-attention
Scaled dot-product attention with a causal mask
Medium
attention(Q, K, V, causal)
import numpy as np def attention(Q, K, V, causal): # Q: n x d, K: m x d, V: m x dv, causal: bool. Return n x dv. pass
[ { "args": [ [ [ 1, 0 ] ], [ [ 1, 0 ], [ 0, 1 ] ], [ [ 10, 0 ], [ 0, 10 ] ], false ], ...
https://geodesicpartners.co.uk/ml-scaled-dot-product-attention
ml-conv2d
2-D convolution with stride and padding
Medium
conv2d(image, kernel, stride, padding)
import numpy as np def conv2d(image, kernel, stride, padding): # Cross-correlation with zero padding and stride. Return a 2-D list or array. pass
[ { "args": [ [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ], [ [ 1, 0 ], [ 0, -1 ] ]...
https://geodesicpartners.co.uk/ml-conv2d
ml-lstm-cell
One step of an LSTM cell
Medium
lstm_cell(x, h, c, W, b)
import numpy as np def lstm_cell(x, h, c, W, b): # Return [h_new, c_new] as lists. pass
[ { "args": [ [ 1 ], [ 0 ], [ 0 ], [ [ 0.5, 0 ], [ 0.5, 0 ], [ 1, 0 ], [ 1, 0 ] ], [ ...
https://geodesicpartners.co.uk/ml-lstm-cell
ml-gru-cell
One step of a GRU cell, as PyTorch computes it
Medium
gru_cell(x, h, W_ih, W_hh, b_ih, b_hh)
import numpy as np def gru_cell(x, h, W_ih, W_hh, b_ih, b_hh): # Return h_new as a list. pass
[ { "args": [ [ 1 ], [ 0 ], [ [ 1 ], [ 1 ], [ 1 ] ], [ [ 1 ], [ 1 ], [ 1 ] ], [ ...
https://geodesicpartners.co.uk/ml-gru-cell
ml-beam-search
Beam search decoding
Hard
beam_search(start, trans, T, B)
import numpy as np def beam_search(start, trans, T, B): # Return [best_sequence, best_score]. pass
[ { "args": [ [ -0.1, -2.5 ], [ [ -2.5, -0.1 ], [ -0.1, -2.5 ] ], 3, 1 ], "out": [ [ 0, 1, 0 ], -0.30000000000000004 ] }, { "arg...
https://geodesicpartners.co.uk/ml-beam-search
ml-cosine-top-k
Top-k search by cosine similarity
Easy
cosine_top_k(q, M, k)
import numpy as np def cosine_top_k(q, M, k): # Return a list of k row indices, most similar first. pass
[ { "args": [ [ 1, 0 ], [ [ 1, 0 ], [ 0, 1 ], [ 1, 1 ], [ -1, 0 ] ], 2 ], "out": [ 0, 2 ] }, { ...
https://geodesicpartners.co.uk/ml-cosine-top-k
ml-info-nce-loss
The InfoNCE contrastive loss
Medium
info_nce(Q, K, tau)
import numpy as np def info_nce(Q, K, tau): # Return the loss as a float. pass
[ { "args": [ [ [ 1, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 0, 1 ] ], 1 ], "out": 0.3132616875 }, { "args": [ [ [ ...
https://geodesicpartners.co.uk/ml-info-nce-loss
ml-negative-sampling
Skip-gram with negative sampling: loss and gradients
Hard
sgns_loss_grad(v, u, U_neg)
import numpy as np def sgns_loss_grad(v, u, U_neg): # Return [loss, grad_v, grad_u, grad_U_neg]. pass
[ { "args": [ [ 1, 0 ], [ 1, 0 ], [ [ 0, 1 ] ] ], "out": [ 1.0064088681, [ -0.2689414214, 0.5 ], [ -0.2689414214, 0 ], [ [ ...
https://geodesicpartners.co.uk/ml-negative-sampling
ml-k-nearest-neighbors
k-nearest neighbours classifier
Easy
knn_predict(X_train, y_train, X_test, k)
import numpy as np def knn_predict(X_train, y_train, X_test, k): # Return a list of predicted labels, one per row of X_test. pass
[ { "args": [ [ [ 0, 0 ], [ 1, 0 ], [ 5, 5 ], [ 6, 5 ] ], [ 0, 0, 1, 1 ], [ [ 0.5, ...
https://geodesicpartners.co.uk/ml-k-nearest-neighbors
ml-k-means-clustering
k-means clustering (Lloyd's algorithm)
Medium
kmeans(X, init, max_iter)
import numpy as np def kmeans(X, init, max_iter): # Return [centroids (k x d), labels (n)]. pass
[ { "args": [ [ [ 0, 0 ], [ 0, 1 ], [ 10, 10 ], [ 10, 11 ] ], [ [ 0, 0 ], [ 10, 10 ...
https://geodesicpartners.co.uk/ml-k-means-clustering
ml-pca
Principal component analysis
Medium
pca(X, k)
import numpy as np def pca(X, k): # Return [components (k x d), projected (n x k)]. pass
[ { "args": [ [ [ 1, 1 ], [ 2, 2 ], [ 3, 3 ] ], 1 ], "out": [ [ [ 0.7071067812, 0.7071067812 ] ], [ [ -1.414...
https://geodesicpartners.co.uk/ml-pca
ml-decision-tree-split
Best decision-tree split by Gini impurity
Medium
best_split(X, y)
import numpy as np def best_split(X, y): # Return [feature, threshold, weighted_gini]. pass
[ { "args": [ [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ], [ 0, 0, 1, 1 ] ], "out": [ 0, 2.5, 0 ] }, { "args": [ [ ...
https://geodesicpartners.co.uk/ml-decision-tree-split
ml-gaussian-naive-bayes
Gaussian naive Bayes classifier
Medium
gaussian_nb(X_train, y_train, X_test)
import numpy as np def gaussian_nb(X_train, y_train, X_test): # Return the predicted label for each row of X_test, as a list. pass
[ { "args": [ [ [ 0, 0 ], [ 1, 1 ], [ 0, 1 ], [ 5, 5 ], [ 6, 6 ], [ 5, 6 ] ], [ ...
https://geodesicpartners.co.uk/ml-gaussian-naive-bayes
ml-gmm-em-step
One EM step for a Gaussian mixture
Medium
em_step(x, pi, mu, var)
import numpy as np def em_step(x, pi, mu, var): # Return [new_pi, new_mu, new_var, loglik]. pass
[ { "args": [ [ -1, 1 ], [ 0.5, 0.5 ], [ 0, 0 ], [ 1, 1 ] ], "out": [ [ 0.5, 0.5 ], [ 0, 0 ], [ 1, 1 ], ...
https://geodesicpartners.co.uk/ml-gmm-em-step
ml-viterbi-algorithm
The Viterbi algorithm for a hidden Markov model
Medium
viterbi(start, trans, emit, obs)
import numpy as np def viterbi(start, trans, emit, obs): # Return [path (list of states), logp (float)]. pass
[ { "args": [ [ 0.6000000000000001, 0.4 ], [ [ 0.7000000000000001, 0.30000000000000004 ], [ 0.4, 0.6000000000000001 ] ], [ [ 0.5, 0.4, 0.1 ], ...
https://geodesicpartners.co.uk/ml-viterbi-algorithm
ml-markov-stationary-distribution
Stationary distribution of a Markov chain
Easy
stationary(P)
import numpy as np def stationary(P): # Return the stationary distribution as a list. pass
[ { "args": [ [ [ 0.9, 0.1 ], [ 0.5, 0.5 ] ] ], "out": [ 0.8333333333, 0.1666666667 ] }, { "args": [ [ [ 0, 1 ], [ 0.5, 0.5 ...
https://geodesicpartners.co.uk/ml-markov-stationary-distribution
ml-running-variance
Running mean and variance in one pass
Easy
running_stats(xs)
def running_stats(xs): # Return [[mean, variance], ...], one record after each value. pass
[ { "args": [ [ 2, 4, 6 ] ], "out": [ [ 2, 0 ], [ 3, 2 ], [ 4, 4 ] ] }, { "args": [ [ 1000000004, 1000000007, 1000000013, 1000000016 ...
https://geodesicpartners.co.uk/ml-running-variance
ml-roc-auc
ROC AUC from scores, with ties
Medium
roc_auc(scores, labels)
import numpy as np def roc_auc(scores, labels): # Return the AUC as a float. pass
[ { "args": [ [ 0.9, 0.8, 0.30000000000000004, 0.1 ], [ 1, 1, 0, 0 ] ], "out": 1 }, { "args": [ [ 0.5, 0.5, 0.5, 0.5 ], [ 1, 0, 1, ...
https://geodesicpartners.co.uk/ml-roc-auc
ml-spearman-rank-correlation
Spearman rank correlation (the information coefficient)
Medium
spearman(x, y)
import numpy as np def spearman(x, y): # Return the Spearman rank correlation as a float. pass
[ { "args": [ [ 1, 2, 3, 4 ], [ 10, 20, 30, 40 ] ], "out": 1 }, { "args": [ [ 1, 2, 3, 4 ], [ 1, 8, 27, 1000 ] ], ...
https://geodesicpartners.co.uk/ml-spearman-rank-correlation
ml-autocorrelation-function
The autocorrelation function of a series
Easy
autocorrelation(x, max_lag)
import numpy as np def autocorrelation(x, max_lag): # Return [r_0, r_1, ..., r_max_lag]. pass
[ { "args": [ [ 1, 2, 3, 4, 5 ], 2 ], "out": [ 1, 0.4, -0.1 ] }, { "args": [ [ 1, -1, 1, -1, 1, -1 ], 1 ], "out": [ 1, -0.8333333333...
https://geodesicpartners.co.uk/ml-autocorrelation-function
ml-ols-standard-errors
Regression coefficients with standard errors and t-statistics
Medium
ols_inference(X, y)
import numpy as np def ols_inference(X, y): # Return [beta, se, t], each a list of p floats. pass
[ { "args": [ [ [ 1, 0 ], [ 1, 1 ], [ 1, 2 ], [ 1, 3 ] ], [ 1, 3, 2, 5 ] ], "out": [ [ 1.1, ...
https://geodesicpartners.co.uk/ml-ols-standard-errors
ml-permutation-test
An exact permutation test for a difference in means
Medium
permutation_p_value(a, b)
from itertools import combinations def permutation_p_value(a, b): # Return the exact two-sided p-value as a float. pass
[ { "args": [ [ 7, 9, 12 ], [ 3, 4, 6 ] ], "out": 0.1 }, { "args": [ [ 1, 2 ], [ 1, 2 ] ], "out": 1 }, { "args": [ [ 5 ], [ ...
https://geodesicpartners.co.uk/ml-permutation-test
ml-kalman-filter-1d
A one-dimensional Kalman filter
Medium
kalman_filter(ys, q, r, m0, p0)
def kalman_filter(ys, q, r, m0, p0): # Return [[mean, variance], ...], one record after each observation. pass
[ { "args": [ [ 10, 12, 11 ], 1, 4, 8, 9 ], "out": [ [ 9.3846153846, 2.7692307692 ], [ 10.653465346499999, 1.9405940594 ], [ 10.8002853067, 1.694721826 ] ] ...
https://geodesicpartners.co.uk/ml-kalman-filter-1d
ml-ndcg-at-k
NDCG at k
Easy
ndcg_at_k(rels, k)
import numpy as np def ndcg_at_k(rels, k): # Return NDCG@k as a float. pass
[ { "args": [ [ 3, 2, 3, 0, 1, 2 ], 6 ], "out": 0.9488107486 }, { "args": [ [ 0, 0, 0 ], 3 ], "out": 0 }, { "args": [ [ 1, 0, 0 ], 1...
https://geodesicpartners.co.uk/ml-ndcg-at-k
ml-expected-calibration-error
Expected calibration error
Medium
expected_calibration_error(p, y, m)
import numpy as np def expected_calibration_error(p, y, m): # Return ECE as a float. pass
[ { "args": [ [ 0.1, 0.2, 0.8, 0.9 ], [ 0, 0, 1, 1 ], 2 ], "out": 0.15 }, { "args": [ [ 1, 1, 0 ], [ 1, 1, 0 ], 10 ], ...
https://geodesicpartners.co.uk/ml-expected-calibration-error
ml-macro-f1
Macro-averaged F1 across classes
Easy
macro_f1(y_true, y_pred)
import numpy as np def macro_f1(y_true, y_pred): # Return macro-F1 as a float. pass
[ { "args": [ [ 0, 0, 1, 1, 2 ], [ 0, 1, 1, 1, 0 ] ], "out": 0.4333333333 }, { "args": [ [ 5, 5, 5 ], [ 5, 5, 5 ] ], ...
https://geodesicpartners.co.uk/ml-macro-f1

ML coding interview problems

40 problems of the kind set in the implementation round of ML and quant interviews: softmax that doesn't overflow, a backward pass, attention, AUC with ties. Each row has a Python starter and a test table, so it also works as a small code-generation benchmark. Every problem's page: https://geodesicpartners.co.uk/machine-learning-coding

Downloads last month
43