File size: 40,826 Bytes
b9fe608 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | {"slug": "ml-stable-softmax", "title": "Softmax that never overflows", "difficulty": "Easy", "signature": "softmax(x)", "starter_python": "import numpy as np\n\ndef softmax(x):\n # x: list of floats. Return a list (or 1-D array) of probabilities.\n pass", "tests": [{"args": [[1, 2, 3]], "out": [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]}, {"args": [[0, 0, 0, 0]], "out": [0.25, 0.25, 0.25, 0.25]}, {"args": [[1000, 1001, 1002]], "out": [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]}, {"args": [[-1000, -1000.5]], "out": [0.6224593312018546, 0.37754066879814546]}, {"args": [[5]], "out": [1]}, {"args": [[3.2, -1.1, 0, 7.5, 2.2]], "out": [0.013311678975991446, 0.00018062030173721444, 0.0005426133737138924, 0.981067994325816, 0.00489709302274137]}, {"args": [[-10000, 0, 10000]], "out": [0, 0, 1]}, {"args": [[709, 710, 711]], "out": [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]}, {"args": [[-745, -746, -800]], "out": [0.7310585786300049, 0.2689414213699951, 9.500701493799415e-25]}, {"args": [[0.1, 0.2, 0.30000000000000004]], "out": [0.3006096053557273, 0.3322249935333472, 0.3671654011109255]}], "url": "https://geodesicpartners.co.uk/ml-stable-softmax"}
{"slug": "ml-cross-entropy-from-logits", "title": "Cross-entropy loss and its gradient from logits", "difficulty": "Medium", "signature": "cross_entropy(logits, labels)", "starter_python": "import numpy as np\n\ndef cross_entropy(logits, labels):\n # logits: N x C list of lists, labels: list of N ints.\n # Return [loss, grad] with loss a float and grad an N x C list.\n pass", "tests": [{"args": [[[2, 1, 0]], [0]], "out": [0.4076059644443804, [[-0.3347590442251782, 0.24472847105479764, 0.09003057317038043]]]}, {"args": [[[0, 0], [0, 0]], [0, 1]], "out": [0.6931471805599453, [[-0.25, 0.25], [0.25, -0.25]]]}, {"args": [[[1000, 0]], [1]], "out": [1000, [[1, -1]]]}, {"args": [[[1, 2, 3], [3, 2, 1], [0, 0, 5]], [2, 2, 2]], "out": [0.9428659435367367, [[0.030010191056793478, 0.08157615701826589, -0.1115863480750594], [0.22174698525827394, 0.08157615701826589, -0.30332314227653984], [0.002216118159622001, 0.002216118159622001, -0.004432236319244017]]]}, {"args": [[[-500, 500, 0, 1], [10, 10, 10, 10]], [0, 3]], "out": [500.69314718055995, [[-0.5, 0.5, 3.562288203370643e-218, 9.683303290956438e-218], [0.125, 0.125, 0.125, -0.375]]]}, {"args": [[[0.5, -1.5, 2.25, 0, -0.75]], [3]], "out": [2.5519384522981765, [[0.12848559882884594, 0.01738863490932766, 0.7393835708486755, -0.9220695449787673, 0.03681174039191839]]]}], "url": "https://geodesicpartners.co.uk/ml-cross-entropy-from-logits"}
{"slug": "ml-bce-with-logits", "title": "Binary cross-entropy from logits", "difficulty": "Medium", "signature": "bce_with_logits(z, y)", "starter_python": "import numpy as np\n\ndef bce_with_logits(z, y):\n # z: list of floats (logits), y: list of 0/1 labels. Return the mean loss as a float.\n pass", "tests": [{"args": [[0], [1]], "out": 0.6931471805599453}, {"args": [[2, -1], [1, 0]], "out": 0.2200948492805977}, {"args": [[-800], [1]], "out": 800}, {"args": [[800], [0]], "out": 800}, {"args": [[800], [1]], "out": 0}, {"args": [[-10000, 10000, 0.5], [0, 1, 0]], "out": 0.32469232806003556}, {"args": [[0.1, -0.2, 3.5, -4.25, 37.5, -37.5], [1, 0, 1, 1, 0, 1]], "out": 13.422741567443216}, {"args": [[1e-09, -1e-09], [1, 1]], "out": 0.6931471805599454}], "url": "https://geodesicpartners.co.uk/ml-bce-with-logits"}
{"slug": "ml-ridge-regression", "title": "Ridge regression in closed form", "difficulty": "Easy", "signature": "ridge(X, y, lam)", "starter_python": "import numpy as np\n\ndef ridge(X, y, lam):\n # X: n x d list of lists, y: list of n floats, lam: float >= 0.\n # Return the d weights as a list.\n pass", "tests": [{"args": [[[1, 0], [0, 1], [1, 1]], [1, 2, 3], 0], "out": [1, 2]}, {"args": [[[1, 0], [0, 1], [1, 1]], [1, 2, 3], 1], "out": [0.875, 1.375]}, {"args": [[[2], [4]], [2, 4], 100], "out": [0.16666666666666666]}, {"args": [[[1, 2, 3], [4, 5, 6]], [1, 1], 0.5], "out": [-0.24561403508771865, 0.03508771929824439, 0.3157894736842111]}, {"args": [[[0.5, -1.2, 3.3], [2.1, 0, -0.7], [1, 1, 1], [-2.2, 4.1, 0.3], [0.9, -0.4, 2.2]], [1.5, -0.3, 2, 0.7, 1.1], 0.01], "out": [0.2847881021380972, 0.3445365781456732, 0.5760548622179539]}, {"args": [[[1, 1], [1, 1.0001], [1, 0.9999]], [2, 2.0001, 1.9999], 0.001], "out": [0.999833359440546, 0.999833362772974]}], "url": "https://geodesicpartners.co.uk/ml-ridge-regression"}
{"slug": "ml-linear-regression-gradient-descent", "title": "Linear regression by gradient descent", "difficulty": "Medium", "signature": "fit_linear_gd(X, y, lr, steps)", "starter_python": "import numpy as np\n\ndef fit_linear_gd(X, y, lr, steps):\n # X: n x d, y: n targets. Return [w_1, ..., w_d, b].\n pass", "tests": [{"args": [[[1], [2], [3]], [2, 4, 6], 0.1, 1], "out": [1.8666666666666665, 0.8]}, {"args": [[[1], [2], [3]], [3, 5, 7], 0.05, 2000], "out": [2.00000000000139, 0.9999999999968399]}, {"args": [[[1, 2], [3, 4]], [1, 2], 0.01, 0], "out": [0, 0, 0]}, {"args": [[[1, 2], [3, 4], [5, 7]], [1, 2, 4], 0.01, 50], "out": [0.2518683508893995, 0.36642500236775866, 0.02669013589260803]}, {"args": [[[0.5, -1, 2], [1.5, 0.3, -0.2], [-0.7, 0.8, 0.1], [0.2, -0.4, 1]], [1, 0.5, -0.2, 0.9], 0.05, 300], "out": [0.2265648057924438, -0.4064040875340351, 0.1343023686923952, 0.3332602583776953]}], "url": "https://geodesicpartners.co.uk/ml-linear-regression-gradient-descent"}
{"slug": "ml-logistic-regression", "title": "Logistic regression with an L2 penalty", "difficulty": "Medium", "signature": "fit_logistic(X, y, lr, steps, lam)", "starter_python": "import numpy as np\n\ndef fit_logistic(X, y, lr, steps, lam):\n # X: n x d, y: n labels in {0, 1}. Return [w_1, ..., w_d, b].\n pass", "tests": [{"args": [[[1], [-1]], [1, 0], 1, 1, 0], "out": [0.5, 0]}, {"args": [[[0, 0], [1, 1], [2, 2], [3, 3]], [0, 0, 1, 1], 0.5, 500, 0.1], "out": [1.0111758567463955, 1.0111758567463955, -3.0334297813217925]}, {"args": [[[1000], [-1000]], [1, 0], 0.002, 3, 0], "out": [1, 0]}, {"args": [[[0.5, 1.2], [1.5, -0.3], [-1, 0.4], [2, 2], [-0.5, -1.5], [0.1, 0.9]], [1, 1, 0, 1, 0, 0], 0.2, 200, 0.01], "out": [2.6967502647885864, 0.5402731237957973, -1.200155278413129]}, {"args": [[[1, 0], [0, 1]], [1, 1], 0.1, 0, 0.5], "out": [0, 0, 0]}], "url": "https://geodesicpartners.co.uk/ml-logistic-regression"}
{"slug": "ml-mlp-forward-pass", "title": "Forward pass of a two-layer network", "difficulty": "Easy", "signature": "mlp_forward(X, W1, b1, W2, b2)", "starter_python": "import numpy as np\n\ndef mlp_forward(X, W1, b1, W2, b2):\n # Return the N x C matrix of probabilities.\n pass", "tests": [{"args": [[[1, 2]], [[1, 0], [0, 1]], [0, 0], [[1, 0], [0, 1]], [0, 0]], "out": [[0.2689414213699951, 0.7310585786300049]]}, {"args": [[[-1, -1]], [[1, 0], [0, 1]], [0, 0], [[1, 0], [0, 1]], [0, 0]], "out": [[0.5, 0.5]]}, {"args": [[[1, 0], [0, 1]], [[2, -1], [-1, 2]], [0.5, 0.5], [[1, -1], [-1, 1]], [0, 0.1]], "out": [[0.9926084586557181, 0.007391541344281971], [0.0060598014915841155, 0.9939401985084158]]}, {"args": [[[0.5, -0.2, 1], [2, 0.3, -1.1], [-0.4, 0.9, 0]], [[0.1, -0.3, 0.8, 0.2], [0.7, 0.5, -0.6, 0], [-0.2, 0.4, 0.3, 1.1]], [0, 0.1, -0.1, 0.2], [[1, -0.5, 0.2], [0.3, 0.8, -1], [-0.7, 0.1, 0.5], [0.4, 0.4, 0.4]], [0, 0, 0.3]], "out": [[0.1876236239748439, 0.3782055973223067, 0.4341707787028495], [0.2205915539249805, 0.18929616880358657, 0.5901122772714329], [0.5183104256842569, 0.29903910181676135, 0.18265047249898184]]}, {"args": [[[300, 200]], [[1, 0], [0, 1]], [0, 0], [[1, 0], [0, 1]], [0, 0]], "out": [[1, 3.720075976020836e-44]]}], "url": "https://geodesicpartners.co.uk/ml-mlp-forward-pass"}
{"slug": "ml-mlp-backpropagation", "title": "Backpropagation through a two-layer network", "difficulty": "Hard", "signature": "mlp_grads(X, y, W1, b1, W2, b2)", "starter_python": "import numpy as np\n\ndef mlp_grads(X, y, W1, b1, W2, b2):\n # Return [dW1, db1, dW2, db2], each with its parameter's shape.\n pass", "tests": [{"args": [[[1, 2]], [0], [[1, 0], [0, 1]], [0, 0], [[1, 0], [0, 1]], [0, 0]], "out": [[[-0.7310585786300049, 0.7310585786300049], [-1.4621171572600098, 1.4621171572600098]], [-0.7310585786300049, 0.7310585786300049], [[-0.7310585786300049, 0.7310585786300049], [-1.4621171572600098, 1.4621171572600098]], [-0.7310585786300049, 0.7310585786300049]]}, {"args": [[[-1, -1]], [1], [[1, 0], [0, 1]], [0, 0], [[1, 0], [0, 1]], [0, 0]], "out": [[[0, 0], [0, 0]], [0, 0], [[0, 0], [0, 0]], [0.5, -0.5]]}, {"args": [[[1, 0], [0, 1]], [0, 1], [[2, -1], [-1, 2]], [0.5, 0.5], [[1, -1], [-1, 1]], [0, 0.1]], "out": [[[-0.0073915413442819265, 0], [0, -0.006059801491584182]], [-0.0073915413442819265, -0.006059801491584182], [[-0.009239426680352353, 0.009239426680352464], [0.007574751864480145, -0.007574751864480311]], [-0.0006658699263488834, 0.0006658699263488608]]}, {"args": [[[0.5, -0.2, 1], [2, 0.3, -1.1], [-0.4, 0.9, 0]], [2, 0, 1], [[0.1, -0.3, 0.8, 0.2], [0.7, 0.5, -0.6, 0], [-0.2, 0.4, 0.3, 1.1]], [0, 0.1, -0.1, 0.2], [[1, -0.5, 0.2], [0.3, 0.8, -1], [-0.7, 0.1, 0.5], [0.4, 0.4, 0.4]], [0, 0, 0.3]], "out": [[[-0.6247321792517738, 0.2325036061699476, 0.51030934722927, -4.869546326528957e-18], [0.19599288328044914, -0.23802320475858532, 0.11105253929010789, 1.2217381508394281e-17], [0.2772124941749263, 0.30822692878248303, -0.44065314074806106, 1.3631375931936526e-18]], [0.049762298084382095, 0.11225157433571781, 0.16104702668995116, 1.524092540100811e-17], [[-0.06174138995785027, -0.09810344786061707, 0.15984483781846734], [0.1313912970673877, -0.12503080081739776, -0.006360496249989934], [-0.21217511745079393, 0.1532370790625372, 0.05893803838825672], [0.10829010821563076, 0.14845750948974692, -0.25674761770537763]], [-0.024491465471972923, -0.04448637735244845, 0.06897784282442138]]}], "url": "https://geodesicpartners.co.uk/ml-mlp-backpropagation"}
{"slug": "ml-adam-optimizer", "title": "The Adam optimiser, with bias correction", "difficulty": "Medium", "signature": "adam(theta, grads, lr, beta1, beta2, eps)", "starter_python": "import numpy as np\n\ndef adam(theta, grads, lr, beta1, beta2, eps):\n # theta: list of floats; grads: list of gradient vectors, applied in order.\n pass", "tests": [{"args": [[1], [[0.5]], 0.1, 0.9, 0.999, 1e-08], "out": [0.900000002]}, {"args": [[1, -2], [[100, -0.001]], 0.01, 0.9, 0.999, 1e-08], "out": [0.990000000001, -1.990000099999]}, {"args": [[0], [[1], [-1], [1], [-1]], 0.1, 0.9, 0.999, 1e-08], "out": [-0.12305301877335414]}, {"args": [[0.5, 0.5, 0.5], [[0.1, 0, -0.2], [0.2, 0, -0.1], [0.3, 0, 0]], 0.001, 0.9, 0.999, 1e-08], "out": [0.4970768169554485, 0.5, 0.5026527570365568]}, {"args": [[2], [[0]], 0.1, 0.9, 0.999, 1e-08], "out": [2]}, {"args": [[1, 1], [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], 0.05, 0.5, 0.9, 1e-06], "out": [0.72685023130129, 0.7269604193007826]}], "url": "https://geodesicpartners.co.uk/ml-adam-optimizer"}
{"slug": "ml-inverted-dropout", "title": "Inverted dropout, forward and backward", "difficulty": "Easy", "signature": "dropout(X, mask, p, dout)", "starter_python": "import numpy as np\n\ndef dropout(X, mask, p, dout):\n # Return [out, dX].\n pass", "tests": [{"args": [[[1, 2], [3, 4]], [[1, 0], [1, 1]], 0.5, [[1, 1], [1, 1]]], "out": [[[2, 0], [6, 8]], [[2, 0], [2, 2]]]}, {"args": [[[5, -5, 5]], [[1, 1, 1]], 0.2, [[0.1, 0.2, 0.3]]], "out": [[[6.25, -6.25, 6.25]], [[0.125, 0.25, 0.375]]]}, {"args": [[[2]], [[0]], 0.9, [[7]]], "out": [[[0]], [[0]]]}, {"args": [[[0.3, -1.2, 4, 2.2], [1.1, 0, -0.5, 3.3]], [[0, 1, 1, 0], [1, 1, 0, 1]], 0.25, [[1, -1, 2, 0.5], [0.3, 0.3, 0.3, 0.3]]], "out": [[[0, -1.5999999999999999, 5.333333333333333, 0], [1.4666666666666668, 0, 0, 4.3999999999999995]], [[0, -1.3333333333333333, 2.6666666666666665, 0], [0.39999999999999997, 0.39999999999999997, 0, 0.39999999999999997]]]}], "url": "https://geodesicpartners.co.uk/ml-inverted-dropout"}
{"slug": "ml-gradient-clipping", "title": "Gradient clipping by global norm", "difficulty": "Easy", "signature": "clip_grad_norm(grads, max_norm)", "starter_python": "import numpy as np\n\ndef clip_grad_norm(grads, max_norm):\n # grads: list of arrays of any shape. Return [clipped_grads, total_norm].\n pass", "tests": [{"args": [[[3], [4]], 1], "out": [[[0.599999880000024], [0.799999840000032]], 5]}, {"args": [[[0.1, 0.2], [[0.1], [0.2]]], 10], "out": [[[0.1, 0.2], [[0.1], [0.2]]], 0.31622776601683794]}, {"args": [[[[1, 1], [1, 1]], [2, 2, 2, 2]], 3], "out": [[[[0.6708202432499704, 0.6708202432499704], [0.6708202432499704, 0.6708202432499704]], [1.3416404864999407, 1.3416404864999407, 1.3416404864999407, 1.3416404864999407]], 4.47213595499958]}, {"args": [[[[0.5, -2, 1.5]], [[0]], [3, -3]], 2.5], "out": [[[[0.2525380851176548, -1.0101523404706192, 0.7576142553529643]], [[0]], [1.5152285107059287, -1.5152285107059287]], 4.949747468305833]}, {"args": [[[1e-09]], 1], "out": [[[1e-09]], 1e-09]}], "url": "https://geodesicpartners.co.uk/ml-gradient-clipping"}
{"slug": "ml-learning-rate-schedule", "title": "Linear warm-up with cosine decay", "difficulty": "Easy", "signature": "lr_schedule(steps, base_lr, min_lr, warmup, total)", "starter_python": "import math\n\ndef lr_schedule(steps, base_lr, min_lr, warmup, total):\n # Return the learning rate for each step, as a list.\n pass", "tests": [{"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": [1, 0.5, 0]}, {"args": [[0, 50, 100, 150], 0.0003, 3e-05, 0, 100], "out": [0.0003, 0.00016499999999999997, 3e-05, 3e-05]}, {"args": [[0, 99, 100, 101, 5000, 10000, 20000], 0.001, 1e-05, 100, 10000], "out": [1e-05, 0.001, 0.001, 0.0009999999750767566, 0.00051285365209823, 1e-05, 1e-05]}], "url": "https://geodesicpartners.co.uk/ml-learning-rate-schedule"}
{"slug": "ml-layer-normalization", "title": "Layer normalisation", "difficulty": "Easy", "signature": "layer_norm(X, gamma, beta, eps)", "starter_python": "import numpy as np\n\ndef layer_norm(X, gamma, beta, eps):\n # X: N x D. Normalise each row over its D features.\n pass", "tests": [{"args": [[[1, 2, 3]], [1, 1, 1], [0, 0, 0], 0], "out": [[-1.224744871391589, 0, 1.224744871391589]]}, {"args": [[[5, 5, 5, 5]], [1, 1, 1, 1], [0.5, 0.5, 0.5, 0.5], 1e-05], "out": [[0.5, 0.5, 0.5, 0.5]]}, {"args": [[[1, 2], [10, 30]], [2, 1], [0, 1], 0], "out": [[-2, 2], [-2, 2]]}, {"args": [[[0.3, -1.2, 4.4, 0, 2.5], [1000000, 1000001, 1000002, 1000003, 1000004]], [1, 0.5, 2, 1, 1], [0, 0, 0, 0.1, -0.1], 1e-05], "out": [[-0.4506759575171558, -0.6009012766895411, 3.204806809010886, -0.5009012766895411, 0.5509763830803363], [-1.4142100268524473, -0.35355250671311184, 0, 0.8071050134262236, 1.3142100268524473]]}], "url": "https://geodesicpartners.co.uk/ml-layer-normalization"}
{"slug": "ml-batch-normalization", "title": "Batch normalisation in training mode", "difficulty": "Medium", "signature": "batch_norm_train(X, gamma, beta, running_mean, running_var, momentum, eps)", "starter_python": "import numpy as np\n\ndef batch_norm_train(X, gamma, beta, running_mean, running_var, momentum, eps):\n # Return [out, new_running_mean, new_running_var].\n pass", "tests": [{"args": [[[1, 10], [3, 30]], [1, 1], [0, 0], [0, 0], [1, 1], 0.1, 0], "out": [[[-1, -1], [1, 1]], [0.2, 2], [1.1, 20.9]]}, {"args": [[[1], [2], [3], [4]], [2], [1], [0], [1], 1, 1e-05], "out": [[[-1.6832708399378538], [0.105576386687382], [1.894423613312618], [3.6832708399378538]], [2.5], [1.6666666666666667]]}, {"args": [[[0.5, -1], [0.5, 3]], [1, 1], [0, 0], [10, 10], [5, 5], 0, 1e-05], "out": [[[0, -0.9999987500023437], [0, 0.9999987500023437]], [10, 10], [5, 5]]}, {"args": [[[0.2, 1.5, -3], [1.1, -0.4, 2.2], [0.7, 0.9, 0], [-1.3, 2.2, 1.1], [0, 0, 0.5]], [1, 0.5, 2], [0.1, 0, -0.2], [0.3, -0.1, 0], [1.2, 0.8, 2], 0.1, 1e-05], "out": [[[0.17349884217267156, 0.34661838606324363, -3.8281058391564793], [1.2759814747627445, -0.6512224223006398, 2.1421949088225367], [0.7859891936116008, 0.031510762369385756, -0.3837015614762775], [-1.6639722121441163, 0.7142439470394112, 0.8792466736731299], [-0.0714972984029002, -0.44115067317140105, 0.19036581813708947]], [0.28400000000000003, -0.0059999999999999915, 0.016000000000000007], [1.1633, 0.8333000000000002, 2.1793]]}], "url": "https://geodesicpartners.co.uk/ml-batch-normalization"}
{"slug": "ml-scaled-dot-product-attention", "title": "Scaled dot-product attention with a causal mask", "difficulty": "Medium", "signature": "attention(Q, K, V, causal)", "starter_python": "import numpy as np\n\ndef attention(Q, K, V, causal):\n # Q: n x d, K: m x d, V: m x dv, causal: bool. Return n x dv.\n pass", "tests": [{"args": [[[1, 0]], [[1, 0], [0, 1]], [[10, 0], [0, 10]], false], "out": [[6.697615493266569, 3.302384506733431]]}, {"args": [[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[1, 2], [3, 4]], true], "out": [[1, 2], [2.3395230986533138, 3.3395230986533138]]}, {"args": [[[0, 0]], [[1, 1], [2, 2], [3, 3]], [[1], [2], [3]], false], "out": [[2]]}, {"args": [[[0.5, -1, 2], [1.5, 0.3, -0.2], [-0.7, 0.8, 0.1], [0.2, -0.4, 1]], [[1, 0, 0.5], [-0.5, 1.2, 0.3], [0.8, -0.8, 0], [0.1, 0.1, 0.9]], [[1, 0], [0, 1], [1, 1], [-1, 2]], true], "out": [[1, 0], [0.7442350393954856, 0.2557649606045144], [0.35388634691118503, 0.7951970318060668], [0.21674361198287245, 1.0307787634658856]]}, {"args": [[[100, 0], [0, 100]], [[1, 0], [0, 1], [1, 1]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]], false], "out": [[4, 5, 6], [5.5, 6.5, 7.5]]}], "url": "https://geodesicpartners.co.uk/ml-scaled-dot-product-attention"}
{"slug": "ml-conv2d", "title": "2-D convolution with stride and padding", "difficulty": "Medium", "signature": "conv2d(image, kernel, stride, padding)", "starter_python": "import numpy as np\n\ndef conv2d(image, kernel, stride, padding):\n # Cross-correlation with zero padding and stride. Return a 2-D list or array.\n pass", "tests": [{"args": [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 0], [0, -1]], 1, 0], "out": [[-4, -4], [-4, -4]]}, {"args": [[[1, 2], [3, 4]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]], 1, 1], "out": [[10, 10], [10, 10]]}, {"args": [[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], [[1, 1], [1, 1]], 2, 0], "out": [[14, 22], [46, 54]]}, {"args": [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]], [[0, 1, 0], [1, -4, 1], [0, 1, 0]], 2, 1], "out": [[4, 2, -6], [-10, 0, -16]]}, {"args": [[[3]], [[2]], 1, 2], "out": [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 6, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]}, {"args": [[[1, 2, 3]], [[1, -1]], 3, 0], "out": [[-1]]}], "url": "https://geodesicpartners.co.uk/ml-conv2d"}
{"slug": "ml-lstm-cell", "title": "One step of an LSTM cell", "difficulty": "Medium", "signature": "lstm_cell(x, h, c, W, b)", "starter_python": "import numpy as np\n\ndef lstm_cell(x, h, c, W, b):\n # Return [h_new, c_new] as lists.\n pass", "tests": [{"args": [[1], [0], [0], [[0.5, 0], [0.5, 0], [1, 0], [1, 0]], [0, 0, 0, 0]], "out": [[0.3227440813830522], [0.47406138896346633]]}, {"args": [[0], [1], [2], [[0, 0], [0, 50], [0, 0], [0, 50]], [-50, 0, 0, 0]], "out": [[0.9640275800758169], [2]]}, {"args": [[1, -1], [0.5], [-0.5], [[1, 2, 3], [0, -1, 1], [2, 0, -2], [-1, 1, 0]], [0.1, -0.2, 0.3, 0]], "out": [[0.019313146337673198], [0.16345950051017266]]}, {"args": [[800], [0], [1], [[1, 0], [-1, 0], [1, 0], [1, 0]], [0, 0, 0, 0]], "out": [[0.7615941559557649], [1]]}, {"args": [[-800], [0], [1], [[1, 0], [-1, 0], [1, 0], [1, 0]], [0, 0, 0, 0]], "out": [[0], [1]]}], "url": "https://geodesicpartners.co.uk/ml-lstm-cell"}
{"slug": "ml-gru-cell", "title": "One step of a GRU cell, as PyTorch computes it", "difficulty": "Medium", "signature": "gru_cell(x, h, W_ih, W_hh, b_ih, b_hh)", "starter_python": "import numpy as np\n\ndef gru_cell(x, h, W_ih, W_hh, b_ih, b_hh):\n # Return h_new as a list.\n pass", "tests": [{"args": [[1], [0], [[1], [1], [1]], [[1], [1], [1]], [0, 0, 0], [0, 0, 0]], "out": [0.20482421480982513]}, {"args": [[0], [1], [[0], [0], [0]], [[0], [50], [0]], [0, 0, 0], [0, 0, 0]], "out": [1]}, {"args": [[1, 2], [0.5, -0.5], [[1, 0], [0, 1], [1, 1], [-1, 0], [0, -1], [1, -1]], [[1, 0], [0, 1], [0.5, 0.5], [1, -1], [2, 0], [0, 2]], [0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [-0.1, -0.2, -0.3, -0.4, -0.5, -0.6]], "out": [0.43847344928785426, -0.7182081868233642]}, {"args": [[600], [0.3], [[1], [-1], [1]], [[0], [0], [1]], [0, 0, 0], [0, 0, 0]], "out": [1]}], "url": "https://geodesicpartners.co.uk/ml-gru-cell"}
{"slug": "ml-beam-search", "title": "Beam search decoding", "difficulty": "Hard", "signature": "beam_search(start, trans, T, B)", "starter_python": "import numpy as np\n\ndef beam_search(start, trans, T, B):\n # Return [best_sequence, best_score].\n pass", "tests": [{"args": [[-0.1, -2.5], [[-2.5, -0.1], [-0.1, -2.5]], 3, 1], "out": [[0, 1, 0], -0.30000000000000004]}, {"args": [[-0.7, -0.7], [[-0.7, -0.7], [-0.7, -0.7]], 2, 2], "out": [[0, 0], -1.4]}, {"args": [[-0.5, -1, -2], [[-3, -0.2, -2], [-0.1, -3, -3], [-0.01, -0.02, -0.03]], 4, 2], "out": [[0, 1, 0, 1], -1]}, {"args": [[-0.2, -0.3], [[-5, -0.01], [-0.01, -5]], 5, 4], "out": [[0, 1, 0, 1, 0], -0.24000000000000005]}, {"args": [[-1], [[-1]], 6, 3], "out": [[0, 0, 0, 0, 0, 0], -6]}], "url": "https://geodesicpartners.co.uk/ml-beam-search"}
{"slug": "ml-cosine-top-k", "title": "Top-k search by cosine similarity", "difficulty": "Easy", "signature": "cosine_top_k(q, M, k)", "starter_python": "import numpy as np\n\ndef cosine_top_k(q, M, k):\n # Return a list of k row indices, most similar first.\n pass", "tests": [{"args": [[1, 0], [[1, 0], [0, 1], [1, 1], [-1, 0]], 2], "out": [0, 2]}, {"args": [[1, 1], [[2, 2], [1, 1], [0, 0], [3, 3]], 3], "out": [0, 1, 3]}, {"args": [[0, 1], [[0, 0], [0, 0], [1, 0]], 3], "out": [0, 1, 2]}, {"args": [[1, 2, 3], [[3, 2, 1], [1, 2, 3], [-1, -2, -3], [2, 4, 6], [1, 0, 0]], 5], "out": [1, 3, 0, 4, 2]}, {"args": [[-1, 1], [[1, -1], [-2, 2], [0, 5], [5, 0]], 1], "out": [1]}], "url": "https://geodesicpartners.co.uk/ml-cosine-top-k"}
{"slug": "ml-info-nce-loss", "title": "The InfoNCE contrastive loss", "difficulty": "Medium", "signature": "info_nce(Q, K, tau)", "starter_python": "import numpy as np\n\ndef info_nce(Q, K, tau):\n # Return the loss as a float.\n pass", "tests": [{"args": [[[1, 0], [0, 1]], [[1, 0], [0, 1]], 1], "out": 0.3132616875182228}, {"args": [[[1, 0], [0, 1]], [[1, 0], [0, 1]], 0.001], "out": 0}, {"args": [[[1, 2], [3, 4], [5, 6]], [[1, 2], [4, 3], [-5, 6]], 0.1], "out": 3.2476224441131634}, {"args": [[[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]], 0.5], "out": 2.2395447662218846}, {"args": [[[2, 2]], [[-1, 3]], 0.07], "out": 0}], "url": "https://geodesicpartners.co.uk/ml-info-nce-loss"}
{"slug": "ml-negative-sampling", "title": "Skip-gram with negative sampling: loss and gradients", "difficulty": "Hard", "signature": "sgns_loss_grad(v, u, U_neg)", "starter_python": "import numpy as np\n\ndef sgns_loss_grad(v, u, U_neg):\n # Return [loss, grad_v, grad_u, grad_U_neg].\n pass", "tests": [{"args": [[1, 0], [1, 0], [[0, 1]]], "out": [1.0064088680781682, [-0.2689414213699951, 0.5], [-0.2689414213699951, 0], [[0.5, 0]]]}, {"args": [[10, 0], [-100, 0], [[100, 0]]], "out": [2000, [200, 0], [-10, 0], [[10, 0]]]}, {"args": [[0.5, -1, 2], [1, 1, 1], [[1, 0, 0], [0, 1, 0], [-1, -1, 2]]], "out": [5.999799694529676, [-0.5489792499739086, -0.9024971598057681, 1.7956005909324573], [-0.09121276190317817, 0.18242552380635635, -0.3648510476127127], [[0.3112296656009273, -0.6224593312018546, 1.2449186624037092], [0.13447071068499755, -0.2689414213699951, 0.5378828427399902], [0.4945065286847034, -0.9890130573694068, 1.9780261147388136]]]}, {"args": [[0, 0], [3, 4], [[1, 1], [2, 2]]], "out": [2.0794415416798357, [0, -0.5], [0, 0], [[0, 0], [0, 0]]]}, {"args": [[30, 0], [30, 0], [[-30, 0], [0, 30]]], "out": [0.6931471805599453, [0, 15], [0, 0], [[0, 0], [15, 0]]]}], "url": "https://geodesicpartners.co.uk/ml-negative-sampling"}
{"slug": "ml-k-nearest-neighbors", "title": "k-nearest neighbours classifier", "difficulty": "Easy", "signature": "knn_predict(X_train, y_train, X_test, k)", "starter_python": "import numpy as np\n\ndef knn_predict(X_train, y_train, X_test, k):\n # Return a list of predicted labels, one per row of X_test.\n pass", "tests": [{"args": [[[0, 0], [1, 0], [5, 5], [6, 5]], [0, 0, 1, 1], [[0.5, 0], [5.5, 5]], 1], "out": [0, 1]}, {"args": [[[0, 0], [2, 0], [4, 0]], [1, 2, 2], [[1, 0]], 3], "out": [2]}, {"args": [[[0, 0], [2, 0], [10, 0]], [7, 3, 3], [[1, 0]], 2], "out": [7]}, {"args": [[[1, 1], [1, -1], [-1, 1], [-1, -1], [3, 3]], [0, 1, 2, 3, 0], [[0, 0], [2, 2], [-2, 0]], 4], "out": [0, 0, 2]}, {"args": [[[0], [1], [2], [3], [4], [5]], [1, 1, 2, 2, 3, 3], [[2.5], [0], [10]], 3], "out": [2, 1, 3]}], "url": "https://geodesicpartners.co.uk/ml-k-nearest-neighbors"}
{"slug": "ml-k-means-clustering", "title": "k-means clustering (Lloyd's algorithm)", "difficulty": "Medium", "signature": "kmeans(X, init, max_iter)", "starter_python": "import numpy as np\n\ndef kmeans(X, init, max_iter):\n # Return [centroids (k x d), labels (n)].\n pass", "tests": [{"args": [[[0, 0], [0, 1], [10, 10], [10, 11]], [[0, 0], [10, 10]], 10], "out": [[[0, 0.5], [10, 10.5]], [0, 0, 1, 1]]}, {"args": [[[0, 0], [1, 0], [2, 0]], [[0, 0], [100, 100]], 10], "out": [[[1, 0], [100, 100]], [0, 0, 0]]}, {"args": [[[0, 0], [4, 0]], [[1, 0], [3, 0]], 0], "out": [[[1, 0], [3, 0]], [0, 1]]}, {"args": [[[1, 1], [1, 2], [2, 1], [8, 8], [8, 9], [9, 8], [1, 8], [2, 9]], [[0, 0], [5, 5], [10, 10]], 50], "out": [[[1.3333333333333333, 1.3333333333333333], [1.5, 8.5], [8.333333333333334, 8.333333333333334]], [0, 0, 0, 2, 2, 2, 1, 1]]}, {"args": [[[0], [1], [2], [10], [11], [12], [20]], [[0], [1], [2]], 100], "out": [[[0], [1.5], [13.25]], [0, 1, 1, 2, 2, 2, 2]]}], "url": "https://geodesicpartners.co.uk/ml-k-means-clustering"}
{"slug": "ml-pca", "title": "Principal component analysis", "difficulty": "Medium", "signature": "pca(X, k)", "starter_python": "import numpy as np\n\ndef pca(X, k):\n # Return [components (k x d), projected (n x k)].\n pass", "tests": [{"args": [[[1, 1], [2, 2], [3, 3]], 1], "out": [[[0.7071067811865476, 0.7071067811865474]], [[-1.414213562373095], [0], [1.414213562373095]]]}, {"args": [[[0, 0], [2, 0], [0, 1], [2, 1]], 2], "out": [[[1, 0], [0, 1]], [[-1, -0.5], [1, -0.5], [-1, 0.5], [1, 0.5]]]}, {"args": [[[1, -2], [-1, 2], [3, -6]], 1], "out": [[[-0.447213595499958, 0.894427190999916]], [[0], [4.47213595499958], [-4.47213595499958]]]}, {"args": [[[2.5, 2.4, 0.5], [0.5, 0.7, 1.1], [2.2, 2.9, 0.3], [1.9, 2.2, 0.9], [3.1, 3, 0.2], [2.3, 2.7, 0.8], [2, 1.6, 1.4], [1, 1.1, 1]], 2], "out": [[[0.6660598134482155, 0.6964993112222387, -0.2669326401478005], [0.5946433249560608, -0.27978653773634055, 0.7537365649792455]], [[0.6744273972524941, 0.036278690154180865], [-2.001900642810423, -0.22512890661861445], [0.876245636858709, -0.4327548891966566], [0.028718590879996984, 0.036944628719510576], [1.572042664099107, -0.0009282070077606209], [0.6700854358851825, 0.05953503333584001], [-0.4560413345824249, 0.6411491663465437], [-1.3635777475826396, -0.11509551573304494]]]}], "url": "https://geodesicpartners.co.uk/ml-pca"}
{"slug": "ml-decision-tree-split", "title": "Best decision-tree split by Gini impurity", "difficulty": "Medium", "signature": "best_split(X, y)", "starter_python": "import numpy as np\n\ndef best_split(X, y):\n # Return [feature, threshold, weighted_gini].\n pass", "tests": [{"args": [[[1], [2], [3], [4]], [0, 0, 1, 1]], "out": [0, 2.5, 0]}, {"args": [[[1, 5], [2, 5], [3, 6], [4, 6]], [0, 1, 0, 1]], "out": [0, 1.5, 0.3333333333333333]}, {"args": [[[7], [7], [7]], [0, 1, 1]], "out": [-1, 0, 0.4444444444444444]}, {"args": [[[2, 3], [1, 1], [3, 2], [2, 4], [5, 1], [4, 4]], [0, 0, 1, 0, 1, 1]], "out": [0, 2.5, 0]}, {"args": [[[1, 1], [1, 2], [2, 1], [2, 2], [3, 3], [3, 3]], [2, 2, 0, 1, 1, 1]], "out": [0, 1.5, 0.25]}], "url": "https://geodesicpartners.co.uk/ml-decision-tree-split"}
{"slug": "ml-gaussian-naive-bayes", "title": "Gaussian naive Bayes classifier", "difficulty": "Medium", "signature": "gaussian_nb(X_train, y_train, X_test)", "starter_python": "import numpy as np\n\ndef gaussian_nb(X_train, y_train, X_test):\n # Return the predicted label for each row of X_test, as a list.\n pass", "tests": [{"args": [[[0, 0], [1, 1], [0, 1], [5, 5], [6, 6], [5, 6]], [0, 0, 0, 1, 1, 1], [[0.5, 0.5], [5.5, 5.5]]], "out": [0, 1]}, {"args": [[[0], [2], [10], [12]], [0, 0, 1, 1], [[6.1]]], "out": [1]}, {"args": [[[0], [1], [2], [0], [20]], [0, 0, 0, 1, 1], [[1]]], "out": [0]}, {"args": [[[1.2, 3.4], [0.8, 2.9], [1.5, 3.1], [4.1, 0.2], [3.8, 0.5], [4.4, 0.1], [2.5, 2.5], [2.7, 2.2]], [3, 3, 3, 7, 7, 7, 9, 9], [[1, 3], [4, 0], [2.6, 2.4], [2, 2]]], "out": [3, 7, 9, 3]}], "url": "https://geodesicpartners.co.uk/ml-gaussian-naive-bayes"}
{"slug": "ml-gmm-em-step", "title": "One EM step for a Gaussian mixture", "difficulty": "Medium", "signature": "em_step(x, pi, mu, var)", "starter_python": "import numpy as np\n\ndef em_step(x, pi, mu, var):\n # Return [new_pi, new_mu, new_var, loglik].\n pass", "tests": [{"args": [[-1, 1], [0.5, 0.5], [0, 0], [1, 1]], "out": [[0.49999999999999994, 0.49999999999999994], [0, 0], [1, 1], -2.8378770664093453]}, {"args": [[0, 0.2, 10, 10.4], [0.5, 0.5], [0, 10], [1, 1]], "out": [[0.5, 0.5], [0.1, 10.2], [0.010000000000000002, 0.04000000000000007], -6.548342855058472]}, {"args": [[0, 1, 2, 9, 10, 11], [0.3, 0.7], [1, 9], [2, 2]], "out": [[0.4999975742412612, 0.5000024257587389], [0.9999953541328674, 9.999960982376587], [0.6666661739040113, 0.6669765040928223], -14.025001332788866]}, {"args": [[1.2, -0.4, 3.3, 2.8, 0.1, 5.5, 4.9, 6.2], [0.2, 0.5, 0.3], [0, 3, 6], [1, 0.5, 2]], "out": [[0.3475367004464585, 0.2820130339515119, 0.3704502656020296], [0.2335372914492443, 2.962516607668044, 5.488912041495864], [0.42869723851744684, 0.5665444275380438, 0.4486773779224253], -18.373298400448466]}], "url": "https://geodesicpartners.co.uk/ml-gmm-em-step"}
{"slug": "ml-viterbi-algorithm", "title": "The Viterbi algorithm for a hidden Markov model", "difficulty": "Medium", "signature": "viterbi(start, trans, emit, obs)", "starter_python": "import numpy as np\n\ndef viterbi(start, trans, emit, obs):\n # Return [path (list of states), logp (float)].\n pass", "tests": [{"args": [[0.6, 0.4], [[0.7, 0.3], [0.4, 0.6]], [[0.5, 0.4, 0.1], [0.1, 0.3, 0.6]], [0, 1, 2]], "out": [[0, 0, 1], -4.19173690823075]}, {"args": [[0.5, 0.5], [[0.9, 0.1], [0.1, 0.9]], [[0.9, 0.1], [0.2, 0.8]], [0, 0, 1, 1, 1]], "out": [[0, 0, 1, 1, 1], -4.191965505785752]}, {"args": [[0.3, 0.7], [[0.5, 0.5], [0.5, 0.5]], [[0.8, 0.2], [0.3, 0.7]], [0]], "out": [[0], -1.4271163556401458]}, {"args": [[0.2, 0.3, 0.5], [[0.6, 0.3, 0.1], [0.2, 0.5, 0.3], [0.1, 0.2, 0.7]], [[0.7, 0.2, 0.1], [0.1, 0.6, 0.3], [0.2, 0.2, 0.6]], [0, 0, 1, 2, 2, 1, 0]], "out": [[0, 0, 1, 2, 2, 2, 2], -11.062936560711798]}], "url": "https://geodesicpartners.co.uk/ml-viterbi-algorithm"}
{"slug": "ml-markov-stationary-distribution", "title": "Stationary distribution of a Markov chain", "difficulty": "Easy", "signature": "stationary(P)", "starter_python": "import numpy as np\n\ndef stationary(P):\n # Return the stationary distribution as a list.\n pass", "tests": [{"args": [[[0.9, 0.1], [0.5, 0.5]]], "out": [0.8333333333333334, 0.16666666666666663]}, {"args": [[[0, 1], [0.5, 0.5]]], "out": [0.3333333333333333, 0.6666666666666666]}, {"args": [[[0.5, 0.25, 0.25], [0.25, 0.5, 0.25], [0.25, 0.25, 0.5]]], "out": [0.33333333333333337, 0.3333333333333333, 0.3333333333333333]}, {"args": [[[1]]], "out": [1]}, {"args": [[[0.2, 0.3, 0.5, 0], [0.1, 0.1, 0.4, 0.4], [0.6, 0, 0.2, 0.2], [0.25, 0.25, 0.25, 0.25]]], "out": [0.3284823284823285, 0.158004158004158, 0.33887733887733884, 0.17463617463617467]}], "url": "https://geodesicpartners.co.uk/ml-markov-stationary-distribution"}
{"slug": "ml-running-variance", "title": "Running mean and variance in one pass", "difficulty": "Easy", "signature": "running_stats(xs)", "starter_python": "def running_stats(xs):\n # Return [[mean, variance], ...], one record after each value.\n pass", "tests": [{"args": [[2, 4, 6]], "out": [[2, 0], [3, 2], [4, 4]]}, {"args": [[1000000004, 1000000007, 1000000013, 1000000016]], "out": [[1000000004, 0], [1000000005.5, 4.5], [1000000008, 21], [1000000010, 30]]}, {"args": [[5]], "out": [[5, 0]]}, {"args": [[1, 1, 1, 1]], "out": [[1, 0], [1, 0], [1, 0], [1, 0]]}, {"args": [[-3.5, 2.25, 0, 10, -1]], "out": [[-3.5, 0], [-0.625, 16.53125], [-0.41666666666666663, 8.395833333333334], [2.1875, 32.723958333333336], [1.55, 26.575]]}, {"args": [[1000000000000, 1000000000001, 1000000000002]], "out": [[1000000000000, 0], [1000000000000.5, 0.5], [1000000000001, 1]]}, {"args": [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6]], "out": [[0.1, 0], [0.15000000000000002, 0.004999999999999999], [0.2, 0.009999999999999995], [0.25, 0.016666666666666666], [0.3, 0.025], [0.35, 0.034999999999999996]]}], "url": "https://geodesicpartners.co.uk/ml-running-variance"}
{"slug": "ml-roc-auc", "title": "ROC AUC from scores, with ties", "difficulty": "Medium", "signature": "roc_auc(scores, labels)", "starter_python": "import numpy as np\n\ndef roc_auc(scores, labels):\n # Return the AUC as a float.\n pass", "tests": [{"args": [[0.9, 0.8, 0.3, 0.1], [1, 1, 0, 0]], "out": 1}, {"args": [[0.5, 0.5, 0.5, 0.5], [1, 0, 1, 0]], "out": 0.5}, {"args": [[0.1, 0.4, 0.35, 0.8], [0, 0, 1, 1]], "out": 0.75}, {"args": [[0.1, 0.9], [1, 0]], "out": 0}, {"args": [[3, 1, 2, 2, 3, 1, 2], [1, 0, 1, 0, 0, 1, 1]], "out": 0.5}, {"args": [[0.2, 0.2, 0.7, 0.7, 0.7, 0.1], [0, 1, 1, 0, 1, 0]], "out": 0.7222222222222222}], "url": "https://geodesicpartners.co.uk/ml-roc-auc"}
{"slug": "ml-spearman-rank-correlation", "title": "Spearman rank correlation (the information coefficient)", "difficulty": "Medium", "signature": "spearman(x, y)", "starter_python": "import numpy as np\n\ndef spearman(x, y):\n # Return the Spearman rank correlation as a float.\n pass", "tests": [{"args": [[1, 2, 3, 4], [10, 20, 30, 40]], "out": 1}, {"args": [[1, 2, 3, 4], [1, 8, 27, 1000]], "out": 1}, {"args": [[1, 2, 2, 3], [4, 3, 3, 1]], "out": -1}, {"args": [[0.3, -1.2, 0.8, 0.8, 2, -0.5], [0.01, -0.02, 0, 0.03, 0.02, 0]], "out": 0.75}, {"args": [[1, 2], [2, 1]], "out": -1}, {"args": [[5, 3, 5, 1, 3, 5, 2], [1, 2, 3, 4, 5, 6, 7]], "out": -0.35552856456843185}], "url": "https://geodesicpartners.co.uk/ml-spearman-rank-correlation"}
{"slug": "ml-autocorrelation-function", "title": "The autocorrelation function of a series", "difficulty": "Easy", "signature": "autocorrelation(x, max_lag)", "starter_python": "import numpy as np\ndef autocorrelation(x, max_lag):\n # Return [r_0, r_1, ..., r_max_lag].\n pass", "tests": [{"args": [[1, 2, 3, 4, 5], 2], "out": [1, 0.4, -0.1]}, {"args": [[1, -1, 1, -1, 1, -1], 1], "out": [1, -0.8333333333333334]}, {"args": [[0.5, 0.1, -0.3, 0.2], 0], "out": [1]}, {"args": [[3, 1, 4, 1, 5, 9, 2, 6], 4], "out": [1, -0.17523640661938533, 0.044326241134751775, 0.1456855791962175, -0.41725768321513]}, {"args": [[0.01, -0.02, 0.015, 0.003, -0.011, 0.007], 3], "out": [1, -0.6177268244575937, -0.07421104536489148, 0.39238165680473375]}, {"args": [[100, 101, 99, 102, 98, 103], 5], "out": [1, -0.7571428571428571, 0.5142857142857142, -0.32857142857142857, 0.14285714285714285, -0.07142857142857142]}], "url": "https://geodesicpartners.co.uk/ml-autocorrelation-function"}
{"slug": "ml-ols-standard-errors", "title": "Regression coefficients with standard errors and t-statistics", "difficulty": "Medium", "signature": "ols_inference(X, y)", "starter_python": "import numpy as np\ndef ols_inference(X, y):\n # Return [beta, se, t], each a list of p floats.\n pass", "tests": [{"args": [[[1, 0], [1, 1], [1, 2], [1, 3]], [1, 3, 2, 5]], "out": [[1.0999999999999999, 1.1], [0.9721111047611788, 0.519615242270663], [1.1315578997220075, 2.1169509870286287]]}, {"args": [[[1, 1], [1, 2], [1, 3], [1, 4], [1, 5]], [2.1, 3.9, 6.2, 7.8, 10.1]], "out": [[0.04999999999999786, 1.9900000000000007], [0.19807406022327423, 0.05972157622389635], [0.25243083290985485, 33.3212906595011]]}, {"args": [[[1], [1], [1]], [2, 4, 9]], "out": [[5], [2.081665999466133], [2.4019223070763065]]}, {"args": [[[1, 0.5, -1.2], [1, 1.5, 0.3], [1, -0.7, 2.2], [1, 2.4, 0.9], [1, 0.1, -0.4], [1, -1.3, 1.1], [1, 0.8, 0]], [1.2, 3.1, 0.4, 4.4, 0.9, -0.5, 2]], "out": [[0.8900813150832596, 1.3697964696164915, 0.25831118782164186], [0.04102469438289523, 0.03037730749271544, 0.034601258779443415], [21.696232683073166, 45.09275451568485, 7.465369669588563]]}, {"args": [[[1, 100], [1, 100.1], [1, 100.2], [1, 100.3], [1, 100.4]], [5, 5.3, 5.1, 5.6, 5.7]], "out": [[-164.9999999999872, 1.6999999999998723], [52.70436351321751, 0.5259911279353204], [-3.130670574526672, 3.2319936776745712]]}], "url": "https://geodesicpartners.co.uk/ml-ols-standard-errors"}
{"slug": "ml-permutation-test", "title": "An exact permutation test for a difference in means", "difficulty": "Medium", "signature": "permutation_p_value(a, b)", "starter_python": "from itertools import combinations\ndef permutation_p_value(a, b):\n # Return the exact two-sided p-value as a float.\n pass", "tests": [{"args": [[7, 9, 12], [3, 4, 6]], "out": 0.1}, {"args": [[1, 2], [1, 2]], "out": 1}, {"args": [[5], [1, 2, 3, 4]], "out": 0.4}, {"args": [[10, 12, 14, 16], [11, 13, 15, 17]], "out": 0.6857142857142857}, {"args": [[3, 3, 3], [3, 3, 3, 3]], "out": 1}, {"args": [[-2, 5, 8, 1, 0], [4, 9, 12, 7, 6, 10]], "out": 0.03463203463203463}, {"args": [[100], [0]], "out": 1}], "url": "https://geodesicpartners.co.uk/ml-permutation-test"}
{"slug": "ml-kalman-filter-1d", "title": "A one-dimensional Kalman filter", "difficulty": "Medium", "signature": "kalman_filter(ys, q, r, m0, p0)", "starter_python": "def kalman_filter(ys, q, r, m0, p0):\n # Return [[mean, variance], ...], one record after each observation.\n pass", "tests": [{"args": [[10, 12, 11], 1, 4, 8, 9], "out": [[9.384615384615385, 2.769230769230769], [10.653465346534654, 1.9405940594059405], [10.800285306704708, 1.6947218259629102]]}, {"args": [[5, 5, 5, 5], 0, 1, 0, 100], "out": [[4.9504950495049505, 0.990099009900991], [4.975124378109452, 0.49751243781094556], [4.983388704318936, 0.3322259136212626], [4.987531172069825, 0.24937655860349137]]}, {"args": [[3], 2, 1, 0, 1], "out": [[1.5, 0.5]]}, {"args": [[1.5, 2.25, 1.75, 3, 2.5, 4], 0.5, 2, 0, 10], "out": [[1.25, 1.6666666666666663], [1.77, 1.0399999999999998], [1.7612994350282487, 0.8700564971751413], [2.2648784576697403, 0.8130762782900252], [2.358064516129032, 0.7926628716002532], [3.0026701500278543, 0.7851777858885454]]}, {"args": [[100, 101, 99, 102], 0.1, 0.5, 100, 0.25], "out": [[100, 0.16666666666666669], [100.34782608695652, 0.1739130434782609], [99.87078651685393, 0.17696629213483145], [100.6297903109183, 0.17823571945047]]}], "url": "https://geodesicpartners.co.uk/ml-kalman-filter-1d"}
{"slug": "ml-ndcg-at-k", "title": "NDCG at k", "difficulty": "Easy", "signature": "ndcg_at_k(rels, k)", "starter_python": "import numpy as np\n\ndef ndcg_at_k(rels, k):\n # Return NDCG@k as a float.\n pass", "tests": [{"args": [[3, 2, 3, 0, 1, 2], 6], "out": 0.9488107485678985}, {"args": [[0, 0, 0], 3], "out": 0}, {"args": [[1, 0, 0], 1], "out": 1}, {"args": [[0, 0, 1], 3], "out": 0.5}, {"args": [[2, 1], 10], "out": 1}, {"args": [[0, 3, 2, 1], 2], "out": 0.4966392596877323}], "url": "https://geodesicpartners.co.uk/ml-ndcg-at-k"}
{"slug": "ml-expected-calibration-error", "title": "Expected calibration error", "difficulty": "Medium", "signature": "expected_calibration_error(p, y, m)", "starter_python": "import numpy as np\n\ndef expected_calibration_error(p, y, m):\n # Return ECE as a float.\n pass", "tests": [{"args": [[0.1, 0.2, 0.8, 0.9], [0, 0, 1, 1], 2], "out": 0.14999999999999997}, {"args": [[1, 1, 0], [1, 1, 0], 10], "out": 0}, {"args": [[0.55, 0.55, 0.55, 0.55], [1, 0, 0, 0], 5], "out": 0.30000000000000004}, {"args": [[0, 0.25, 0.5, 0.75, 1], [0, 1, 0, 1, 1], 4], "out": 0.3}, {"args": [[0.3], [1], 1], "out": 0.7}], "url": "https://geodesicpartners.co.uk/ml-expected-calibration-error"}
{"slug": "ml-macro-f1", "title": "Macro-averaged F1 across classes", "difficulty": "Easy", "signature": "macro_f1(y_true, y_pred)", "starter_python": "import numpy as np\n\ndef macro_f1(y_true, y_pred):\n # Return macro-F1 as a float.\n pass", "tests": [{"args": [[0, 0, 1, 1, 2], [0, 1, 1, 1, 0]], "out": 0.43333333333333335}, {"args": [[5, 5, 5], [5, 5, 5]], "out": 1}, {"args": [[0, 1], [1, 0]], "out": 0}, {"args": [[1, 2, 3, 4], [1, 2, 3, 9]], "out": 0.6}, {"args": [[0, 0, 0, 1], [0, 0, 0, 0]], "out": 0.42857142857142855}], "url": "https://geodesicpartners.co.uk/ml-macro-f1"}
|