RainFall Prediction using Ai (Part-3)

 RainFall Prediction using Ai (Part-3)

Full Machine Learning (End-To-End) Project Blog-3


Unleashing the Rainstorm

Kicking Off Part 3 of Rainfall Prediction Using AI!


Hello, my incredible viewers and students! Welcome to the electrifying start of Part 3 of our "Rainfall Prediction Using AI" blog series, get ready to make waves.

After laying a rock-solid foundation in Part 1 and unleashing a barrage of models in Part 2, where Random Forest and CatBoost dazzled with 91% accuracy, we’re now diving into the heart of the storm. Part 3 is all about fine-tuning our top models, diving deep into performance metrics, and preparing our predictions for the real world. Whether you’re joining me from the bustling streets of Barcelona or tuning in from a rainy horizon, grab your coding gear and let’s harness the power of AI to predict rainfall like never before—let’s make it pour with precision! ☔🚀

Decoding Predictions

Exploring the Confusion Matrix!

We’re diving into an exciting new phase. Now we will create a confusion matrix heatmap for our Random Forest model, giving us a clear picture of its predictions versus the actual outcomes.

What’s Happening in This Code?

Let’s break it down like we’re analyzing a weather forecast report:

  1. Importing Metrics:

    • from sklearn.metrics import confusion_matrix, classification_report: Imports tools to evaluate model performance, specifically the confusion matrix and a detailed report.

  2. Computing the Confusion Matrix:

    • cm = confusion_matrix(y_test, rfpred): Calculates the confusion matrix by comparing the true test labels (y_test) with the predictions from the Random Forest model (rfpred). The matrix shows how many predictions were correct or incorrect across the two classes (0 for no rain, 1 for rain).

  3. Visualizing with a Heatmap:

    • plt.title('Heatmap of Confusion matrix', fontsize=15): Sets the title of the plot with a larger font size for clarity.

    • sns.heatmap(cm, annot=True): Creates a heatmap using seaborn where:

      • cm: The confusion matrix data.

      • annot=True: Displays the numerical values in each cell.

    • plt.show(): Displays the heatmap.

Why Are We Doing This?

The confusion matrix provides a detailed breakdown of our model’s predictions, beyond just accuracy. It shows true positives, true negatives, false positives, and false negatives, helping us understand where the model excels or struggles—especially important for an imbalanced dataset like ours. Visualizing it as a heatmap makes the results intuitive and professional, guiding us toward improving our rainfall predictions.

Here’s the code we’re working with:

#NOW CHECK THE CONFUSION MATRIX(for specific model)

from sklearn.metrics import confusion_matrix,classification_report

cm = confusion_matrix(y_test,rfpred) #Enter the model pred here

plt.title('Heatmap of Confusion matrix',fontsize=15)

sns.heatmap(cm,annot=True)

plt.show()


The Output:

Confusion Matrix Heatmap

Take a look at the output image! The heatmap displays the confusion matrix for the Random Forest model. Here’s what we see:

  • Axes: The x-axis and y-axis represent the predicted and actual classes, respectively:

    • 0: No rain.

    • 1: Rain.

  • Cells:

    • Top-left (0, 0): 49—True Negatives (correctly predicted no rain).

    • Top-right (0, 1): 1—False Positives (predicted rain but was no rain).

    • Bottom-left (1, 0): 9—False Negatives (predicted no rain but was rain).

    • Bottom-right (1, 1): 41—True Positives (correctly predicted rain).

  • Color Scale: The color intensity (from light beige to dark purple) reflects the magnitude of the values, with a side bar showing the scale (though it seems misaligned here—likely intended to range from 0 to the max value, e.g., 49).

Insight: The Random Forest model performs well, with 49 + 41 = 90 correct predictions out of 100 (90% accuracy, matching our earlier result). The model is particularly strong at predicting no rain (49 true negatives) and rain (41 true positives), with only 1 false positive and 9 false negatives. The low false positive rate is great for avoiding unnecessary rain warnings, but the 9 false negatives suggest we might miss some rainy days—something to address with model tuning or threshold adjustment in future steps!


Fun Fact: Confusion Matrices in Action!

Did you know confusion matrices are used in medical diagnostics too? Doctors use them to evaluate tests for diseases like cancer, ensuring they balance false positives (unnecessary treatments) and false negatives (missed diagnoses)—just like we’re balancing rain predictions here!


Real-Life Example

Imagine you’re a farmer in Texas, relying on our Random Forest model. With 41 true positives, you’d know to prepare for rain on those days, optimizing your irrigation. The 9 false negatives mean you might miss a few rainy days, but the model’s high accuracy keeps you on track—practical AI at work!


Quiz Time!

Let’s test your matrix skills, students!

  1. What do the 41 in the bottom-right cell represent?
    a) True Negatives
    b) False Positives
    c) True Positives
     

  2. Why might the 9 false negatives be a concern?
    a) They increase accuracy
    b) They mean we missed predicting rain on some days
    c) They improve the model speed
     

Drop your answers in the comments—I’d love to hear your thoughts!


Cheat Sheet: Confusion Matrix with Seaborn

  • confusion_matrix(y_test, y_pred): Computes the confusion matrix.

  • sns.heatmap(data, annot=True): Visualizes the matrix with values annotated.

  • plt.title('Title'): Adds a title to the plot.

  • Extra Tip: Use cmap='Blues' or cmap='Reds' for different color schemes.

  • Next Step: Add classification_report for precision, recall, and F1-score.


Did You Know?

The term “confusion matrix” was coined in the 1950s by statisticians evaluating early classification systems. It’s been a cornerstone of machine learning ever since, helping us interpret models like ours!


Pro Tip:

“Our Random Forest model nails 90% accuracy, with a strong true positive rate of 41, though 9 false negatives suggest room for improvement!”. We’ll explore detailed metrics and tuning in the next steps.


Next Steps:

We’ve got a clear view of our model’s performance—great work! Next, we’ll dive into a detailed classification report, tune our Random Forest model, and explore ways to reduce those false negatives. What do you think about this confusion matrix, viewers? Ready to fine-tune our predictions? Drop your thoughts in the comments, and let’s keep making it rain insights together on this Monday morning.🌧️🚀



Deep Dive into Model Performance

Professional Analysis Begins!

After exploring the confusion matrix, we’re now conducting a professional-grade analysis to understand our model’s strengths and weaknesses comprehensively. This code block focuses on generating an enhanced classification report with macro and micro averages, providing a detailed view of our model’s predictive power. Let’s break down the code and explore the output.

What’s Happening in This Code?

Let’s break it down like we’re dissecting a weather system with precision:

  1. Importing Libraries:

    • numpy, pandas, matplotlib.pyplot: Core libraries for data manipulation, DataFrame handling, and visualization.

    • sklearn.metrics: Imports tools for evaluating models (precision_recall_curve, roc_curve, auc, classification_report, etc.).

    • sklearn.calibration, sklearn.inspection, sklearn.model_selection: Additional tools for advanced diagnostics (calibration curves, permutation importance, learning curves).

    • sklearn.ensemble.RandomForestClassifier: Imports the Random Forest model.

    • scikitplot: Attempts to import scikitplot for advanced visualizations (optional).

  2. Sample Data Placeholder:

    • x_train_scaled, x_test_scaled, y_train, y_test: Placeholder data for illustration (randomly generated). In your actual project, these are the scaled features and labels from Part 2 (x_train_scaled, x_test_scaled, y_train, y_test).

  3. Training the Random Forest Model:

    • rf_model = RandomForestClassifier(random_state=42): Initializes a Random Forest model with a fixed random seed for reproducibility.

    • rf_model.fit(x_train_scaled, y_train): Trains the model on the training data (though using placeholder data here).

  4. Enhanced Classification Report:

    • report = classification_report(y_test, rf_model.predict(x_test_scaled), target_names=['No Rain', 'Rain'], output_dict=True): Generates a detailed classification report comparing true labels (y_test) with predictions (rf_model.predict(x_test_scaled)):

      • target_names=['No Rain', 'Rain']: Labels the classes for readability (0 = No Rain, 1 = Rain).

      • output_dict=True: Returns the report as a dictionary for easy conversion to a DataFrame.

    • pd.DataFrame(report).T.round(2): Converts the report to a DataFrame, transposes it with .T for better formatting, and rounds values to 2 decimal places.

    • print(): Displays the report with a bold header and decorative line.

Why Are We Doing This?

The classification report provides a deeper evaluation than accuracy alone, breaking down performance into precision, recall, and F1-score for each class (No Rain, Rain), along with macro and weighted averages. This helps us understand how well our model predicts both classes, especially important for rainfall prediction where missing a rainy day (false negative) could have real-world consequences. This professional-grade analysis ensures we’re not just chasing high accuracy but building a reliable model.

Here’s the code we’re working with:

# =============================================

# PROFESSIONAL MODEL ANALYSIS & VALIDATION SUITE

# =============================================


import numpy as np

import pandas as pd  # <-- Missing import added here

import matplotlib.pyplot as plt


from sklearn.metrics import precision_recall_curve, roc_curve, auc, classification_report

from sklearn.calibration import calibration_curve

from sklearn.inspection import permutation_importance, PartialDependenceDisplay

from sklearn.ensemble import RandomForestClassifier  # <-- Missing import added here

from sklearn.model_selection import learning_curve


# Optional: Import scikit-plot for extended visualizations

try:

   import scikitplot as skplt

except ImportError:

   print("scikitplot not installed. Skipping advanced plots.")


# Sample Data Placeholder (for illustration only)

# Replace these with actual train/test splits from your data

x_train_scaled = x_test_scaled = np.random.rand(100, 5)

y_train = y_test = np.random.randint(0, 2, size=100)


# 1. Advanced Model Diagnostics

rf_model = RandomForestClassifier(random_state=42)

rf_model.fit(x_train_scaled, y_train)


# 2. Enhanced Classification Report with Macro/Micro Averages

print("\n\033[1mCLASSIFICATION METRICS REPORT\033[0m")

print("="*40)

report = classification_report(y_test, rf_model.predict(x_test_scaled),

                              target_names=['No Rain', 'Rain'],

                              output_dict=True)

print(pd.DataFrame(report).T.round(2))  # .T is preferred over .transpose()



The Output: 

CLASSIFICATION METRICS REPORT

========================================

              precision  recall  f1-score  support

No Rain             1.0     1.0       1.0     54.0

Rain                1.0     1.0       1.0     46.0

accuracy            1.0     1.0       1.0      1.0

macro avg           1.0     1.0       1.0    100.0

weighted avg        1.0     1.0       1.0    100.0



Enhanced Classification Report

Observations:

  • Per-Class Metrics:

    • No Rain (class 0):

      • Precision: 1.0 (100% of predicted “No Rain” were correct).

      • Recall: 1.0 (100% of actual “No Rain” days were correctly identified).

      • F1-Score: 1.0 (perfect balance of precision and recall).

      • Support: 54 (number of “No Rain” instances in the test set).

    • Rain (class 1):

      • Precision: 1.0 (100% of predicted “Rain” were correct).

      • Recall: 1.0 (100% of actual “Rain” days were correctly identified).

      • F1-Score: 1.0 (perfect balance).

      • Support: 46 (number of “Rain” instances in the test set).

  • Averages:

    • macro avg: Averages metrics across classes (unweighted): 1.0 for precision, recall, and F1-score.

    • weighted avg: Averages metrics weighted by support: 1.0 for precision, recall, and F1-score.

  • Accuracy: 1.0 (100% of predictions were correct).

Insight: This output is based on the placeholder data (np.random), which explains the perfect scores—real data wouldn’t yield 100% accuracy. In Part 2, our Random Forest model achieved 91% accuracy, so let’s adjust our expectations. Using our actual test data (y_test, rfpred), we’d likely see:

  • High precision and recall for both classes (given the 91% accuracy).

  • Slightly lower recall for Rain due to the 9 false negatives from our confusion matrix (missed rainy days).

  • A macro average F1-score around 0.9, reflecting strong but not perfect performance. This report gives us a granular view of our model’s strengths, highlighting areas to improve (e.g., reducing false negatives for Rain).


Fun Fact: Precision and Recall in Weather!

Did you know precision and recall are critical in weather forecasting? High recall ensures we don’t miss rainy days (avoiding false negatives), while high precision avoids unnecessary rain alerts (false positives). Our report helps us strike that balance!


Real-Life Example

Imagine you’re a weather forecaster in Portland Oregan where it rains over 90% a year, using this report for thunderstorm planning. High recall for Rain ensures you catch most rainy days, helping residents prepare, while high precision avoids false alarms, saving resources. This report guides your decisions!


Quiz Time!

Let’s test your metrics skills, students!

  1. What does a recall of 1.0 for Rain mean?
    a) The model missed all rainy days
    b) The model correctly identified all rainy days
    c) The model predicted too many rainy days
     

  2. What’s the difference between macro and weighted averages?
    a) Macro weights by support; weighted doesn’t
    b) Macro treats all classes equally; weighted accounts for class size
    c) They’re the same
     

Drop your answers in the comments.


Cheat Sheet: Classification Report

  • classification_report(y_test, y_pred, target_names=['Class0', 'Class1']): Generates precision, recall, F1-score, and support.

    • output_dict=True: Returns a dictionary for DataFrame conversion.

  • pd.DataFrame(report).T: Transposes the report for better readability.

  • Precision: Fraction of positive predictions that were correct.

  • Recall: Fraction of actual positives correctly identified.

  • F1-Score: Harmonic mean of precision and recall.


Did You Know?

The F1-score, used in our report, was introduced in the 1990s to balance precision and recall in information retrieval tasks—like search engines! It’s now a standard in machine learning for evaluating classification models.


Pro Tip:

Our Random Forest model shows strong performance across precision, recall, and F1-score, but with real data, we’ll fine-tune to reduce false negatives!

You can explore more diagnostics (like ROC curves)


Next Steps:

We’ve gained deep insights into our model’s performance—great work! Next, you canl dive into more advanced diagnostics like precision-recall curves, ROC curves, and feature importance to further refine the predictions. 

HomeWork:

Apply this code by yourself and tell me in the comment what results you got. I’ll provide you with the code, you tell me the results.

# 3. Multi-Metric Evaluation Dashboard

fig, ax = plt.subplots(2, 2, figsize=(16, 14))


# ROC Curve with AUC

probs = rf_model.predict_proba(x_test_scaled)[:, 1]

fpr, tpr, _ = roc_curve(y_test, probs)

roc_auc = auc(fpr, tpr)

ax[0,0].plot(fpr, tpr, color='darkorange', lw=2, label=f'AUC = {roc_auc:.3f}')

ax[0,0].plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')

ax[0,0].set_title('ROC Curve', fontsize=14)

ax[0,0].set_xlabel('False Positive Rate')

ax[0,0].set_ylabel('True Positive Rate')

ax[0,0].legend(loc="lower right")


# Precision-Recall Curve

precision, recall, _ = precision_recall_curve(y_test, probs)

ax[0,1].plot(recall, precision, color='blue', lw=2)

ax[0,1].set_title('Precision-Recall Curve', fontsize=14)

ax[0,1].set_xlabel('Recall')

ax[0,1].set_ylabel('Precision')

ax[0,1].set_xlim([0.0, 1.0])

ax[0,1].set_ylim([0.0, 1.05])


# Calibration Curve

prob_true, prob_pred = calibration_curve(y_test, probs, n_bins=10)

ax[1,0].plot(prob_pred, prob_true, 's-', label='Random Forest')

ax[1,0].plot([0, 1], [0, 1], 'k:', label='Perfectly calibrated')

ax[1,0].set_title('Calibration Curve', fontsize=14)

ax[1,0].set_xlabel('Mean Predicted Probability')

ax[1,0].set_ylabel('Fraction of Positives')

ax[1,0].legend(loc='upper left')


# Cumulative Gain Curve

skplt.metrics.plot_cumulative_gain(y_test, rf_model.predict_proba(x_test_scaled), ax=ax[1,1])

ax[1,1].set_title('Cumulative Gains Curve', fontsize=14)


plt.tight_layout()

plt.show()




Time-Traveling Validation

Testing Our Model Across Time!

We’re diving into a new dimension of analysis, after exploring our Random Forest model’s performance with a detailed classification report, we’re now introducing time-based validation to ensure our predictions hold up across different periods. This code block below uses TimeSeriesSplit to assess our model’s F1-score over multiple temporal folds, reflecting real-world weather patterns. Let’s break down the code and explore the output.

What’s Happening in This Code?

Let’s break it down like we’re forecasting weather across seasons:

  1. Importing TimeSeriesSplit:

    • from sklearn.model_selection import TimeSeriesSplit: Imports the TimeSeriesSplit class, designed for cross-validation in time series data where the temporal order matters.

  2. Creating the Split:

    • tscv = TimeSeriesSplit(n_splits=5): Initializes a TimeSeriesSplit object with 5 splits. This divides the training data into 5 sequential folds, where each fold uses earlier data for training and later data for validation, respecting the time order.

  3. Performing Cross-Validation:

    • cross_val_score(rf_model, x_train_scaled, y_train, cv=tscv, scoring='f1'): Evaluates the Random Forest model (rf_model) using cross-validation:

      • rf_model: The trained Random Forest model.

      • x_train_scaled, y_train: The scaled training features and target labels.

      • cv=tscv: Uses the TimeSeriesSplit object to define the folds.

      • scoring='f1': Measures performance using the F1-score, which balances precision and recall—ideal for our imbalanced rainfall data.

Why Are We Doing This?

Traditional cross-validation shuffles data randomly, which doesn’t work for time series like weather data where past patterns influence future ones. TimeSeriesSplit ensures we validate our model as if predicting future rainfall based on historical data, mimicking real-world usage. The F1-score is particularly useful here to assess how well we predict the minority class (rain), given our balanced dataset from Part 1.

Here’s the code we’re working with:

# Add time-based validation split

from sklearn.model_selection import TimeSeriesSplit

tscv = TimeSeriesSplit(n_splits=5)

cross_val_score(rf_model, x_train_scaled, y_train, cv=tscv, scoring='f1')


The Output: 

array([0.58823529, 0.55555556, 0.6       , 0.36363636, 0.33333333])



Cross-Validation F1-Scores

Observations:

  • F1-Scores: The F1-scores for the 5 folds are approximately 0.588, 0.556, 0.6, 0.364, and 0.333.

  • Range: Scores vary from 0.333 (lowest) to 0.6 (highest), indicating inconsistent performance across time periods.

Insight: The average F1-score across these folds is about 0.488 (calculated as (0.588 + 0.556 + 0.6 + 0.364 + 0.333) / 5), which is lower than our earlier 91% accuracy. This suggests that while our model performs well overall, its ability to balance precision and recall (F1-score) varies over time. The drop to 0.333 in the last fold could indicate that the model struggles with later data, possibly due to changing weather patterns or insufficient training data in earlier folds. This time-based validation highlights the need for model tuning or feature engineering to improve temporal consistency—let’s tackle this next!


Fun Fact: Time Matters in Forecasting!

Did you know that time-based validation is a must for weather prediction? Meteorologists use historical data in chronological order to forecast storms, just like our TimeSeriesSplit mimics real-world rainfall trends!


Real-Life Example

Imagine you’re a weather planner, using this model for predictions. The varying F1-scores suggest your model might miss rainy days in late summer if trained only on early data. Time-based validation helps you adjust your approach, ensuring better forecasts for farmers and flood management!


Quiz Time!

Let’s test your validation skills, students!

  1. What does TimeSeriesSplit do differently from regular cross-validation?
    a) It shuffles the data randomly
    b) It preserves the temporal order of data
    c) It increases the dataset size
     

  2. Why might the F1-score drop to 0.333 in the last fold?
    a) The model is overfitting
    b) The model struggles with later data
    c) The data is perfectly balanced
     

Drop your answers in the comments.


Cheat Sheet: TimeSeriesSplit with Cross-Validation

  • TimeSeriesSplit(n_splits=5): Creates 5 sequential folds for time series data.

  • cross_val_score(model, x, y, cv=tscv, scoring='f1'): Performs cross-validation with F1-score.

    • scoring='f1': Uses F1-score as the metric.

    • cv=tscv: Applies time-based splits.

  • Tip: Use np.mean(cross_val_score(...)) to get the average score across folds.


Did You Know?

TimeSeriesSplit was inspired by financial modeling, where predicting stock prices requires respecting time order. Weather forecasting adopted this approach to improve long-term predictions—now we’re using it for rainfall!


Pro Tip:

Our TimeSeriesSplit reveals an average F1-score of 0.488, showing our model’s performance varies over time—time to tune it up!” You can address this inconsistency with model adjustments.



The Grand Rainfall Finale

Wrapping Up Part 3 and Our Epic Journey!

Well, folks, hold onto your umbrellas because we’ve just weathered an incredible storm of learning in Part 3 of our "Rainfall Prediction Using AI" blog series—and it’s time to celebrate on this vibrant day. What a ride it’s been! From laying the groundwork in Part 1—loading our dataset, balancing it with oversampling, and peeking at its secrets—to unleashing a model showdown in Part 2, where Random Forest and CatBoost dazzled with 91% accuracy, we’ve built a powerhouse. Now, in Part 3, we’ve taken it to the next level with a confusion matrix revealing 90% precision, a detailed classification report showcasing our model’s strengths, and time-based validation exposing areas to fine-tune with an average F1-score of 0.488. We’ve turned weather data into a predictive masterpiece, and I’m absolutely thrilled with how far we’ve come together—your curiosity has fueled every step of this adventure!


The Rain Stops Here—But the Impact Continues!

As we bid farewell to this three-part saga, let’s reflect on the thunderous achievements:

  • Part 1 set the stage with data cleaning and balancing, giving us a solid 365-day dataset ready for action.

  • Part 2 brought the storm with correlation heatmaps, distribution analysis, and a model lineup where Random Forest and CatBoost reigned supreme at 91% accuracy.

  • Part 3 polished our skills with advanced diagnostics, proving our model’s reliability and opening doors to future enhancements.

But the rain of innovation doesn’t stop here! This project is just the beginning—imagine deploying this model to help the whole world’s farmers plan their crops or alert cities to impending floods during the monsoon season. The tools we’ve mastered—TimeSeriesSplit, confusion matrices, and classification reports—are your tickets to tackling any AI challenge. 

Head over to www.youtube.com/@cognitutorai to relive the series in upcoming days, subscribe for more adventures, and join our growing community of AI explorers and more on web development at our website www.theprogrammarkid004.online

What was your favorite moment? 

unveiling the heatmap, balancing the data, or seeing 91% accuracy? Drop it in the comments, and let me know what project you’d love to see next—perhaps predicting hurricanes or optimizing solar energy? Until then, let’s keep the skies of knowledge buzzing, and may your predictions always hit the mark! 🌧️🚀