Skip to main content

What is Scikit-learn?

Scikit-learn (sklearn) is one of the most popular Python libraries for Machine Learning. It provides easy-to-use implementations of various ML algorithms for:
  • Classification
  • Regression
  • Clustering
  • Dimensionality Reduction
  • Model Selection
  • Data Preprocessing
  • Evaluation

Installation

Import commonly used libraries:

Scikit-learn Workflow

Almost every ML model in sklearn follows the same workflow.

Built-in Datasets

Scikit-learn provides several datasets for learning and experimentation.

Common Datasets


Example: Iris Dataset

Output
Access data
Feature names
Target classes

Example: California Housing Dataset


Train-Test Split

Machine Learning models should never be evaluated using the same data they were trained on. Instead:
  • Training Data → Learn patterns
  • Testing Data → Evaluate performance

Syntax


Parameters


Example


Model Workflow

All sklearn models have the same interface.
This consistency is one of sklearn’s biggest strengths.

fit()

Used to train the model.
Example

predict()

Predicts outputs for unseen data.
Example
Predict a single sample

predict_proba()

Returns prediction probabilities.
Example Output
Useful for:
  • Confidence scores
  • ROC Curve
  • Threshold tuning

score()

Returns the default evaluation metric. Classification
Regression Returns R² Score

Complete Example


Data Preprocessing

Preprocessing improves model performance. Common preprocessing includes:
  • Scaling
  • Encoding
  • Missing values
  • Feature engineering

StandardScaler

Standardizes features. Formula
Output
  • Mean = 0
  • Standard Deviation = 1
Useful for:
  • Logistic Regression
  • SVM
  • KNN
  • Neural Networks

Example

Notice
Never do

MinMaxScaler

Scales values between 0 and 1. Formula
Example
Useful for:
  • Neural Networks
  • Distance-based algorithms

StandardScaler vs MinMaxScaler


LabelEncoder

Encodes target labels. Example
Output
Decode
Note: Use LabelEncoder mainly for the target (y), not feature columns.

OneHotEncoder

Converts categorical features into binary columns. Example
Output
Ignore unknown categories

Model Persistence with joblib

Instead of training every time, save the trained model. Install

Save Model


Load Model

Use it

Complete Example


Important Missing Topics

Instead of manually scaling and training:
Advantages
  • Prevents data leakage
  • Cleaner code
  • Easy deployment

Model Evaluation

Accuracy


Confusion Matrix


Classification Report

Shows:
  • Precision
  • Recall
  • F1 Score
  • Accuracy

Regression Metrics


Cross Validation

Instead of a single train-test split:
Benefits
  • More reliable evaluation
  • Less variance
  • Better performance estimate

Random State

Ensures reproducible results. Without it:
  • Every run gives a different split.

Common Mistakes

❌ Scaling before splitting data
✔ Correct

❌ Fitting scaler on test data
✔ Correct

❌ Training and testing on the same dataset
Always keep a separate test set.

End-to-End Example


Summary