Skip to content

Understanding Data Preparation

Data preparation is a crucial step in any data science or machine learning project. The quality of your data directly impacts the performance of your models and the reliability of your insights. This guide explores key concepts and techniques in data preparation, from handling missing values to addressing imbalanced datasets.

Missing data is a common challenge in real-world datasets. Understanding the mechanism behind missing data is crucial for choosing the appropriate treatment method.

  1. Missing Completely At Random (MCAR) When data is MCAR, the missing values have no relationship with any other variables in the dataset. For example, if a sensor randomly malfunctions, the missing readings would be MCAR. In this case, simple imputation methods are appropriate:
    • Mean imputation for numerical data
    • Median imputation for skewed distributions
    • KNN imputation for maintaining relationships between variables
  2. Missing At Random (MAR) MAR occurs when the missingness can be explained by other variables in the dataset. For instance, if younger people are less likely to report their income, the missing income data is MAR. Multiple Imputation by Chained Equations (MICE) is particularly effective for MAR data as it:
    • Creates multiple complete datasets
    • Accounts for uncertainty in imputations
    • Preserves relationships between variables
  3. Missing Not At Random (MNAR) MNAR occurs when the missingness depends on the missing values themselves. For example, if people with high incomes are less likely to report their income. MNAR requires specialized approaches:
    • Selection models that explicitly model the missing data mechanism
    • Pattern-mixture models that stratify data based on missing patterns
    • Shared parameter models that link the measurement and missing data processes

Text data often contains noise that can hinder model performance. Stop words are a prime example of such noise.

Stop words are common words like “the,” “is,” and “at” that carry little meaningful information. Removing them:

  • Reduces the dimensionality of the text data
  • Improves processing speed
  • Helps focus on meaningful content

Both NLTK and spaCy libraries offer comprehensive stop word lists, but you can also customize these lists based on your specific needs. For instance, in a technical document analysis, words like “figure” or “table” might be considered stop words.

Raw data often contains irregularities and inconsistencies that need addressing.

Several factors can lead to data corruption:

  • User input errors (e.g., incorrect date formats)
  • System synchronization issues
  • Processing errors during data transfer

NumPy and Pandas provide robust tools for handling these issues. For complex cases, custom Python functions can be developed to address specific formatting requirements.

Different features often have different scales, which can bias machine learning models. Common scaling techniques include:

  • StandardScaler: Transforms data to have zero mean and unit variance
  • MinMaxScaler: Scales data to a fixed range, usually [0,1]
  • RobustScaler: Scales data using statistics that are robust to outliers

Outliers are data points that significantly deviate from the general pattern of the dataset.

  1. Z-score Method This method assumes normal distribution and flags points that are several standard deviations away from the mean:

    Z-score = (x - mean) / standard deviation

    Points with |Z-score| > 3 are typically considered outliers.

  2. Interquartile Range (IQR) Method This method is more robust to non-normal distributions:

    IQR = Q3 - Q1
    Lower bound = Q1 - 1.5 * IQR
    Upper bound = Q3 + 1.5 * IQR

Not all outliers are errors - some may represent genuine rare events. Before treating outliers:

  • Consult domain experts
  • Understand the business context
  • Consider the impact on your analysis

An imbalanced dataset occurs when one class significantly outnumbers others, which can lead to biased models.

  1. Sampling Techniques
    • Undersampling: Reducing majority class samples
    • Oversampling: Increasing minority class samples Both approaches have drawbacks: undersampling may lose important information, while oversampling may lead to overfitting.
  2. SMOTE (Synthetic Minority Over-sampling Technique) SMOTE generates synthetic samples for the minority class by:
    • Identifying k-nearest neighbors for minority class samples
    • Creating new samples along the lines connecting these points This approach helps avoid overfitting while balancing the dataset.

Data labeling is crucial for supervised learning tasks but can be resource-intensive.

Amazon SageMaker Ground Truth was the AWS labelling service, coordinating a private, vendor or Mechanical Turk workforce and using automated labelling to reduce the human effort. It is no longer open to new customers — AWS moved it to maintenance on 30 July 2026 and does not plan to add features. The fully managed variant, Ground Truth Plus, reached end of support on 30 June 2026. Amazon Mechanical Turk itself closes on 30 September 2026, so from that date its workforce option disappears from Ground Truth and Augmented AI.

For a new pipeline, the practical options are:

  • A third-party labelling platform writing manifests into S3 in the format the training job expects
  • An internal labelling tool with a private workforce, where the data is too sensitive to leave the organisation
  • Labels that already exist as a by-product of a manual business process — often the cheapest labelled dataset available, and usually already in a database
  • Weak supervision, using heuristics or labelling functions to produce noisy labels at volume, refined by reviewing the cases a first model is least confident about

Effective data preparation is fundamental to successful data science projects. While it may be time-consuming, proper data preparation:

  • Improves model performance
  • Reduces training time
  • Leads to more reliable insights
  • Prevents garbage-in-garbage-out scenarios

Remember that data preparation is not a one-size-fits-all process. Each dataset and project may require different combinations of these techniques, and the choice of methods should be guided by both statistical principles and domain knowledge.

import numpy as np
import pandas as pd
import missingno as msno
from sklearn.impute import KNNImputer, SimpleImputer
from sklearn.preprocessing import StandardScaler
import nltk
from nltk.corpus import stopwords
import spacy
from imblearn.over_sampling import SMOTE
# 1. Missing Data Analysis and Handling
def analyze_missing_data(df):
"""
Visualize and analyze missing data patterns
"""
# Visualize missing data patterns
msno.matrix(df)
msno.heatmap(df)
# Calculate missing percentages
missing_percentages = df.isnull().sum() / len(df) * 100
return missing_percentages
def handle_missing_data(df, mcar_columns, mar_columns, categorical_columns):
"""
Handle missing data according to the missingness mechanism.
Each column is imputed once, by the method appropriate to its mechanism.
Running mean imputation over every numerical column first would leave no
NaNs for the KNN imputer to fill, making the second step a silent no-op.
"""
# MCAR: mean imputation is adequate where missingness is unrelated to the data
if mcar_columns:
mean_imputer = SimpleImputer(strategy='mean')
df[mcar_columns] = mean_imputer.fit_transform(df[mcar_columns])
# MAR: KNN imputation uses the other variables that explain the missingness
if mar_columns:
knn_imputer = KNNImputer(n_neighbors=5)
df[mar_columns] = knn_imputer.fit_transform(df[mar_columns])
# Mode imputation for categorical data
if categorical_columns:
cat_imputer = SimpleImputer(strategy='most_frequent')
df[categorical_columns] = cat_imputer.fit_transform(df[categorical_columns])
return df
# 2. Text Processing and Stop Words
def process_text(text, custom_stop_words=None):
"""
Process text data and remove stop words
"""
# Download required NLTK data
nltk.download('stopwords')
# Get default stop words and add custom ones
stop_words = set(stopwords.words('english'))
if custom_stop_words:
stop_words.update(custom_stop_words)
# Remove stop words
words = text.lower().split()
filtered_words = [word for word in words if word not in stop_words]
return ' '.join(filtered_words)
# 3. Outlier Detection and Handling
def detect_outliers(data):
"""
Detect outliers using Z-score and IQR methods
"""
# Z-score method
z_scores = (data - np.mean(data)) / np.std(data)
z_score_outliers = np.abs(z_scores) > 3
# IQR method
Q1 = data.quantile(0.25)
Q3 = data.quantile(0.75)
IQR = Q3 - Q1
IQR_outliers = (data < (Q1 - 1.5 * IQR)) | (data > (Q3 + 1.5 * IQR))
return z_score_outliers, IQR_outliers
# 4. Handle Imbalanced Data
def balance_dataset(X, y):
"""
Balance dataset using SMOTE
"""
smote = SMOTE(random_state=42)
X_balanced, y_balanced = smote.fit_resample(X, y)
return X_balanced, y_balanced
# 5. Data Formatting and Scaling
def format_and_scale_data(df, numerical_columns):
"""
Format and scale numerical data
"""
# Handle data type conversions
for col in numerical_columns:
df[col] = pd.to_numeric(df[col], errors='coerce')
# Scale numerical features
scaler = StandardScaler()
df[numerical_columns] = scaler.fit_transform(df[numerical_columns])
return df
# Example usage
if __name__ == "__main__":
# Load sample data
df = pd.DataFrame({
'num_feature': [1, 2, np.nan, 4, 100],
'cat_feature': ['A', None, 'B', 'A', 'C'],
'text_feature': ['This is a sample text', 'Another example with stop words', 'More text data']
})
# Analyze missing data
missing_analysis = analyze_missing_data(df)
# Handle missing data
df = handle_missing_data(df,
mcar_columns=['num_feature'],
mar_columns=[],
categorical_columns=['cat_feature'])
# Process text
df['text_feature'] = df['text_feature'].apply(process_text)
# Detect outliers
z_outliers, iqr_outliers = detect_outliers(df['num_feature'])
# Format and scale data
df = format_and_scale_data(df, numerical_columns=['num_feature'])