A Peek at Data Sampling Methods | by Kurtis Pykes | Towards Data Science

Sampling Bias is one of the most common types of biases observed in real-world scenarios. It occurs when the data used to train a model doesn’t reflect the distribution of the samples that the model will receive whilst in production.
towardsdatascience.com
Generally, whenever we work on Machine Learning projects, it’s vital that the correct research is done about the real proportions of various properties in the data that will be observed in a production environment.
Additionally, when we are actually working on a problem and the data set is very large, it’s usually not practical, nor necessary, to work with the entire data set — unless you want to be waiting hours whilst your processing transformations and feature engineering take place.
A more effective strategy, which would still allow you to draw valid conclusions from your results, is to draw a sample from your data that is informative enough for learning valuable insights. This technique is known as Data Sampling.
We refer to a subset of observations from a larger population as a sample. Sampling, however, is used in reference to the group of observations that data will be collected from for our research. Therefore, Data Sampling may be defined as a technique leveraged by practitioners to select a subset of observations that is representative of the larger population.
Typically, people that work with data (i.e. Data Scientists, Data Analysts, etc.) use data sampling techniques to reduce a large data set to a smaller, more manageable amount of data without sacrificing the accuracy of their insights.
There are 2 main data sampling strategies:
Probability sampling involves random selection. All of the observations within the data have a chance to be selected, therefore strong statistical inferences could be made about the entire group.
Non-probability sampling does not involve random selection. Instead, it’s based on convenience or other criteria. As a result, some observations do not have a chance of being selected regardless of the number of samples that are built.
A major pitfall of the non-probability sampling methods is that they include non-representative samples and it is possible that important observations are excluded from the sample. Consequently, it’s usually recommended to consider probability sampling methods first which is why I will only be focusing on such sampling methods for the remainder of this article.
As mentioned prior, we typically leverage data sampling methods in situations where it’s impractical to study an entire population because the data is too large, among other reasons (i.e. time, costs, etc.).
For this demonstration, we will be using synthetic data I’ve created in Python — See code below.
import numpy as npimport pandas as pd# create synthetic dataid = np.arange(0, 10).tolist()distance = np.round(np.random.normal(loc=100, scale =5, size=len(id)), 2)# convert to pandas dataframedata = {"id":id, "distance": distance}df = pd.DataFrame(data=data)df
The most straightforward way to sample data is with simple random sampling. Essentially, the subset is built of observations that were chosen from a larger set purely by chance; Each observation has the same chance of being selected from the larger set.
# simple sampling examplesimple_random_sample = df.sample(n=5, random_state=24)simple_random_sample
Simple random sampling is extremely simple and easy to implement. On the other hand, it’s possible that we can still introduce bias into our sample data. For instance, consider an event where we have a large dataset with imbalanced labels. By performing simple random sampling, we may accidentally fail to capture enough examples to represent the minority class — if we catch any of them at all.
Interval sampling is a technique that creates a subset by selecting observations from a larger set at a regular interval. For instance, we could decide to select every 31st observation from the larger set.
# interval sampling exampleidx = np.arange(0, len(df), step=2)interval_sample = df.iloc[idx]interval_sample
If the observations are randomized, then interval sampling typically tends to return a better sample than simple random sampling. However, if there is periodicity or repetitive patterns within our data then interval sampling is quite inappropriate.
Stratified random sampling divides the larger data set into groups known as strata. From these groups, we randomly select the observations we want to create the new subset. The number of examples to select from each stratum is proportional to the size of the stratum.
from sklearn.model_selection import StratifiedKFold# dividing the data into groupsdf["strata"] = np.repeat([1, 2], len(df)/2).tolist()# instantiating stratified samplingstratified = StratifiedKFold(n_splits=2)1qfor x, y in stratified.split(df, df["strata"]):    stratified_random_sample = df.iloc[x]stratified_random_sample
This sampling strategy tends to improve the representativeness of the sample by reducing the amount of bias we introduce; In the worst-case scenario, our resulting sample would be of no less quality than that of simple random sampling. On the other hand, defining the strata can be a difficult task because it requires good knowledge of the properties of the data. It’s also the slowest of the methods presented.
When we do not know how to define the strata of the data, cluster random sampling is a good alternative. Upon deciding the number of clusters we’d like our data to have, we then divide the larger set into these smaller clusters, then randomly select from among them to form a sample.
# cluster sampling example# removing the stratadf.drop("strata", axis=1, inplace=True)# Divide the units into 5 clusters of equal sizedf['cluster_id'] = np.repeat([range(1,6)], len(df)/5)# Append the indexes from the clusters that meet the criteriaidx = []# add all observations with an even cluster_id to idxfor i in range(0, len(df)):    if df['cluster_id'].iloc[i] % 2 == 0:        idx.append(i)cluster_random_sample = df.iloc[idx]cluster_random_sample
Cluster sampling is much more time and cost-efficient than the other probability sampling methods. However, it’s difficult to ensure that your clusters are representative of the larger set, therefore, it often provides less statistical certainty than other methods like simple random sampling.
Data sampling is an effective technique to use when working with a large data set. By leveraging data sampling techniques, we can sample a smaller, easier manage subset of the larger set to perform our analysis and modeling whilst ensuring we still draw valid conclusions from this subset. In this article, we covered the 2 main ways to perform data sampling, why it may be better to start with probability sampling techniques, and implemented 4 probability sample techniques in python.
Thank you for reading!
If you enjoyed this article, connect with me by subscribing to my FREE weekly newsletter. Never miss a post I make about Artificial Intelligence, Data Science, and Freelancing.