In science, sampling and games of chance we want to calculate the probability of picking certain items, at random, from some larger collection. For example: Draw 7 cards from a standard deck of 52 cards. What’s the probability we see 2 or more of the 4 kings in the draw? If we assume that each item in the collection has an equal probability of being selected on each draw, and the item is not…
Jane Street posted a Christmas-themed puzzle in January 2021: Jane received 78 figurines as gifts this holiday season: 12 drummers drumming, 11 pipers piping, 10 lords a-leaping, etc., down to 1 partridge in a pear tree. They are all mixed together in a big bag. She agrees with her friend Alex that this seems like too many figurines for one person to have, so she decides to give some of her…
In Part 1 and Part 2 we looked at key array attributes such as shape, strides and offset and how NumPy implements array operations by changing these attributes to traverse a contiguous region of memory in different ways. In this final post on the topic and shape and strides, we’ll look at ways to bypass NumPy’s convenient high-level methods for manipulating an array’s shape and strides ( reshape ,…
Part 2 follows on from the key concepts of strided arrays introduced in the previous post and examines how NumPy implements other fundamental array concepts using strides: Transposing Arrays and Permuting Axes C order vs. Fortran order Ravelling Arrays 1. Transposing Arrays and Permuting Axes Transposing an array means reversing the order of its axes. The transpose of a 1D array is itself. The…
Welcome to the first part of a three-part illustrated guide examining shapes, strides and multidimensionality in NumPy . The idea of strided arrays is simple, and is a basis for implementing arrays, matrices and tensors in many higher-level languages and frameworks including TensorFlow and Julia . Strides allow solutions to mind-bending multi-dimensional problems. A better understanding of how…
You might see the approximation: \[\pi \approx 768{\sqrt {2-{\sqrt {2+{\sqrt {2+{\sqrt {2+{\sqrt {2+{\sqrt {2+{\sqrt {2+{\sqrt {2+{\sqrt {2+1}}}}}}}}}}}}}}}}}}\] and think: Hmmm! How accurate is that? You might then proceed to check using a scientific calculator, your phone, or your favourite programming language. But if you’re like me, your next thought will be: …this is tedious and surely more…
There are 12 unique permutations of the word ‘food’. Of these, there are only six that satisfy the constraint that no adjacent letters are equal: fodo odof odfo ofdo ofod dofo How can we efficiently count the number of such permutations for a given word? Approach 1: Brute Force The simplest approach is to generate each and every possible permutation of the word in turn, checking whether any…
Expectation Maximisation is a fantastically useful algorithm used to estimate model parameters (e.g. the bias of a coin, or the mean of data points) when some information about our data is hidden from us. A while ago, I felt that I didn’t have a good intuition about how the algorithm worked and why the estimates for parameters always converged. This post is an attempt to organise my reasoning and…
The inverse square root of a number x is x -1/2 . For example, put in 25, you’ll get back 0.2: the square root of 25 is 5, the inverse of 5 is 1/5, or 0.2 in decimal notation. It’s a very common calculation in computer graphics, for example, where you need to normalise a lot of vectors. If you’ve marvelled at the Fast Inverse Square Root method and want to use this piece of witchcraft to speed up…
I’ve started compiling a list of short pandas puzzles of varying difficulty. You can find it over on GitHub here . There are already some great guides to pandas out there (not least in the official documents themselves) but nothing short and punchy to measure your skills against like, say, the 100 Numpy exercises project. Hopefully this goes a little way towards filling that gap. As I write this…