python
23 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.ensemble import RandomForestClassifier
preprocessor = ColumnTransformer([
('num', Pipeline([
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler()),
]), ['age', 'income', 'days_since_last_login']),
('cat', Pipeline([
('imputer', SimpleImputer(strategy='most_frequent')),
('encoder', OneHotEncoder(handle_unknown='ignore')),
]), ['country', 'plan_tier']),
('text', TfidfVectorizer(max_features=5000, ngram_range=(1, 2)), 'support_ticket_text'),
])
model = Pipeline([
('preprocessor', preprocessor),
('classifier', RandomForestClassifier(n_estimators=300, random_state=42, n_jobs=-1)),
])
1 file · python
Explain with highlit
I push nearly all preprocessing into a Pipeline so training and inference paths share exactly the same logic. ColumnTransformer is the workhorse here because real-world tables mix numeric, categorical, boolean, and text fields. It gives you reproducibility without having to manage fragile pre-fit artifacts by hand.
Related snips
python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
from sklearn.linear_model import LogisticRegression
standard_pipeline = Pipeline([
('scaler', StandardScaler()),
Scaling and normalization choices for different model families
feature-scaling
normalization
machine-learning
by Dr. Elena Vasquez
1 tab
python
import cv2
image = cv2.imread('receipt.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresholded = cv2.adaptiveThreshold(
OpenCV image preprocessing for OCR and vision pipelines
opencv
image-processing
computer-vision
by Dr. Elena Vasquez
1 tab
python
import pandas as pd
df = pd.read_csv('customers.csv')
df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_', regex=False)
Cleaning missing values and normalizing messy CSV exports
pandas
data-cleaning
missing-values
by Dr. Elena Vasquez
1 tab
python
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_absolute_error, root_mean_squared_error
models = {
'linear': LinearRegression(),
'ridge': Ridge(alpha=1.0),
Regression workflows with linear, ridge, lasso, and elastic net
scikit-learn
regression
ridge
by Dr. Elena Vasquez
1 tab
python
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
Encoding categorical variables without creating leakage
categorical-encoding
preprocessing
scikit-learn
by Dr. Elena Vasquez
1 tab
python
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
grid_search = GridSearchCV(
estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
param_grid={
Hyperparameter tuning with GridSearchCV and randomized search
hyperparameter-tuning
gridsearch
scikit-learn
by Dr. Elena Vasquez
1 tab
Share this code
Here's the card — post it anywhere.