0% found this document useful (0 votes)
3 views7 pages

Akshar AI Assignment

The document outlines a project by students Akshar Chavda and Jeet Upadhyaya at Indrashil University to develop an AI-ML model for predicting rainfall in Ahmedabad for June, July, and August 2025. The code utilizes a RandomForestRegressor and includes data preprocessing, feature scaling, model training, evaluation, and visualization of results. The model predicts rainfall amounts of 213.850 mm for June, 333.575 mm for July, and 351.950 mm for August, showing a strong correlation between actual and predicted values.

Uploaded by

antratiwari04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Akshar AI Assignment

The document outlines a project by students Akshar Chavda and Jeet Upadhyaya at Indrashil University to develop an AI-ML model for predicting rainfall in Ahmedabad for June, July, and August 2025. The code utilizes a RandomForestRegressor and includes data preprocessing, feature scaling, model training, evaluation, and visualization of results. The model predicts rainfall amounts of 213.850 mm for June, 333.575 mm for July, and 351.950 mm for August, showing a strong correlation between actual and predicted values.

Uploaded by

antratiwari04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

INDRASHIL UNIVERSITY

AT & PO; RAJPUR, TA: KADI, DI: MEHSANA-382740, GUJARAT, INDIA


Tel. No. (02764)278813-15, Fax No. (02764)278814
www.indrashiluniversity.edu.in

Students Name: Akshar Chavda (220110101005)


Jeet Upadhyaya (220110101056)
Course Name: Artificial Intelligence
Course Code: CSE603
Branch: B.Tech(CSE)
Year: 2024-2025
Semester: 6th
Problem Statement: Develop an AI-ML model capable of predicting
rainfall for the months of June, July and August 2025 in your residential
area.

1. Model Code:

import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_absolute_error
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Step 1: Load the CSV data


data = pd.read_csv('rainfall_ahmedabad.csv')

# Step 2: Preprocessing
# Convert month names to numbers
month_map = {'June': 6, 'July': 7, 'August': 8}
data['Month'] = data['Month'].map(month_map)

# Features and target


X = data[['Year', 'Month', 'Avg_Rain_1994_2023']]
y = data['Total_Rainfall_mm']

# Optional: Feature scaling


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Step 3: Train/Test Split


X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

# Step 4: Train model


model = RandomForestRegressor(n_estimators=200, random_state=42, max_depth=10)
model.fit(X_train, y_train)

# Step 5: Evaluate
y_pred = model.predict(X_test)
r2 = r2_score(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)

# Colored output
GREEN = "\033[92m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
RESET = "\033[0m"

B.Tech-CSE 2
print(f"{GREEN}Model R2 Score (Accuracy): {r2:.2f}{RESET}")
print(f"{YELLOW}Mean Absolute Error: {mae:.2f} mm{RESET}")

# Step 6: Check feature importance


print(f"\n{CYAN}Feature Importances:{RESET}")
importance = model.feature_importances_
for name, score in zip(['Year', 'Month', 'Avg_Rain_1994_2023'], importance):
print(f"{name}: {score:.3f}")

# Step 7: Predict rainfall for June, July, August 2025


future = pd.DataFrame({
'Year': [2025, 2025, 2025],
'Month': [6, 7, 8],
'Avg_Rain_1994_2023': [816, 816, 816] # Replace with appropriate value if needed
})
future_scaled = scaler.transform(future)
predictions = model.predict(future_scaled)
future['Predicted_Rainfall_mm'] = predictions

print(f"\n{GREEN}Rainfall Predictions for 2025:{RESET}\n")


print(future)

# Step 8a: Line plot actual vs predicted


plt.figure(figsize=(10, 6))
plt.plot(y_test.values, label="Actual", marker='o', color='blue')
plt.plot(y_pred, label="Predicted", linestyle='--', marker='x', color='red')
plt.legend()
plt.title("Actual vs Predicted Rainfall")
plt.xlabel("Sample Index")
plt.ylabel("Rainfall (mm)")
plt.grid(True)
plt.show()

# Step 8b: Bar graph for 2025 predictions


plt.figure(figsize=(8, 5))
plt.bar(future['Month'], future['Predicted_Rainfall_mm'], color=['#1f77b4', '#ff7f0e', '#2ca02c'])
plt.xticks(future['Month'], ['June', 'July', 'August'])
plt.title("Predicted Rainfall for Ahmedabad (2025)")
plt.xlabel("Month")
plt.ylabel("Predicted Rainfall (mm)")
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

B.Tech-CSE 3
2. Description of Code:

The code implements a sophisticated approach to rainfall prediction using machine learning
techniques. Here's a detailed explanation of the implementation:

The code begins by importing necessary libraries: pandas for data manipulation,
matplotlib.pyplot for visualization, sklearn.ensemble for the RandomForestRegressor model,
and other scikit-learn components for data splitting, evaluation metrics, and feature scaling.
The rainfall dataset for Ahmedabad is loaded using pandas.read_csv(). This dataset spans from
2010 to 2023, containing monthly rainfall data for June, July, and August - the primary
monsoon months in Ahmedabad. The implementation follows a structured approach:

1. Enhanced Preprocessing: The code maps month names to numerical values (June→6,
July→7, August→8) for better model understanding, and uses three features for
prediction: 'Year', 'Month', and 'Avg_Rain_1994_2023' (historical average rainfall of
816mm).
2. Feature Scaling: Unlike the previous approach, this implementation includes a
StandardScaler to normalize the feature values, which can improve model performance,
particularly when features have different scales.
3. Proper Data Splitting: The data is split into training (80%) and testing (20%) sets
using train_test_split, which allows for more reliable model evaluation.
4. Improved Model Configuration: The RandomForestRegressor is configured with 200
estimators (decision trees) and a maximum depth of 10, providing more robust
predictions through ensemble learning while preventing overfitting.
5. Comprehensive Model Evaluation: The model calculates multiple performance
metrics:
o R² score: Measures the proportion of variance explained by the model
o Mean Absolute Error (MAE): The average absolute difference between
predicted and actual values
6. Feature Importance Analysis: The code examines which features contribute most to
the predictions, providing insights into the rainfall patterns.
7. Future Predictions: The model generates predictions for June, July, and August 2025
after properly scaling the input features.
8. Visualization: The implementation includes two visualizations:
o A line chart comparing actual vs. predicted values for test data
o A color-coded bar chart showing the rainfall predictions for the three months in
2025

B.Tech-CSE 4
3. Model Dataset:

B.Tech-CSE 5
4. Model Output:

 Model Training Statistics:


o Model R2 Score (Accuracy): 0.94
o Mean Absolute Error: 7.59 mm

 Feature Importances:
o Year: 0.145
o Month: 0.855
o Avg_Rain_1994_2023: 0.000

 Rainfall Forecast for 2025:


o June: 213.850 mm
o July: 333.575 mm
o August: 351.950 mm

 Visualization of Model Performance:

The "Actual vs Predicted Rainfall" chart shows:

o Strong correlation between actual (blue line with circle markers) and predicted
(red dashed line with x markers) values
o Generally close prediction patterns across all test samples
o Slight underestimation in some cases, particularly at samples 4 and 5

B.Tech-CSE 6
 Rainfall Forecast for 2025:

The bar chart showing predicted rainfall for June, July, and August 2025 indicates:

o June (blue): 213.850 mm


o July (orange): 333.575 mm
o August (green): 351.950 mm

The model predicts that August 2025 will experience the highest rainfall in Ahmedabad
(351.950 mm), followed closely by July (333.575 mm), with June having significantly
less rainfall (213.850 mm). This pattern aligns with historical monsoon trends in the
region, where rainfall typically increases as the monsoon season progresses from June
through August.

B.Tech-CSE 7

You might also like