Machine Learning Preview: scikit-learn Basics
মেশিন লার্নিং প্রাইমার — scikit-learn
1. What Machine Learning Actually Is
Machine learning is the discipline of writing programs that improve at a task by being shown
examples, rather than by being told step-by-step rules. You give a dataset — inputs and correct outputs — and
a learning algorithm finds patterns that generalize to new, unseen inputs. scikit-learn is Python's most popular
ML library for classic (non-deep-learning) techniques. It uses the same fit/predict
API across dozens of algorithms.
fit/predict API সব অ্যালগরিদমে একই।
pip install scikit-learn pandas numpy. These code snippets are meant to run
locally or in Google Colab — the browser sandbox may not have scikit-learn installed.
2. The ML Workflow
- Get data (CSV, database, API).
- Split into train and test sets — never peek at the test set.
- Choose a model (linear regression, logistic regression, tree, forest...).
model.fit(X_train, y_train)— training.model.predict(X_test)— prediction.- Evaluate — accuracy, F1, RMSE, whatever is meaningful.
- Improve: better features, different model, tune hyperparameters.
3. First Model — Linear Regression
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import numpy as np
# Toy dataset: y ≈ 3x + 5 + noise
rng = np.random.default_rng(42)
X = rng.uniform(0, 10, size=(100, 1))
y = 3 * X.ravel() + 5 + rng.normal(0, 1, 100)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression().fit(X_train, y_train)
print("slope:", round(model.coef_[0], 3))
print("intercept:", round(model.intercept_, 3))
print("test R²:", round(model.score(X_test, y_test), 3))
4. Classification — Logistic Regression & Trees
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
X, y = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=7)
for name, model in [
("logreg", LogisticRegression(max_iter=500)),
("tree", DecisionTreeClassifier(random_state=7)),
("forest", RandomForestClassifier(n_estimators=50, random_state=7)),
]:
model.fit(X_tr, y_tr)
preds = model.predict(X_te)
print(f"{name}: acc = {accuracy_score(y_te, preds):.3f}")
5. Evaluation Metrics — Don't Trust Accuracy Alone
Accuracy is how often your model is right. But on imbalanced data (99% negative, 1% positive), a trivial "always predict negative" classifier gets 99% accuracy while being useless. Use precision, recall, and F1 for classification; MAE, RMSE, or R² for regression.
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 1, 0, 1, 0, 1, 1]
y_pred = [0, 1, 0, 0, 1, 1, 1, 1]
print("precision:", precision_score(y_true, y_pred))
print("recall:", recall_score(y_true, y_pred))
print("F1:", f1_score(y_true, y_pred))
6. Pipelines — Do It Right
Real ML has pre-processing: scaling, encoding categories, imputing missing values. Pipeline glues
the whole flow together, preventing data leakage (where info from the test set accidentally shapes the model).
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0)
pipe = Pipeline([
("scaler", StandardScaler()),
("model", LogisticRegression(max_iter=500)),
])
pipe.fit(X_tr, y_tr)
print("score:", pipe.score(X_te, y_te))
7. Vocabulary (শব্দভাণ্ডার)
| Term | Meaning | বাংলায় |
|---|---|---|
| Feature | An input column. | একটি input column। |
| Label / Target | The value to predict. | যে মান predict করতে হবে। |
| Overfitting | Memorizing training data; poor on test. | training data মুখস্থ — test-এ খারাপ। |
| Hyperparameter | A setting of the learning algorithm. | অ্যালগরিদমের একটি সেটিং। |
| Pipeline | Chain of pre-processing + model steps. | pre-processing + model-এর chain। |
8. Practice Problems
-
Load the Iris dataset; train a
RandomForestClassifier; print test accuracy.Iris dataset load করে RandomForest train করুন, test accuracy প্রিন্ট করুন।✨ Show Answer (উত্তর দেখুন)
ans1.pyfrom sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split X, y = load_iris(return_X_y=True) X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0) m = RandomForestClassifier(random_state=0).fit(X_tr, y_tr) print(m.score(X_te, y_te)) -
Explain in 2 sentences why you split data into train and test.২ বাক্যে বলুন: ডেটাকে train ও test-এ কেন ভাগ করা হয়।
✨ Show Answer (উত্তর দেখুন)
Answer: You split so the model's performance is measured on examples it has never seen — which is what matters in the real world. If you evaluate on the training data, a model that simply memorizes perfectly would look perfect, while having learned nothing that generalizes.
Train-এ train করে test-এ evaluate করলে বাস্তব-দুনিয়ার unseen data-র উপর performance বোঝা যায়। Training data-তে evaluate করলে একটি "মুখস্থকারী" model perfect দেখাবে, কিন্তু generalize কিছুই শেখেনি।
-
Write a
PipelinewithStandardScaler+LogisticRegression.Scaler + LogReg-এর Pipeline লিখুন।✨ Show Answer (উত্তর দেখুন)
ans3.pyfrom sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression pipe = Pipeline([ ("scale", StandardScaler()), ("clf", LogisticRegression(max_iter=500)), ]) print(pipe) -
On an imbalanced spam dataset (95% ham, 5% spam), which metric should you trust — accuracy or F1? Why?Imbalanced spam dataset-এ accuracy নাকি F1 — কোনটি বিশ্বাস করবেন? কেন?
✨ Show Answer (উত্তর দেখুন)
Answer: Trust F1 (or precision+recall). A model that labels everything as ham gets 95% accuracy while being useless — it catches zero spam. F1 balances precision (how many of the predicted spams are really spam?) and recall (how many real spams did we catch?), so a model that fails on the minority class is correctly penalized.
F1 (বা precision+recall)। সব ham বলা model ৯৫% accuracy পায় কিন্তু এক টুকরো spam-ও ধরতে পারে না। F1 precision ও recall-এর balance — minority class-এ ব্যর্থ model সঠিকভাবে penalty পায়।
-
List three reasons why adding more data is usually more valuable than picking a fancier model.ফ্যান্সি মডেলের চেয়ে বেশি ডেটা সাধারণত কেন বেশি মূল্যবান — ৩টি কারণ লিখুন।
✨ Show Answer (উত্তর দেখুন)
1) Better-quality data reduces noise — model's signal-to-noise rises. 2) More data covers more of the input distribution, reducing the chance your model meets a case it has never seen. 3) Simpler models trained on more data often outperform complex models trained on little data — they generalize better and are easier to reason about.
Summary — Module 39
scikit-learn turns machine learning into a uniform fit/predict/score workflow.
Always split data, always evaluate honestly, always prefer pipelines. Start with linear/logistic regression as a
baseline; escalate to trees and forests only if the baseline isn't enough. Understand your data and metrics before
reaching for deep learning.
fit/predict/score workflow-এ নামিয়ে আনে। সবসময় split, সৎ evaluation, pipeline ব্যবহার করুন। Baseline হিসেবে linear/logistic; অপর্যাপ্ত হলে tree/forest। deep learning-এ যাওয়ার আগে data ও metric বুঝুন।