> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Scikit Learn Code Example

This example demonstrates a **complete Machine Learning workflow** using **Scikit-learn**. It covers loading data, preprocessing, training, evaluation, saving the model, and making predictions.

***

# Step 1: Import Required Libraries

```python theme={null}
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
import joblib
```

Each import has a specific purpose.

| Import               | Purpose                                    |
| :------------------- | :----------------------------------------- |
| `load_iris`          | Loads the Iris dataset                     |
| `train_test_split`   | Splits data into training and testing sets |
| `Pipeline`           | Chains preprocessing and model together    |
| `StandardScaler`     | Standardizes feature values                |
| `LogisticRegression` | Classification algorithm                   |
| `joblib`             | Saves and loads trained models             |

***

# Step 2: Load the Dataset

```python theme={null}
iris = load_iris()
```

This loads the **Iris dataset** into memory.

Think of `iris` as a Python object containing everything about the dataset.

You can inspect it:

```python theme={null}
print(iris.keys())
```

Output

```python theme={null}
dict_keys([
    'data',
    'target',
    'frame',
    'target_names',
    'DESCR',
    'feature_names',
    'filename',
    'data_module'
])
```

***

## Dataset Contents

```python theme={null}
iris.data
```

Contains the feature values.

Example

| Sepal Length | Sepal Width | Petal Length | Petal Width |
| :----------- | :---------- | :----------- | :---------- |
| 5.1          | 3.5         | 1.4          | 0.2         |
| 4.9          | 3.0         | 1.4          | 0.2         |

There are **4 numerical features**.

***

```python theme={null}
iris.target
```

Contains the labels.

Example

```python theme={null}
[0, 0, 0, 1, 2, 1, ...]
```

Where

```text theme={null}
0 → Setosa
1 → Versicolor
2 → Virginica
```

***

# Step 3: Separate Features and Labels

```python theme={null}
X = iris.data
y = iris.target
```

Machine Learning models always use:

```text theme={null}
X → Input Features

y → Target Labels
```

Here,

```text theme={null}
X =
[
 [5.1,3.5,1.4,0.2],
 [4.9,3.0,1.4,0.2],
 ...
]

y =
[
0,
0,
1,
2,
...
]
```

***

# Step 4: Split the Dataset

```python theme={null}
X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42
)
```

This divides the dataset into **training** and **testing** sets.

Imagine you have **150 flowers**.

```text theme={null}
Total Flowers = 150
```

Since

```python theme={null}
test_size = 0.2
```

20% goes to testing.

```text theme={null}
Training = 120 flowers

Testing = 30 flowers
```

Diagram

```text theme={null}
150 Samples

      │
      ▼

 ┌──────────────┐
 │ train = 120  │
 └──────────────┘

 ┌──────────────┐
 │ test = 30    │
 └──────────────┘
```

***

## Why Split?

If we train and test on the same data:

```text theme={null}
Model memorizes answers
```

Instead,

```text theme={null}
Training Data
↓

Model Learns

↓

Testing Data

↓

Check if it generalizes
```

***

## random\_state=42

```python theme={null}
random_state=42
```

Makes the split reproducible.

Without it,

Run 1

```text theme={null}
Train
A B C D

Test
E F
```

Run 2

```text theme={null}
Train
B D E

Test
A C
```

Every run changes.

With `42`, every run produces the same split.

***

# Step 5: Create a Pipeline

```python theme={null}
pipeline = Pipeline([
    ("scaler", StandardScaler()),
    ("classifier", LogisticRegression())
])
```

This is one of the best features of Scikit-learn.

Instead of writing

```python theme={null}
scaler.fit()

scaler.transform()

model.fit()

model.predict()
```

everything is combined into a single object.

Pipeline structure

```text theme={null}
Input Data
      │
      ▼

StandardScaler

      │
      ▼

LogisticRegression

      │
      ▼

Prediction
```

***

## First Step

```python theme={null}
("scaler", StandardScaler())
```

Standardizes every feature.

Formula

\[<br />z=\frac\{x-\mu}\{\sigma}<br />]

Example

Original values

```text theme={null}
5
10
15
```

Scaled values

```text theme={null}
-1.22
0.00
1.22
```

Now all features have

```text theme={null}
Mean = 0

Standard Deviation = 1
```

This helps many algorithms converge faster and prevents features with larger scales from dominating.

***

## Second Step

```python theme={null}
("classifier", LogisticRegression())
```

This is the Machine Learning algorithm.

Its job:

```text theme={null}
Given flower measurements

↓

Predict flower species
```

***

# Step 6: Train the Pipeline

```python theme={null}
pipeline.fit(X_train, y_train)
```

This is the most important step.

Internally, the pipeline performs:

### Step A

```python theme={null}
StandardScaler.fit(X_train)
```

Calculates

```text theme={null}
Mean

Standard deviation
```

using only the training data.

***

### Step B

```python theme={null}
StandardScaler.transform(X_train)
```

Converts the training data into standardized values.

***

### Step C

```python theme={null}
LogisticRegression.fit()
```

Learns the relationship

```text theme={null}
Flower Measurements

↓

Flower Species
```

So the pipeline effectively does:

```text theme={null}
Training Data

↓

Compute Mean & Std

↓

Scale Data

↓

Train Logistic Regression
```

All with one line:

```python theme={null}
pipeline.fit(...)
```

***

# Step 7: Predict

```python theme={null}
predictions = pipeline.predict(X_test)
```

The pipeline automatically:

```text theme={null}
Test Data

↓

StandardScaler.transform()

↓

Logistic Regression

↓

Predictions
```

Notice that it **does not call** `fit() `**on the scaler again**. It reuses the mean and standard deviation learned from the training data.

Example output

```python theme={null}
[1 0 2 1 1]
```

Meaning

```text theme={null}
Flower 1 → Versicolor

Flower 2 → Setosa

Flower 3 → Virginica
```

***

# Step 8: Evaluate

```python theme={null}
print("Accuracy:", pipeline.score(X_test, y_test))
```

For `LogisticRegression`, `.score()` returns **accuracy**.

Internally it is equivalent to:

```python theme={null}
from sklearn.metrics import accuracy_score

pred = pipeline.predict(X_test)

accuracy = accuracy_score(y_test, pred)
```

Suppose

```text theme={null}
30 flowers tested

29 correct

1 incorrect
```

Accuracy

```text theme={null}
29 / 30

=

0.9667

=

96.67%
```

***

# Step 9: Save the Model

```python theme={null}
joblib.dump(pipeline, "iris_pipeline.pkl")
```

This saves the **entire pipeline** to a file.

```text theme={null}
iris_pipeline.pkl
```

contains:

* StandardScaler (with learned mean and standard deviation)
* LogisticRegression (with learned coefficients)

You don't need to retrain the model every time you run your application.

***

# Step 10: Load the Saved Model

```python theme={null}
loaded_model = joblib.load("iris_pipeline.pkl")
```

This restores the saved pipeline into memory.

It is identical to the original trained pipeline.

***

# Step 11: Predict Again

```python theme={null}
print(loaded_model.predict(X_test[:5]))
```

Here,

```python theme={null}
X_test[:5]
```

selects the first five test samples.

For example:

```text theme={null}
30 test samples

↓

Take only first 5
```

These samples are automatically:

```text theme={null}
Standardized

↓

Passed into Logistic Regression

↓

Predicted
```

Possible output

```python theme={null}
[1 0 2 1 1]
```

***

# What Happens Internally?

```text theme={null}
                Iris Dataset
                     │
                     ▼
        X (Features), y (Labels)
                     │
                     ▼
           train_test_split()
          /                   \
         ▼                     ▼
Training Data           Testing Data
     │                       │
     ▼                       │
 StandardScaler.fit()         │
     │                       │
     ▼                       │
 Transform Training Data      │
     │                       │
     ▼                       │
 LogisticRegression.fit()     │
     │                       │
     └───────────────┐        │
                     ▼        ▼
             pipeline.predict(X_test)
                     │
                     ▼
          StandardScaler.transform()
                     │
                     ▼
          Logistic Regression Predict
                     │
                     ▼
               Predicted Labels
                     │
                     ▼
             Compare with y_test
                     │
                     ▼
                 Accuracy Score
                     │
                     ▼
          Save Pipeline (.pkl file)
                     │
                     ▼
              Load Later with joblib
                     │
                     ▼
             Predict New Samples
```

## Why use a `Pipeline`?

Without a pipeline, you'd manually write:

```python theme={null}
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

model = LogisticRegression()
model.fit(X_train, y_train)

predictions = model.predict(X_test)
```

With a pipeline:

```python theme={null}
pipeline = Pipeline([
    ("scaler", StandardScaler()),
    ("classifier", LogisticRegression())
])

pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)
```

The pipeline automatically ensures that preprocessing is applied consistently during both training and prediction, making the code cleaner and reducing the risk of mistakes such as accidentally fitting the scaler on the test data.
