Hook — কাঁচা চাল বনাম রান্না করা ভাত
কাঁচা চাল model কে দিলে কিছুই হবে না। ধোয়া, ভেজানো, সিদ্ধ করা — এই process এর পর সেটা পুষ্টিকর ভাত হয়। Feature Engineering ঠিক সেই “রান্না” — raw data থেকে model-friendly feature বানানো।
শিল্পের সত্য
“Better features beat better models.” — Kaggle GM রা বারবার বলেন।
Concept — ৫টি কৌশল
- Encoding — categorical → numerical।
- Scaling — সব feature একই range এ।
- Transformation — log, sqrt, Box-Cox।
- Creation — নতুন feature derive করা।
- Selection — অপ্রয়োজনীয় feature বাদ।
Math — কিছু গুরুত্বপূর্ণ formula
Log Transform
x' = log(1 + x)
Skewed distribution (যেমন salary, price) কে normal এর কাছে আনে।
Target Encoding
encoded(category) = mean(target | category)
High-cardinality categorical এর জন্য শক্তিশালী — কিন্তু leakage এর ঝুঁকি আছে।
Feature Creation এর উদাহরণ
- Date থেকে — year, month, weekday, is_weekend।
- Name থেকে — title (Mr, Mrs, Dr) extract।
- Address থেকে — city, area, distance from center।
- Two feature কে combine — BMI = weight / height²।
- Aggregation — user এর গত ৭ দিনের গড় খরচ।
Code — Pipeline এ Feature Engineering
feature_eng.py
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import seaborn as sns
df = sns.load_dataset("titanic")[["pclass","sex","age","fare","embarked","survived"]].dropna()
# Feature creation
df["family_size"] = 0 # placeholder demo
df["fare_log"] = np.log1p(df["fare"])
df["is_child"] = (df["age"] < 12).astype(int)
X = df.drop("survived", axis=1)
y = df["survived"]
num_cols = ["age", "fare_log", "family_size"]
cat_cols = ["pclass", "sex", "embarked", "is_child"]
pre = ColumnTransformer([
("num", StandardScaler(), num_cols),
("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols),
])
pipe = Pipeline([("pre", pre), ("clf", LogisticRegression(max_iter=500))])
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)
pipe.fit(Xtr, ytr)
print("Accuracy:", pipe.score(Xte, yte))Common Mistakes
- Train/Test split এর আগে encoding/scaling — leakage।
- High-cardinality column কে one-hot করা — হাজার column হয়ে যায়।
- Target encoding sans regularization — overfit।
- Date কে string রাখা — pattern extract হয় না।
Practice Tasks
- Task 1: Fare column এর log transform এর আগে ও পরে histogram তুলনা করো।
- Task 2: Name column থেকে title extract করো (regex)।
- Task 3: family_size = sibsp + parch + 1 feature বানিয়ে accuracy তে effect দেখো।
Mini Project — Feature Engineering Battle
একই dataset এ — শুধু raw feature দিয়ে একটি model, আর engineered feature দিয়ে আরেকটি। দুটোর accuracy/F1 তুলনা করে blog post style এ writeup করো — কোন feature কতটা help করল।
Summary
এক নজরে
Feature Engineering = domain knowledge + creativity = model এর প্রকৃত power।