ছোট প্রজেক্ট: টাইটানিক বিশ্লেষণ
এই পাঠে যা শিখবেন
- Real-world dataset-এর প্রথম exploration
- Missing value diagnosis ও handling strategy
- Categorical encoding — ordinal, one-hot
- Feature engineering — Title from Name, FamilySize
- ColumnTransformer + Pipeline — production-grade preprocessing
- Cross-validation দিয়ে model compare
- Confusion matrix, precision/recall — beyond accuracy
- Result interpretation — feature importance ও social context
১ · Titanic dataset কেন
Titanic datasetTitanic datasetRMS Titanic-এর ১৯১২-র দুর্ঘটনার passenger record। ২২২৩ জনের মধ্যে ১৫০২ মৃত্যু — বাস্তব ঐতিহাসিক ঘটনা। Kaggle-এর "Hello World" competition (২০১২+) — ১৫ লক্ষ+ submission। ML community-র shared touchstone। = Iris-এর পরবর্তী natural step। ছোট কিন্তু বাস্তব। Sex, age, class, fare, port, family — মানুষের decision-making-এর data। Iris-এ পরিষ্কার সংখ্যা; Titanic-এ ভাঙা world। Missing value, categorical mess, social pattern — সবই production reality-র মতো।
Survived — target (০ = মারা গেছেন, ১ = বেঁচেছেন)
Pclass — ticket class ১/২/৩
Sex — male/female
Age — বয়স (~২০% missing)
SibSp — sibling/spouse onboard
Parch — parent/child onboard
Fare — ticket price
Embarked — port (C/Q/S)
Cabin — cabin number (~৭৭% missing)
Name, Ticket — string id
২ · Step 1 — Load & first look
import seaborn as sns
import pandas as pd
# Seaborn-এ built-in (Kaggle CSV-র প্রায় same subset)
df = sns.load_dataset("titanic")
print("Shape:", df.shape)
print("\nColumns:", list(df.columns))
print("\nFirst 5 rows:")
print(df.head())
print("\nDtypes:")
print(df.dtypes)
৩ · Step 2 — missing pattern diagnosis
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("titanic")
# Missing % per column
miss = df.isna().mean().sort_values(ascending=False) * 100
print("Missing % per column:")
print(miss.round(1))
# Visualize
plt.figure(figsize=(8, 4))
sns.heatmap(df.isna(), cbar=False, yticklabels=False, cmap="viridis")
plt.title("Missing value pattern (yellow = missing)")
plt.tight_layout()
plt.show()
deck ~৭৭% missing — সম্ভবত drop। age ~২০% — impute। embark_town ২ row — mode imputation যথেষ্ট। Strategy column-অনুযায়ী।
৪ · Step 3 — survival rate এ EDA
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
df = sns.load_dataset("titanic")
# Overall survival rate
print(f"Overall survival: {df['survived'].mean():.2%}")
# By sex
print("\nBy sex:")
print(df.groupby("sex")["survived"].agg(["mean", "count"]).round(3))
# By class
print("\nBy class:")
print(df.groupby("pclass")["survived"].agg(["mean", "count"]).round(3))
# Visualize jointly
fig, axes = plt.subplots(1, 3, figsize=(14, 4))
sns.barplot(data=df, x="sex", y="survived", ax=axes[0], errorbar=None)
axes[0].set_title("Survival by Sex")
axes[0].set_ylim(0, 1)
sns.barplot(data=df, x="pclass", y="survived", ax=axes[1], errorbar=None)
axes[1].set_title("Survival by Class")
axes[1].set_ylim(0, 1)
sns.barplot(data=df, x="pclass", y="survived", hue="sex",
ax=axes[2], errorbar=None)
axes[2].set_title("Survival — Class × Sex")
axes[2].set_ylim(0, 1)
plt.tight_layout()
plt.show()
৫ · Step 4 — age ও fare distribution
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("titanic").dropna(subset=["age", "fare"])
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
# Age distribution by survival
sns.kdeplot(data=df, x="age", hue="survived",
fill=True, alpha=0.4, ax=axes[0])
axes[0].set_title("Age — survived vs not")
# Fare — log-scale (very skewed)
sns.boxplot(data=df, x="pclass", y="fare", hue="survived", ax=axes[1])
axes[1].set_yscale("log")
axes[1].set_title("Fare — by class & survival")
plt.tight_layout()
plt.show()
print(f"\nMean age (survived): {df.query('survived==1').age.mean():.1f}")
print(f"Mean age (not survived): {df.query('survived==0').age.mean():.1f}")
৬ · Step 5 — feature engineering
Raw column থেকে নতুন feature বানানো — প্রায়ই accuracy boost। Titanic-এর famous এক ফিচার — Name থেকে Title (Mr./Mrs./Miss./Master.) extract।
import seaborn as sns
import pandas as pd
df = sns.load_dataset("titanic").copy()
# Family size
df["family_size"] = df["sibsp"] + df["parch"] + 1
df["is_alone"] = (df["family_size"] == 1).astype(int)
# Age bin (নতুন column)
df["age_group"] = pd.cut(df["age"],
bins=[0, 12, 18, 35, 60, 80],
labels=["child", "teen", "adult", "middle", "senior"])
# Fare bin (quantile-based)
df["fare_q"] = pd.qcut(df["fare"], q=4, labels=["low", "mid", "high", "vhigh"])
print(df[["family_size", "is_alone", "age_group", "fare_q", "survived"]].head(8))
# নতুন feature-এর মান
print("\nSurvival by family_size:")
print(df.groupby("family_size")["survived"].mean().round(2))
print("\nSurvival by is_alone:")
print(df.groupby("is_alone")["survived"].mean().round(2))
sibsp + parch-এ ধরা পড়ে না — feature engineering দিয়ে surfaced।
৭ · Step 6 — ColumnTransformer + Pipeline
Mixed type — numeric, categorical, ordinal — একসাথে handle করার জন্য ColumnTransformer। Pipeline-এর সাথে — leak-proof, single object।
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score, train_test_split
df = sns.load_dataset("titanic").drop(columns=["deck", "alive", "alone",
"class", "who", "embark_town"])
# Feature engineering
df["family_size"] = df["sibsp"] + df["parch"] + 1
df["is_alone"] = (df["family_size"] == 1).astype(int)
X = df.drop(columns=["survived"])
y = df["survived"]
# Column groups
num_cols = ["age", "fare", "family_size"]
cat_cols = ["sex", "embarked", "pclass"]
bin_cols = ["is_alone", "sibsp", "parch"]
# Numeric pipeline — impute → scale
num_pipe = Pipeline([
("impute", SimpleImputer(strategy="median")),
("scale", StandardScaler()),
])
# Categorical pipeline — impute mode → one-hot
cat_pipe = Pipeline([
("impute", SimpleImputer(strategy="most_frequent")),
("ohe", OneHotEncoder(handle_unknown="ignore", drop="first")),
])
preprocess = ColumnTransformer([
("num", num_pipe, num_cols),
("cat", cat_pipe, cat_cols),
("bin", "passthrough", bin_cols),
])
# Final pipeline — preprocess + classifier
clf = Pipeline([
("prep", preprocess),
("model", LogisticRegression(max_iter=1000)),
])
scores = cross_val_score(clf, X, y, cv=5, scoring="accuracy")
print(f"LogReg 5-fold accuracy: {scores.mean():.3f} ± {scores.std():.3f}")
# এক ক্লিকে algorithm switch
clf.set_params(model=RandomForestClassifier(n_estimators=200, random_state=42))
scores_rf = cross_val_score(clf, X, y, cv=5, scoring="accuracy")
print(f"RF 5-fold accuracy: {scores_rf.mean():.3f} ± {scores_rf.std():.3f}")
৮ · Step 7 — confusion matrix ও metric
Accuracy একটাই metric নয়। Confusion matrix-এ false positive / false negative দেখুন। Imbalanced dataset-এ — F1, precision, recall, AUC গুরুত্বপূর্ণ।
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import (classification_report, confusion_matrix,
ConfusionMatrixDisplay, roc_auc_score)
df = sns.load_dataset("titanic").drop(columns=["deck", "alive", "alone",
"class", "who", "embark_town"])
df["family_size"] = df["sibsp"] + df["parch"] + 1
df["is_alone"] = (df["family_size"] == 1).astype(int)
X = df.drop(columns=["survived"]); y = df["survived"]
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2,
stratify=y, random_state=42)
num_cols = ["age", "fare", "family_size"]
cat_cols = ["sex", "embarked", "pclass"]
bin_cols = ["is_alone", "sibsp", "parch"]
prep = ColumnTransformer([
("num", Pipeline([("imp", SimpleImputer(strategy="median")),
("sc", StandardScaler())]), num_cols),
("cat", Pipeline([("imp", SimpleImputer(strategy="most_frequent")),
("ohe", OneHotEncoder(handle_unknown="ignore",
drop="first"))]), cat_cols),
("bin", "passthrough", bin_cols),
])
model = Pipeline([("prep", prep),
("rf", RandomForestClassifier(n_estimators=200,
random_state=42))])
model.fit(X_tr, y_tr)
y_pred = model.predict(X_te)
y_proba = model.predict_proba(X_te)[:, 1]
print(classification_report(y_te, y_pred, target_names=["died", "survived"]))
print(f"ROC AUC: {roc_auc_score(y_te, y_proba):.3f}")
# Confusion matrix
fig, ax = plt.subplots(figsize=(5, 4))
ConfusionMatrixDisplay.from_predictions(
y_te, y_pred, display_labels=["died", "survived"], ax=ax,
cmap="Blues", colorbar=False
)
ax.set_title("Titanic — confusion matrix")
plt.tight_layout()
plt.show()
৯ · Feature importance — সিদ্ধান্ত ব্যাখ্যা
import pandas as pd
# Above-এর fitted model থেকে
prep = model.named_steps["prep"]
rf = model.named_steps["rf"]
# Feature names after one-hot encoding
ohe_cols = prep.named_transformers_["cat"]\
.named_steps["ohe"].get_feature_names_out(cat_cols)
feat_names = num_cols + list(ohe_cols) + bin_cols
importance = pd.Series(rf.feature_importances_, index=feat_names)\
.sort_values(ascending=False)
print("Top features:")
print(importance.head(10).round(3))
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ Missing value handling — drop, mean impute, median impute, KNN impute, MICE, indicator column। ছয়টি strategy-র pros/cons। Titanic-এর Age ২০% missing — কোনটা বেছে নেবেন এবং কেন?
Missing data — production ML-এর সবচেয়ে কপট সমস্যা। Hadley Wickham-এর famous quip: "Real data is messy. Most missing data is missing for a reason।" পদ্ধতি বাছার আগে — কেন missing, সেই বুঝতে হয়।
Missing-এর তিন mechanism:
- MCAR (Missing Completely At Random): random — sensor failure। Drop OK।
- MAR (Missing At Random): অন্য variable-এর function। Pclass=3 → Age recording sloppy। Sophisticated impute helps।
- MNAR (Missing Not At Random): missing reason target-related। "Income missing because high earners don't disclose"। Hardest — needs domain knowledge।
৬টি strategy:
(১) Drop rows with NaN:
- Pros: simplest, no assumption।
- Cons: data loss huge — 10% missing in 5 columns can drop 40% rows।
- Use: very small fraction (< 5%), MCAR, large dataset।
(২) Drop column:
- Pros: simple, no impute risk।
- Cons: lose potentially predictive information।
- Use: column > 50% missing (Titanic deck 77%)।
(৩) Mean / median impute:
- Pros: simple, fast, no information loss।
- Cons: distribution shrinks (variance underestimate); doesn't capture structure।
- Use: numerical, < 10% missing, MCAR; median for skewed (Fare)।
(৪) Mode impute (categorical):
- Pros: simple।
- Cons: bias toward majority।
- Use: categorical, low missing rate।
(৫) KNN imputer:
- Pros: uses inter-row similarity; captures structure।
- Cons: slow on big data; needs full feature scaling first; sensitive to k।
- Use: medium dataset, structured missing pattern।
(৬) MICE / IterativeImputer:
- Pros: each missing column predicted from others; iterative refine; multiple imputation possible।
- Cons: slow; assumption-heavy; can be unstable।
- Use: research-grade analysis; many missing columns interrelated।
(৭) Bonus — indicator column:
- Add binary "was_missing" column + impute value।
- Lets model learn from missing pattern itself।
SimpleImputer(add_indicator=True)- Use: MNAR suspected; missing-ness itself predictive।
Titanic Age — recommendation:
- ২০% missing — too high to drop rows।
- MAR-likely: Pclass=3 has higher missing rate। Class-conditional median better than overall median।
- Better: Title-based imputation। "Master" → median age 5; "Mr" → median 30।
- Best simple approach:
df.groupby(["pclass", "sex"])["age"].transform(lambda x: x.fillna(x.median())) - Plus: add
age_was_missingindicator column।
Production rules of thumb:
- < 5% missing, MCAR → drop rows।
- 5-30% → impute (median for numeric, mode for cat)।
- 30-60% → impute + indicator।
- > 60% → drop column (or use as binary indicator)।
- Domain knowledge উপরে — group-conditional impute।
Bias warning:
- Mean impute regression coefficient toward 0 push।
- "Fair imputation" — simple model better behavior।
- Imputation cv split-এর আগে করলে — leak। Pipeline-এ রাখুন।
Modern alternatives:
- Missing-aware models — XGBoost, LightGBM native handle।
- Deep learning — masked language model-style, learn-during-train।
- Multiple imputation — ensemble of imputed datasets, statistical correctness।
মূল উপলব্ধি: Missing handling = data-র স্টোরিতে ফাঁক ভরাট — কিন্তু কী দিয়ে ভরাট, সিদ্ধান্ত matters। Mean impute সরল কিন্তু lazy। Group-conditional + indicator = production sweet spot। Mature ML practice — mechanism understand, strategy match।
প্র ০২ Categorical encoding — one-hot, label, ordinal, target, frequency, hash, learned embedding। ৭ পদ্ধতি কখন কোনটা? High cardinality (zip code 33000 unique value) — কী strategy?
ML model নিতে চায় number; data-তে আছে "Dhaka", "Chittagong"। Encoding বাছাই — যা accuracy ১০% এদিক-ওদিক করতে পারে। Subtle, important।
৭টি কেন:
(১) One-hot encoding:
- প্রতিটি unique value → একটা binary column।
- Pros: ordering assumption নেই; linear model + tree উভয়ে কাজ করে।
- Cons: cardinality বেশি → column explosion (10K cardinality → 10K column)।
- Use: low cardinality (< 10-20 unique values)।
sklearn.preprocessing.OneHotEncoder
(২) Label encoding (ordinal):
- প্রতিটি category → একটা integer (0, 1, 2, ...)।
- Pros: simple, no column explosion।
- Cons: artificial ordering — "Chittagong=0, Dhaka=1, Sylhet=2" — model ভাবে Dhaka=Sylhet/2 + Chittagong/2।
- Use: tree-based model only; OR truly ordinal (low/medium/high)।
(৩) Ordinal encoding (true ordinal):
- Order matters: education = ["primary", "secondary", "tertiary"] → 0, 1, 2।
- Pros: preserves order semantics।
- Cons: needs domain-defined ranking।
- Use: order-meaningful categorical (size, education level, ranking)।
(৪) Target encoding (mean encoding):
- প্রতিটি category → that category-এর target mean।
- Pros: high cardinality handle, model বুঝে।
- Cons: leakage prone — must be out-of-fold; rare category overfit।
- Use: high cardinality + tree models।
category_encoders.TargetEncoder - Smoothing add: bayesian shrink toward global mean।
(৫) Frequency / count encoding:
- প্রতিটি category → that category-এর count।
- Pros: simple, leak-free, interesting signal।
- Cons: loses categorical identity; ties (same count) treated same।
- Use: high cardinality, count itself meaningful।
(৬) Hash encoding:
- Hash function → fixed-width vector।
- Pros: any cardinality, fixed memory; out-of-vocabulary handled।
- Cons: collision; not interpretable।
- Use: massive cardinality (URLs, user IDs), online learning।
sklearn.feature_extraction.FeatureHasher
(৭) Learned embedding (NN):
- প্রতিটি category → trained low-dim vector।
- Pros: best for very high cardinality; captures similarity।
- Cons: needs neural network setup; data-hungry।
- Use: production NN models (recommendation, NLP)।
- Entity Embedding (Cheng & Schubert, ২০১৬) — Kaggle Rossmann winning approach।
Cardinality-based decision:
- 2-10 unique → one-hot।
- 10-100 → one-hot or target encoding।
- 100-1000 → target encoding + smoothing।
- 1000-100K → frequency, hash, or embedding।
- 100K+ → hash or embedding (NN)।
Zipcode case (33K unique):
- One-hot impossible (33K column)।
- Target encoding good baseline; out-of-fold mandatory।
- Hash encoding (e.g., 50 dim) — production-grade।
- Better: aggregate to county/district level — domain knowledge।
- Best: latitude/longitude lookup → continuous numeric features।
- NN with embedding → state-of-the-art।
Caveats:
- Tree models (RandomForest, XGBoost) tolerate label-encoded। Linear models cannot।
- Test set new category — must handle (one-hot
handle_unknown="ignore")। - Target leakage — encode in cv-fold-aware manner।
- Imbalanced category — rare value rolled to "other" বা smoothed।
Production reality:
- e-commerce: product_id (millions) → embedding।
- Ad-tech: domain (millions) → hash encoding।
- Banking: branch_code (1000s) → target encoding।
- Healthcare: ICD code → ontology-based encoding।
- NLP: word → BPE / WordPiece subword।
মূল উপলব্ধি: Encoding choice = model design choice। Wrong choice = ১০% accuracy loss easily। Cardinality + model type → strategy। Beginners default to one-hot — works but not always optimal। Production maturity = matching encoding to data & model।
প্র ০৩ Titanic-এর "Sex" sex-based survival difference — model এই pattern শিখে। Real-world deployment-এ এটা কি acceptable? Algorithmic fairness, disparate impact, GDPR — এই ML decision-এ কতটুকু apply?
Titanic একটি historical disaster — model "sex" দেখে predict করে কারণ historical reality এমনই ছিল। কিন্তু production ML-এ — gender, race, religion-based decision-এ আজ অনেক regulation, ethical concern, technical mitigation।
Titanic-এ "Sex" feature — historic context:
- "Women and children first" — Edwardian protocol; lifeboats female-prioritized।
- Class privilege — first class lifeboat access বেশি।
- Data accurately reflects historical pattern।
- Model learns to predict — not enforce — that pattern।
কিন্তু analogous production scenario-এ:
- Loan approval — historical bias against women, minorities।
- Hiring algorithm — Amazon scrapped 2018 algorithm because biased against female candidates।
- Healthcare — algorithm prioritizing white patients (Obermeyer ২০১৯)।
- Predictive policing — racial bias in arrests data → biased model।
- Insurance pricing — gender-based pricing illegal in EU since 2012।
Algorithmic fairness — তিন definition:
- Demographic parity: P(predict positive | group A) = P(predict positive | group B)। Same approval rate across groups। কিন্তু ground truth ভিন্ন হলে — accuracy হারায়।
- Equal opportunity: True positive rate same across groups। Qualified people equal chance।
- Equalized odds: TPR ও FPR same। সবচেয়ে strict; প্রায়ই অসম্ভব mathematically (Chouldechova ২০১৭)।
Disparate impact (legal concept):
- 4/5 rule: protected group-এর positive rate < 80% of dominant group → disparate impact।
- US Civil Rights Act, EU Race Equality Directive।
- Lawsuit-প্রবণ। Tech company-গুলো এই metric monitor করে।
GDPR (EU, ২০১৮):
- Article 22: "right not to be subject to a decision based solely on automated processing"।
- "Right to explanation" — debated; arguably implicit।
- Sensitive category (race, religion, health) — explicit consent।
- Bangladesh Data Protection Act ২০২৩ — similar provisions।
Bias mitigation strategies:
(১) Pre-processing:
- Re-balance sample — oversample minority।
- Reweight data — fairness-aware loss।
- Remove protected feature (sex)। কিন্তু proxy feature থাকে — Title, occupation।
(২) In-processing:
- Adversarial debiasing — predict target, but not group।
- Constrained optimization — fairness as constraint।
- Multi-objective loss — accuracy + fairness।
(৩) Post-processing:
- Threshold per group adjust — same TPR।
- Reject option — uncertain predictions human review।
(৪) Audit-driven:
- SHAP — feature attribution per individual।
- What-If Tool, Aequitas, Fairlearn — fairness audit toolkit।
- Regular bias monitoring in production।
Titanic-specific reflection:
- Educational use — fine, transparently teach।
- Discuss bias openly with student।
- Compare: "remove sex feature" → accuracy drop ৭% — but ethically robust।
- Real-world analogy → loan approval discussion।
Bangladesh context:
- RAJUK, banking — gender-blind decisions legally required।
- Microfinance — counterintuitively, gender-aware (women better repayment)।
- Health — gender-specific clinical decision OK।
- Hiring algorithm — emerging concern।
Beyond fairness — wider ethics:
- Privacy: data collection consent।
- Transparency: model explainability।
- Accountability: who responsible for model decision?
- Sustainability: compute cost, model lifecycle।
- Misuse: dual-use technology (face recognition)।
Practitioner checklist:
- Identify protected attributes in data।
- Audit baseline model for disparate impact।
- Quantify fairness vs accuracy tradeoff।
- Document decision in model card।
- Monitor production metrics by subgroup।
- Establish human-in-loop for edge cases।
- Stakeholder engagement — affected community input।
মূল উপলব্ধি: ML model = data-র mirror। Biased data = biased model। Education-এ Titanic-এর "sex" feature transparent learning material। Production-এ — same logic = legal liability + social harm। ২০২৬-এ — "build accurate model" সর্বোত্তম standard নয়; "build fair, accurate, accountable model" সরকারি ও corporate দু'জায়গায় expectation। Mature ML practitioner — দু'টোই balance করেন।
প্র ০৪ Kaggle Titanic leaderboard top ~০.৮৫। ০.৮২ থেকে ০.৮৫-এ যেতে কোন techniques? Stacking, hyperparameter tune, feature engineering — কোনটা সবচেয়ে productive? এই incremental gain real-world-এ matter করে?
Kaggle Titanic — ১২ বছরের oldest competition, ১৫ লক্ষ submission। Top ০.৮৫-০.৮৭ (with overfitting on public leaderboard); honest cv ~০.৮৪। ০.৮২ থেকে ০.৮৫ — কী step?
Productive ladder (effort to gain ratio):
(১) Feature engineering — biggest gain:
- Title from Name: Mr/Mrs/Miss/Master + rare. Master = boy → high survival। +১-২%।
- Age × Pclass interaction: first-class child very high survival। +0.5%।
- Family survival rate: same surname-এর অন্যজন বাঁচলে — likely বাঁচবে (group dynamics)। +১-২%।
- Ticket prefix: ticket sharing pattern → group। +0.5%।
- Fare per person: fare / family_size — true individual cost।
- Cabin letter (when available): deck letter → location proximity to lifeboat।
(২) Imputation sophistication:
- Median age → group-wise median (Title × Pclass)। +0.5%।
- Iterative imputer। +0.2%।
- Missing indicator — pattern itself signal।
(৩) Algorithm switch:
- Logistic ০.৮০ → RandomForest ০.৮২ → XGBoost ০.৮৩ → LightGBM ০.৮৪।
- +০.৫-১% per upgrade।
- GBDT often best for tabular।
(৪) Hyperparameter tuning:
- RandomForest: n_estimators 100→500, max_depth 10→8, min_samples_leaf optimize।
- XGBoost: learning_rate, max_depth, subsample, colsample_bytree।
- +0.3-0.7% on top of default।
- Optuna/Bayesian — efficient।
(৫) Ensembling / stacking:
- Simple voting: LogReg + RF + XGB → +0.3%।
- Stacking: model predictions as feature for meta-learner। +0.5-1%।
- Blending — weighted average।
- Production cost: 3x inference time।
(৬) Cross-validation strategy:
- Stratified K-Fold mandatory।
- Repeated K-Fold — robust estimate।
- Group-aware (family) — leak prevent।
(৭) Threshold tuning:
- Default 0.5 — not optimal for imbalanced।
- F1-maximizing threshold — +0.5%।
- Cost-sensitive threshold — business case-specific।
Effort/return ranking:
- Title feature engineering — 30 min, +1.5%। Biggest bang for buck।
- XGBoost/LightGBM switch — 10 min, +1%।
- Hyperparameter tune — 1-2 hour, +0.5-1%।
- Ensembling — 1 hour, +0.5%।
- Sophisticated imputation — 30 min, +0.3%।
- Threshold optimization — 15 min, +0.3%।
Real-world relevance — incremental gain matter?
YES, when:
- High volume: Recommendation system, ad CTR — 0.1% lift = millions in revenue।
- Critical decision: medical diagnosis — 1% better accuracy = lives saved।
- Competitive market: Kaggle bronze/silver/gold-এ 0.001 difference।
- Regulatory benchmarks: credit scoring — must beat baseline।
NO, when:
- Already shipping decision: 95% vs 96% — same business action।
- Calibration matters more: probability quality > raw accuracy।
- Latency critical: 10ms vs 100ms inference — simple model wins।
- Data quality limit: noise floor reached — gain illusory।
- Maintenance cost: stacked ensemble maintenance heavy।
Pareto frontier reality:
- 0 → 80% accuracy: 1 day, simple baseline।
- 80 → 85%: 1 week, feature eng + GBDT।
- 85 → 90%: 1 month, ensembling + tuning + custom feature।
- 90 → 92%: 6 months, deep learning + transfer learning।
- 92 → 93%: years of research।
Diminishing returns — when to stop:
- Business impact per percent gain।
- Maintenance cost per percent gain।
- Data noise floor estimate (Bayes error)।
- Stakeholder priority — accuracy vs interpretability vs latency।
Production wisdom:
- "80% accuracy in 2 weeks beats 85% in 6 months" — most cases।
- Iterate: ship baseline → user feedback → improve।
- Data > model: more/better data > complex model।
- Maintainability > accuracy: simple model survives team changes।
মূল উপলব্ধি: Kaggle leaderboard chase = sport, real-world = engineering। Both have value। Junior — incremental percent chase শেখান, baseline build শেখান। Senior — ১% gain-এর business value justify, maintenance cost weigh, simple ship। Best practice = right level of complexity for problem। Titanic-এ ০.৮২ — production-ready; ০.৮৫ — competition-worthy। Difference matters when stakes match।
অনুশীলন
-
Title feature: seaborn-এর Titanic dataset-এ Name column নেই, কিন্তু আপনি Kaggle CSV download করতে পারেন। Name থেকে Title (Mr/Mrs/Miss/Master) extract করে accuracy boost test করুন।
import pandas as pd # Kaggle Titanic CSV — https://www.kaggle.com/c/titanic/data df = pd.read_csv("train.csv") # Title extract — regex df["Title"] = df["Name"].str.extract(r" ([A-Za-z]+)\.", expand=False) # Rare title-গুলো একসাথে common = ["Mr", "Mrs", "Miss", "Master"] df["Title"] = df["Title"].where(df["Title"].isin(common), "Rare") print(df.groupby("Title")["Survived"].agg(["mean", "count"])) # Master (boys) survival ~0.58 — vs Mr ~0.16! -
Group-conditional age impute: Age missing-এ Title × Pclass-ভিত্তিক median impute করুন।
df["Age"] = df.groupby(["Title", "Pclass"])["Age"]\ .transform(lambda x: x.fillna(x.median())) # Verify print("Missing after impute:", df["Age"].isna().sum()) print(df.groupby(["Title", "Pclass"])["Age"].median().round(1)) -
Compare classifiers: Pipeline-এ LogReg, RandomForest, GradientBoostingClassifier — ৩টি model-এর 5-fold CV accuracy compare করুন।
from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier from sklearn.model_selection import cross_val_score # Above-এর preprocess pipeline reuse models = { "LogReg": LogisticRegression(max_iter=1000), "RF": RandomForestClassifier(n_estimators=200, random_state=42), "GB": GradientBoostingClassifier(random_state=42), } for name, mdl in models.items(): clf = Pipeline([("prep", preprocess), ("model", mdl)]) scores = cross_val_score(clf, X, y, cv=5, scoring="accuracy", n_jobs=-1) print(f"{name:8s} {scores.mean():.3f} ± {scores.std():.3f}")সাধারণত GradientBoosting সবচেয়ে ভাল — এমনকি default settings-এ।
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ২৫ · কোর্সের চূড়ান্ত পর্যালোচনা পরবর্তী পাঠ পুরো কোর্স recap + পরবর্তী step।
- পাঠ ২৩ · Git ও GitHub-এ AI প্রজেক্ট আগের পাঠ এই project Git-এ commit করুন — portfolio entry।
- পাঠ ২০ · scikit-learn-এর সাথে পরিচয় এই পাঠের সাথে সম্পর্কিত sklearn API — এই project-এর foundation।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।