Practical Data Preprocessing Techniques for Machine Learning in Python
Real-world datasets are rarely ready for immediate model training. They frequently contain missing values, inconsistent formatting, and significant noise or outliers. When algorithms process low-quality input, the resulting predictions degrade substantially. Preprocessing transforms raw information into a clean, consistant format that machine learning models can interpret effectively.
Scikit-learn standardizes this workflow through a consistent API patern. Transformers typically implement a fit() method to calculate necessary parameters (like mean or min/max values) and a transform() method to apply those calculations to the dataset. For convenience, fit_transform() combines both steps. This approach ensures that training and testing data undergo identical transformations without data leakage.
Min-Max Scaling
Algorithms sensitive to feature magnitude, such as K-Nearest Neighbors, neural networks, and gradient descent-based optimizers, benefit significantly from uniform feature ranges. Min-max scaling compresses all numerical attributes into a specified interval, typically [0, 1]. This technique preserves the original distribution shape while eliminating scale disparities.
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
# Load dataset and separate features from labels
dataset = pd.read_csv('dataset.csv', header=None)
feature_matrix = dataset.iloc[:, :-1].values
label_vector = dataset.iloc[:, -1].values
# Apply min-max scaling to the [0, 1] range
range_scaler = MinMaxScaler(feature_range=(0, 1))
scaled_features = range_scaler.fit_transform(feature_matrix)
np.set_printoptions(precision=3, suppress=True)
print(scaled_features)
Standardization (Z-score Normalization)
Linear models, logistic regression, and linear discriminant analysis often assume that input features follow a Gaussian distribution centered around zero. Standardization adjusts each feature to have a mean of 0 and a standard deviation of 1. Unlike min-max scaling, this method is less affected by outliers and does not bound values to a fixed range.
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
raw_data = pd.read_csv('dataset.csv', header=None)
inputs = raw_data.iloc[:, :-1].values
outputs = raw_data.iloc[:, -1].values
# Compute mean and variance, then standardize
z_score_scaler = StandardScaler()
z_score_scaler.fit(inputs)
standardized_inputs = z_score_scaler.transform(inputs)
np.set_printoptions(precision=3, suppress=True)
print(standardized_inputs)
Vector Normalization
While standardization operates column-wise, vector normalization scales individual samples (rows) to have a unit norm. This approach is particularly useful for text classification, clustering, and neural networks where the direction of the feature vector matters more than its absolute magnitude. By default, it applies L2 normalization.
import pandas as pd
import numpy as np
from sklearn.preprocessing import Normalizer
df = pd.read_csv('dataset.csv', header=None)
X_raw = df.iloc[:, :-1].values
y_raw = df.iloc[:, -1].values
# Normalize each sample to unit length
row_normalizer = Normalizer(norm='l2')
normalized_X = row_normalizer.fit_transform(X_raw)
np.set_printoptions(precision=3, suppress=True)
print(normalized_X)
Feature Binarization
Converting continuous variables into binary flags can simplify models or meet specific algorithmic requirements. Binarization applies a hard threshold: values above the limit become 1, while those at or below it become 0. This technique is frequently applied to probability outputs or when creating indicator featurse from continuous measurements.
import pandas as pd
import numpy as np
from sklearn.preprocessing import Binarizer
source_df = pd.read_csv('dataset.csv', header=None)
predictors = source_df.iloc[:, :-1].values
target = source_df.iloc[:, -1].values
# Convert features to 0 or 1 based on a threshold
threshold_converter = Binarizer(threshold=0.0)
binary_predictors = threshold_converter.fit_transform(predictors)
np.set_printoptions(precision=3, suppress=True)
print(binary_predictors)