Machine Learning: Regression, Classification & Model Evaluation
1. Machine Learning Overview
Machine Learning (ML) allows a computer to learn patterns from data and make predictions without explicitly programming every rule. Two common supervised learning tasks are:2. Regression
Regression is used when the target/output is a continuous numerical value.Example
Predict house price:- Linear Regression
- Ridge Regression
- Lasso Regression
3. Linear Regression
Linear Regression finds a relationship between input features and a continuous target. The basic equation is:y= predicted valuex= inputm= slope/coefficientb= intercept
Example
Predict salary based on years of experience.Important Properties
coef_→ learned slope/weightintercept_→ value ofywhen all features are 0
When to use
Use Linear Regression when:- Target is continuous
- Relationship is approximately linear
- You want a simple and interpretable model
4. Ridge Regression
Ridge Regression is Linear Regression with L2 regularization. It adds a penalty for large coefficients. Conceptually:α= regularization strength- Larger
α→ stronger regularization
Example
Why use Ridge?
Ridge helps when:- Features are highly correlated
- The model is overfitting
- You have many features
Important
Ridge generally shrinks coefficients toward zero, but usually does not make them exactly zero.5. Lasso Regression
Lasso Regression uses L1 regularization. Conceptually:Example
Why use Lasso?
Lasso is useful for:- Feature selection
- Reducing unnecessary features
- Preventing overfitting
Ridge vs Lasso
6. Classification
Classification predicts a category/class.Examples
- Logistic Regression
- K-Nearest Neighbors
- Decision Tree
- Random Forest
- Support Vector Machine
- Naive Bayes
7. Logistic Regression
Despite its name, Logistic Regression is mainly used for classification. It predicts the probability of a class. The sigmoid function converts a value into a probability between 0 and 1.Example
Important methods
When to use
Use Logistic Regression when:- Target is categorical
- You need probability estimates
- You want a simple and interpretable classification model
8. K-Nearest Neighbors (KNN)
KNN predicts a data point based on its nearest training examples. The basic idea:Example
Choosing K
SmallK:
K:
Important
KNN is distance-based, so feature scaling is usually important.9. Decision Tree
A Decision Tree makes predictions using a sequence of questions. Example:- Root node
- Internal nodes
- Branches
- Leaf nodes
Example
Important parameters
Advantages
- Easy to understand
- Little preprocessing required
- Can model non-linear relationships
Disadvantage
A deep tree can easily overfit.10. Random Forest
Random Forest is an ensemble of multiple Decision Trees. Instead of relying on one tree:Example
Important parameters
n_estimators controls the number of trees.
Advantages
- Usually more robust than a single tree
- Handles non-linear relationships
- Works with many features
- Less prone to overfitting than a single unrestricted tree
11. Support Vector Machine (SVM)
SVM finds a decision boundary that separates classes. The best boundary tries to maximize the margin between classes.Example
Common kernels
Important
SVM is sensitive to feature scale. Scaling is commonly performed before training:12. Naive Bayes
Naive Bayes is a probabilistic classification algorithm based on Bayes’ theorem. Bayes’ theorem:Example
Common Naive Bayes types
13. Model Evaluation
After training a model, we need to determine:14. Confusion Matrix
A confusion matrix summarizes classification predictions. For binary classification:
Where:
- TP = True Positive
- TN = True Negative
- FP = False Positive
- FN = False Negative
Python
15. Accuracy
Accuracy tells us the percentage of predictions that were correct. Formula:Example
Problem with Accuracy
Accuracy can be misleading for imbalanced datasets. Example:16. Precision
Precision answers:Of all the samples predicted as positive, how many were actually positive?Formula:
Python
When precision matters
Precision is important when false positives are costly. Example:17. Recall
Recall answers:Of all the actual positive samples, how many did the model find?Formula:
Python
When recall matters
Recall is important when false negatives are costly. Example:18. Precision vs Recall
Remember:19. F1 Score
F1 Score combines Precision and Recall. It is the harmonic mean of precision and recall. Formula:Python
When to use
F1 is useful when:- Classes are imbalanced
- Both precision and recall matter
- You want one metric balancing both
20. ROC-AUC
ROC-AUC evaluates how well a classification model separates positive and negative classes across different classification thresholds.ROC
ROC stands for:AUC
AUC means:Python
Important
ROC-AUC generally requires probabilities or decision scores, not just final class labels. For example:21. MAE
MAE stands for:Python
Interpretation
If MAE is:22. MSE
MSE stands for:Python
Important
MSE gives more importance to large errors because errors are squared.23. RMSE
RMSE stands for:Python
Version note
If your scikit-learn version does not provideroot_mean_squared_error, use: