Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 5 of 10
Tabular, Text & Sensor Data Modalities in Production
Machine learning workflows differ fundamentally based on data modality. Tabular data (relational DB records, customer transactions) features heterogeneous column types and complex feature correlations, where Tree Ensembles (XGBoost, LightGBM) dominate. Unstructured text, audio, and sensor time-series require tokenization, positional embeddings, frequency transforms, and deep neural architectures. Understanding feature representations across modalities is vital for multi-modal systems.
Heterogeneous Feature Types & Modality Processing
import pandas as pd
import numpy as np
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.feature_extraction.text import TfidfVectorizer
# Multi-modal enterprise transaction record
data = pd.DataFrame({
# Numerical Continuous
'account_balance': [1520.50, 480.00, 29000.10, 110.00],
'transaction_amount': [45.00, 1200.00, 35.50, 890.00],
# Categorical Nominal
'device_type': ['ios', 'android', 'web', 'android'],
'payment_network': ['visa', 'mastercard', 'amex', 'visa'],
# Unstructured Text Field
'user_support_note': [
"Card was used while traveling abroad in France",
"Urgent: unauthorized online merchant charge",
"Routine subscription renewal verified by user",
"Failed transaction retry after insufficient balance"
]
})
# Construct a unified ColumnTransformer for multi-modal feature matrices
preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), ['account_balance', 'transaction_amount']),
('cat', OneHotEncoder(drop='first', sparse_output=False), ['device_type', 'payment_network']),
('text', TfidfVectorizer(max_features=10), 'user_support_note'),
],
remainder='drop'
)
feature_matrix = preprocessor.fit_transform(data)
feature_names = preprocessor.get_feature_names_out()
print(f"Transformed matrix shape: {feature_matrix.shape}")
print("Feature columns generated:")
for name in feature_names[:6]:
print(f" -> {name}")Pro Tip: When to Use GBDTs vs Deep Learning on Tabular Data
Extensive empirical benchmarks consistently confirm that Gradient Boosted Decision Trees (XGBoost, LightGBM, CatBoost) outperform Deep Neural Networks (MLPs, TabNet, FT-Transformer) on raw tabular data in training speed, hyperparameter robustness, and predictive accuracy. Reserve neural networks for unstructured text, audio, vision, or graph-structured interactions.
MlDataTypesDatasets Visual Representation
Hands-On Challenge: Multi-Modal Fraud Embeddings
Extend the multi-modal ColumnTransformer above by extracting temporal cyclical features from a datetime column (`sin_hour = sin(2*pi*hour/24)`, `cos_hour = cos(2*pi*hour/24)`) and combining them with the one-hot categorical vectors.
Quick Quiz & Key Concept Checks
Common Pitfall: Dropping Rare Categories Indiscriminately
Filtering out categories with low frequencies can inadvertently discard your most valuable predictive signal. In fraud detection or cyber threat hunting, anomalous behavior frequently originates from previously unseen or rare user agents and IP subnets. Group rare categories into an explicit `__rare__` token rather than deleting them.
Finished this lesson?