Welcome to our deep dive into Reservoir Sampling, a powerful algorithm used for selecting random samples from a large data stream. This tutorial is designed for both beginners and intermediates, so let's get started! š
Reservoir Sampling is an efficient method for randomly selecting a fixed-size subset from a large data stream, such as log files, network traffic, or time series data. The algorithm ensures that each data point has an equal chance of being selected, regardless of when it appears in the stream.
Reservoir Sampling is crucial in various applications like data analysis, machine learning, and performance monitoring. It allows us to work with smaller datasets while still preserving the essential characteristics of the original data stream.
Our goal is to maintain a reservoir containing the current best k items, ensuring that each item in the data stream has an equal probability of being selected. Here's a step-by-step guide to the Reservoir Sampling algorithm:
k items from the data stream.k/i, replace a random item in the reservoir with the current item. Here, i is the current position in the data stream.The final reservoir will contain the selected sample.
Let's see how Reservoir Sampling works with a simple example. Suppose we have a data stream consisting of 100 items:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 100]
If we want to select a sample of size k=3, our reservoir would look like this:
[1, 2, 3]3/4 = 0.75 > random number between 0 and 1, so we replace item 3 with 4: [1, 2, 4]3/5 = 0.6 > random number between 0 and 1, so we replace item 2 with 5: [1, 5, 4]3/6 = 0.5 > random number between 0 and 1, so we replace item 1 with 6: [6, 5, 4]What is the purpose of Reservoir Sampling in data analysis?
In this tutorial, we learned about Reservoir Sampling, an essential algorithm for selecting random samples from a large data stream. We discussed its importance, key concepts, and an example of its practical application. Now, you're ready to use Reservoir Sampling in your own projects! š
Here are two code examples demonstrating Reservoir Sampling in Python and Java:
import random
def reservoir_sample(data_stream, k):
reservoir = [data_stream[0]] * k
for i, item in enumerate(data_stream[1:], start=1):
if random.random() <= k/i:
reservoir[random.randint(0, k-1)] = item
return reservoirimport java.util.Random;
import java.util.stream.IntStream;
public class ReservoirSampling {
public static int[] reservoirSample(int[] data_stream, int k) {
int[] reservoir = new int[k];
for (int i = 0, j = 0; i < data_stream.length; i++, j = (int) (Math.random() * k)) {
if (j < k) {
reservoir[j] = data_stream[i];
} else if (Math.random() <= k / (i + 1)) {
reservoir[j] = data_stream[i];
}
}
return reservoir;
}
public static void main(String[] args) {
int[] data_stream = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k = 3;
int[] reservoir = reservoirSample(data_stream, k);
System.out.println("Reservoir Sample: " + java.util.Arrays.toString(reservoir));
}
}