Python Notes
Learn the fundamentals of machine learning — what it is, types of ML (supervised, unsupervised, reinforcement), key concepts, the ML workflow, and how to set up your Python ML environment.
Machine Learning (ML) is a subset of Artificial Intelligence that enables computers to learn from data and improve their performance on tasks without being explicitly programmed. Instead of writing rules, you give the machine examples and it learns the patterns.
What is Machine Learning?
Traditional Programming
Data + Rules → Output
Machine Learning
Data + Output (labels) → Rules (Model)
Real-world examples:
- Email spam detection — learns from labeled spam/not-spam emails
- Image recognition — learns from labeled images
- Movie recommendations — learns from user behavior
- Stock price prediction — learns from historical prices
- Medical diagnosis — learns from patient records
Types of Machine Learning
1. Supervised Learning
The algorithm learns from labeled training data (input-output pairs):
- Classification: Predict a category
- Spam detection (spam/not spam)
- Image classification (cat/dog/bird)
- Sentiment analysis (positive/negative)
- Fraud detection (fraud/legitimate)
- Regression: Predict a continuous value
- House price prediction
- Stock price forecasting
- Temperature prediction
- Salary estimation
2. Unsupervised Learning
The algorithm finds patterns in unlabeled data:
- Clustering: Group similar data points
- Customer segmentation
- Document grouping
- Gene expression analysis
- Dimensionality Reduction: Reduce the number of features
- Principal Component Analysis (PCA)
- t-SNE for visualization
- Association Rules: Find co-occurring patterns
- Market basket analysis ("customers who buy X also buy Y")
3. Reinforcement Learning
An agent learns by interacting with an environment and receiving rewards or penalties:
- Game playing (AlphaGo, Chess AI)
- Robot locomotion
- Autonomous vehicles
- Trading bots
Key ML Concepts
Features and Labels
Training vs Testing
from sklearn.model_selection import train_test_split
# Split data: 80% training, 20% testing
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print(f"Training samples: {len(X_train)}") # 4
print(f"Testing samples: {len(X_test)}") # 1
# Why split?
# - Train: Model learns from this data
# - Test: Evaluate how well model generalizes to NEW data
# - Never train on test data! (data leakage)Overfitting and Underfitting
Underfitting (High Bias)
- Model is too simple
- Doesn't capture underlying patterns
- High error on both training AND test data
Good Fit
- Model captures the real patterns
- Low error on both training AND test data
Overfitting (High Variance)
- Model memorizes training data
- Doesn't generalize to new data
- Low training error, HIGH test error
Bias-Variance Tradeoff
| Bias | How wrong is our model's assumptions? |
| Variance | How sensitive is the model to data fluctuations? |
| High Bias | Underfitting → Simple model, wrong assumptions |
| High Variance | Overfitting → Complex model, memorizes noise |
Setting Up Your ML Environment
pip install scikit-learn pandas numpy matplotlib seaborn
pip install xgboost lightgbm # Advanced boosting
pip install imbalanced-learn # For imbalanced datasets
pip install mlflow # Experiment tracking# Verify installations
import sklearn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
print(f"scikit-learn: {sklearn.__version__}")
print(f"NumPy: {np.__version__}")
print(f"Pandas: {pd.__version__}")The Machine Learning Workflow
Your First ML Model
Feature Engineering Basics
Scikit-Learn API
Scikit-learn follows a consistent API:
Common ML Algorithms Overview
| Algorithm | Type | When to Use |
|---|---|---|
| Linear Regression | Regression | Continuous output, linear relationship |
| Logistic Regression | Classification | Binary/multi-class, interpretable |
| Decision Tree | Both | Interpretable, non-linear, handles categorical |
| Random Forest | Both | Good baseline, handles missing values |
| SVM | Both | High-dim data, small-medium datasets |
| K-Means | Clustering | Find groups in unlabeled data |
| KNN | Both | Simple, non-parametric |
| XGBoost | Both | Often wins competitions, tabular data |
| Neural Network | Both | Complex patterns, large datasets |
Summary
In this lesson, you learned:
- ✅ What machine learning is and real-world applications
- ✅ Types of ML: supervised, unsupervised, reinforcement
- ✅ Key concepts: features, labels, training/test split
- ✅ Overfitting, underfitting, and the bias-variance tradeoff
- ✅ Setting up your Python ML environment
- ✅ The machine learning workflow (6 steps)
- ✅ Your first ML model with scikit-learn
- ✅ Introduction to feature engineering
- ✅ Scikit-learn's consistent API
📤 Code Output Examples
Features and Labels Output:
Features (X): house_size bedrooms age_years location 0 1200 3 10 1 1 1500 4 5 2 2 800 2 25 1 3 2000 5 2 3 4 1800 4 15 2 Target (y): 0 250000 1 320000 2 180000 3 450000 4 380000 Name: price, dtype: int64
Training vs Testing Output:
Training samples: 4 Testing samples: 1
Verify Installations Output:
scikit-learn: 1.4.2 NumPy: 1.26.4 Pandas: 2.2.1
First ML Model Output:
Dataset shape: (569, 30)
Classes: ['malignant' 'benign']
Class distribution:
1 357
0 212
Name: malignant, dtype: int64
First 3 rows:
mean radius mean texture mean perimeter mean area ...
0 17.99 10.38 122.80 1001.0 ...
1 20.57 17.77 132.90 1326.0 ...
2 19.69 21.25 130.00 1203.0 ...
Training: (455, 30), Testing: (114, 30)
Test Accuracy: 0.9737 (97.4%)
Classification Report:
precision recall f1-score support
malignant 0.97 0.95 0.96 43
benign 0.97 0.99 0.98 71
accuracy 0.97 114
macro avg 0.97 0.97 0.97 114
weighted avg 0.97 0.97 0.97 114
Sample prediction: benign
Probability: 0.998Feature Engineering Output:
age tenure_years salary_per_year_tenure is_senior 0 31 5.962466 14256.410256 0 1 38 7.558904 9525.423729 1 2 26 3.837440 24750.715307 0
Scikit-Learn API Output:
Pipeline accuracy: 0.974 CV scores: [0.96491228 0.96491228 0.97368421 0.96460177 0.99115044] Mean CV accuracy: 0.972 ± 0.010
⚠️ Common Mistakes
❌ Mistake 1: Test Data पर Training करना (Data Leakage)
# ❌ WRONG — scaler पूरे data से सीखता है (test info leak!)
scaler.fit(X) # Fits on ALL data including test
X_scaled = scaler.transform(X)
X_train, X_test = train_test_split(X_scaled, ...)
# ✅ RIGHT — पहले split करो, फिर only train data पर fit करो
X_train, X_test = train_test_split(X, ...)
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)🔑 Rule:fit()सिर्फ training data पर,transform()दोनों पर!
❌ Mistake 2: Feature Scaling भूल जाना
# ❌ WRONG — बिना scaling, SVM/KNN बहुत poor perform करते हैं
model = SVC()
model.fit(X_train, y_train) # Features different ranges में हैं
# ✅ RIGHT — हमेशा distance-based models से पहले scale करो
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
model = SVC()
model.fit(X_train_scaled, y_train)🔑 Note: Tree-based models (Random Forest, XGBoost) को scaling की जरूरत नहीं!
❌ Mistake 3: Accuracy से Imbalanced Data Evaluate करना
# ❌ WRONG — 95% accuracy मतलब अच्छा model नहीं (अगर 95% data एक class है)
print(f"Accuracy: {accuracy_score(y_test, y_pred)}") # Misleading!
# ✅ RIGHT — precision, recall, f1-score use करो
from sklearn.metrics import classification_report, roc_auc_score
print(classification_report(y_test, y_pred))
print(f"ROC-AUC: {roc_auc_score(y_test, y_pred_proba)}")❌ Mistake 4: Missing Values Handle नहीं करना
# ❌ WRONG — NaN values से model crash या wrong predictions
model.fit(X_with_nans, y) # Will raise error in most models!
# ✅ RIGHT — impute या remove करो
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy='median')
X_clean = imputer.fit_transform(X_with_nans)❌ Mistake 5: Random State Set नहीं करना
# ❌ WRONG — हर बार different results आएंगे, reproduce नहीं कर सकते
X_train, X_test = train_test_split(X, y, test_size=0.2)
# ✅ RIGHT — reproducibility के लिए random_state fix करो
X_train, X_test = train_test_split(X, y, test_size=0.2, random_state=42)❌ Mistake 6: Overfitting को Ignore करना
# ❌ WRONG — सिर्फ training accuracy देखकर खुश होना
print(f"Train Accuracy: {model.score(X_train, y_train)}") # 99.9%! Great? NO!
# ✅ RIGHT — training vs test दोनों compare करो
print(f"Train: {model.score(X_train, y_train):.3f}") # 99.9%
print(f"Test: {model.score(X_test, y_test):.3f}") # 62.3% → Overfitting!
# बड़ा gap = overfitting signal!❌ Mistake 7: Categorical Data को Directly Model में देना
✅ Key Takeaways
- 🧠 Machine Learning = Data से Rules सीखना — Traditional programming में rules manually लिखते हैं, ML में data से automatically learn होते हैं
- 📊 3 Types of ML — Supervised (labeled data), Unsupervised (unlabeled data), Reinforcement (reward-based learning)
- 🎯 Supervised Learning — Classification (category predict करना) और Regression (continuous value predict करना) दो main tasks हैं
- ✂️ Train-Test Split जरूरी है — Model कितना अच्छा generalize करता है, ये test data से पता चलता है, training accuracy से नहीं
- ⚖️ Bias-Variance Tradeoff — Simple model = underfitting, Complex model = overfitting; दोनों के बीच balance चाहिए
- 🔧 Feature Engineering is Key — Raw data को meaningful features में convert करना ML success का 80% हिस्सा है
- 📐 Scikit-Learn का Consistent API —
fit()→predict()→score()pattern सब models में same है, एक बार सीखो बार-बार use करो - 🔄 Cross-Validation use करो — Single split unreliable हो सकता है, 5-fold CV ज्यादा robust estimate देता है
- 🧹 Data Preprocessing mandatory है — Missing values, scaling, encoding — ये steps skip करने से model garbage results देगा
- 🚀 Pipeline बनाओ — Preprocessing + Model को Pipeline में combine करो ताकि data leakage avoid हो और code clean रहे
❓ FAQ
Q1: Machine Learning और AI में क्या difference है?
A: AI (Artificial Intelligence) एक broad field है जो machines को intelligent behavior देती है। ML, AI का एक subset है जो specifically data से learning पर focus करता है। Deep Learning, ML का subset है जो neural networks use करता है। Think of it as: AI > ML > Deep Learning।
Q2: ML सीखने के लिए कितना Math जरूरी है?
A: Basic level पर — Linear Algebra (matrices), Probability/Statistics, और Calculus (derivatives) की understanding चाहिए। लेकिन शुरुआत में scikit-learn use करके practical ML बिना deep math के भी सीख सकते हो। जैसे-जैसे advanced topics आएं (neural networks, optimization), math ज्यादा important हो जाता है।
Q3: Overfitting कैसे detect और fix करें?
A: Detection: Training accuracy बहुत high (99%) लेकिन test accuracy low (70%) — ये overfitting है। Fix: (1) More training data collect करो, (2) Model complexity कम करो (regularization), (3) Feature selection करो, (4) Cross-validation use करो, (5) Dropout/Early stopping (neural networks में)।
Q4: कौन सा algorithm पहले सीखना चाहिए?
A: शुरुआत Logistic Regression और Linear Regression से करो — ये simple, interpretable, और foundation हैं। फिर Decision Trees → Random Forest → XGBoost की तरफ बढ़ो। "No Free Lunch" theorem कहता है कि कोई एक algorithm सबके लिए best नहीं है — problem पर depend करता है।
Q5: Feature Scaling कब जरूरी है और कब नहीं?
A: जरूरी है: Distance-based algorithms (KNN, SVM, K-Means), Gradient-based algorithms (Neural Networks, Logistic Regression)। जरूरी नहीं: Tree-based algorithms (Decision Tree, Random Forest, XGBoost) — ये features को splits से handle करते हैं, magnitude matter नहीं करती।
Q6: Real-world projects में data कितना important है?
A: "Garbage in, garbage out" — ML में data ही सब कुछ है! Industry में 80% time data collection, cleaning, और preprocessing में जाता है। एक simple model with good data, complex model with bad data से हमेशा better perform करता है। Quality > Quantity (लेकिन quantity भी matter करती है)।
Q7: Classification और Regression में कैसे decide करें?
A: Output देखो: अगर output categories/classes हैं (spam/not-spam, cat/dog) → Classification। अगर output continuous number है (price, temperature, score) → Regression। कभी-कभी regression को classification में convert कर सकते हो (price > 50000 → expensive/cheap)।
Q8: Cross-Validation क्या है और क्यों use करें?
A: Single train-test split unreliable हो सकता है — random chance से अच्छा/बुरा result आ सकता है। K-Fold CV data को K parts में बांटता है, हर part बारी-बारी test set बनता है। ये ज्यादा reliable performance estimate देता है। Standard practice: 5-fold या 10-fold CV use करो।
🎯 Interview Questions
Q1: What is the difference between Supervised and Unsupervised Learning?
A: Supervised Learning uses labeled data (input-output pairs) to train models — the algorithm knows the correct answer during training and learns to map inputs to outputs. Examples: spam detection, house price prediction. Unsupervised Learning uses unlabeled data — the algorithm finds hidden patterns/structure on its own. Examples: customer segmentation, anomaly detection.
Q2: Explain the Bias-Variance Tradeoff with a real example.
A: Bias is error from oversimplified assumptions — like predicting house prices using only square footage (ignoring location, bedrooms). Variance is error from the model being too sensitive to training data — performing great on training set but poorly on new data. The trade-off: reducing one often increases the other.
Q3: What is Data Leakage and how do you prevent it?
A: Data leakage occurs when information from outside the training dataset is used to create the model, giving unrealistically good performance. Common causes: (1) Fitting scaler/encoder on full data before splitting, (2) Using future data to predict past, (3) Including target-correlated features that won't exist at prediction time.
Q4: When would you choose Random Forest over Logistic Regression?
A: Choose Random Forest when: (1) Data has non-linear relationships, (2) Feature interactions are important, (3) You have many features including irrelevant ones (built-in feature selection), (4) You need robustness without much tuning, (5) Missing values exist. Choose Logistic Regression when: (1) Interpretability is critical (coefficients have meaning), (2) You need probability calibration, (3) Data is linearly separable, (4) Dataset is small, (5) Regulatory requirements need explainable models (healthcare, finance).
Q5: How do you handle imbalanced datasets?
A: Strategies include: (1) Resampling: Oversample minority (SMOTE) or undersample majority class, (2) Class weights: Set class_weight='balanced' in sklearn models, (3) Metrics: Use precision, recall, F1-score, ROC-AUC instead of accuracy, (4) Ensemble methods: BalancedRandomForest, EasyEnsemble, (5) Threshold tuning: Adjust decision threshold from 0.5, (6) Collect more data for minority class. Best practice: combine multiple strategies and evaluate with stratified cross-validation.
Q6: Explain the complete ML Pipeline from raw data to deployment.
A: (1) Problem Definition: Define what to predict, success metrics, and business constraints. (2) Data Collection: Gather data from databases, APIs, files. (3) EDA: Understand distributions, correlations, outliers. (4) Preprocessing: Handle missing values, encode categoricals, scale features. (5) Feature Engineering: Create meaningful features from raw data. (6) Model Selection: Try multiple algorithms, use cross-validation. (7) Hyperparameter Tuning: GridSearch or RandomSearch for optimal params. (8) Evaluation: Final test set evaluation, compare to baseline. (9) Deployment: Serve model via API (Flask/FastAPI), containerize with Docker. (10) Monitoring: Track prediction drift, retrain periodically.
Q7: What is Cross-Validation and why is a single train-test split not enough?
A: A single random split can be lucky or unlucky — your model might get an easy or hard test set by chance. K-Fold Cross-Validation divides data into K equal folds, trains K models (each using K-1 folds for training, 1 for testing), and averages results. Benefits: (1) Every data point is tested exactly once, (2) More reliable performance estimate with confidence interval, (3) Better use of limited data. Stratified K-Fold maintains class proportions in each fold — essential for imbalanced data. Standard: use 5 or 10 folds.
Q8: What is Feature Engineering and why is it considered the most important step?
A: Feature engineering is creating new meaningful features from raw data that better represent the underlying patterns. Examples: extracting day-of-week from dates, creating ratios (price-per-sqft), binning ages into groups, polynomial features for non-linear relationships. It's considered most important because: (1) A good feature can make a simple model outperform a complex one, (2) Domain knowledge encoded as features is irreplaceable by algorithms, (3) "Applied ML is 80% feature engineering." Andrew Ng's famous quote captures this — algorithms are commoditized, but understanding your data isn't.
Q9: How do you decide between Classification and Regression for a given problem?
A: Look at the target variable: if it's a discrete category (yes/no, spam/ham, cat/dog/bird) → Classification. If it's a continuous number (price, temperature, age) → Regression. Edge cases: (1) Ordinal outputs (ratings 1-5) can be either — regression if you want exact values, classification if categories matter more. (2) You can convert: regression to classification (price > threshold → expensive/cheap), or use classification confidence scores as pseudo-regression. The business question determines the approach.
Q10: What metrics would you use to evaluate a classification model and why?
A: Accuracy — overall correct predictions; misleading for imbalanced data. Precision — of all predicted positives, how many are actually positive (important when false positives are costly: spam detection). Recall — of all actual positives, how many did we catch (important when false negatives are costly: cancer detection). F1-Score — harmonic mean of precision and recall; balanced trade-off. ROC-AUC — measures model's ability to distinguish classes across all thresholds; threshold-independent. Confusion Matrix — full picture of TP, TN, FP, FN. Choice depends on business cost: medical → prioritize recall, spam filter → prioritize precision.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Introduction to Machine Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Python Master Course topic.
Search Terms
python-master-course, python master course, python, master, course, machine, learning, introduction
Related Python Master Course Topics