python 22 lines · 1 tab

Scaling and normalization choices for different model families

1 tab
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
from sklearn.linear_model import LogisticRegression

standard_pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('model', LogisticRegression(max_iter=1000)),
])

robust_pipeline = Pipeline([
    ('scaler', RobustScaler()),
    ('model', LogisticRegression(max_iter=1000)),
])

minmax_pipeline = Pipeline([
    ('scaler', MinMaxScaler()),
    ('model', LogisticRegression(max_iter=1000)),
])

print(standard_pipeline)
print(robust_pipeline)
print(minmax_pipeline)
1 file · python Explain with highlit

Not every model cares about scale, but enough of them do that I keep scaling explicit. Linear models, SVMs, neural nets, and distance-based methods all benefit from well-behaved inputs. I prefer putting scalers inside the pipeline so train and inference paths cannot drift apart.


Related snips

ruby
module EmailNormalization
  extend ActiveSupport::Concern

  included do
    attr_accessor :soft_warnings

Soft Validation: Normalize + Validate Email

rails activerecord validations
by codesnips 4 tabs
ruby
class CreateTags < ActiveRecord::Migration[7.0]
  def change
    create_table :tags do |t|
      t.string :name, null: false
      t.integer :taggings_count, null: false, default: 0
      t.timestamps

Normalize Tags at Write Time

rails activerecord callbacks
by codesnips 3 tabs
python
import pandas as pd

df = pd.read_parquet('churn_training.parquet')

print('shape:', df.shape)
print('target balance:', df['churned'].value_counts(normalize=True).round(3))

Exploratory data analysis checklist for tabular ML projects

eda machine-learning tabular-data
by Dr. Elena Vasquez 1 tab
go
package workpool

import (
	"context"
	"sync"
)

Bounded Worker Pool Processing Jobs from a Buffered Channel in Go

go concurrency worker-pool
by codesnips 3 tabs
python
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

ColumnTransformer pipelines that keep preprocessing honest

scikit-learn pipelines columntransformer
by Dr. Elena Vasquez 1 tab
python
from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List

Parsing a Nested JSON Feed into Normalized Dataclasses in Python

dataclasses json parsing
by codesnips 3 tabs

Share this code

Here's the card — post it anywhere.

Scaling and normalization choices for different model families — share card
Link copied