# -*- coding: utf-8 -*-
"""Untitled2.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1eBO4J_Yo79hY-4E0YhzbRtVHgQF9fPZA
"""

import pandas as pd
data = pd.read_csv("/content/Data1.csv")

data

data.isnull().sum()

import numpy as np
x = np.mean(data["TEM"])
y = np.mean(data["MXT"])
z = np.mean(data["MNT"])
s = np.mean(data["SLP"])
q = np.mean(data["RIN"])

data["TEM"].fillna(x,inplace=True)
data["MXT"].fillna(y,inplace=True)
data["MNT"].fillna(z,inplace=True)
data["SLP"].fillna(s,inplace=True)
data["RIN"].fillna(q,inplace=True)

data.isnull().sum()

import seaborn as sns
import matplotlib.pyplot as  plt
sns.lineplot(x=data["Year"],y=data["TEM"])
plt.show()

import seaborn as sns
import matplotlib.pyplot as  plt
sns.lineplot(x=data["Year"],y=data["SLP"])
plt.show()

data.describe()

cor = data[["TEM","MXT","SLP","MNT"]].corr

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPRegressor

X = data.drop(["TEM","Year","Month","Day"],axis=1)
Y = data["TEM"]

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2,random_state=210450)

X_scaled = X.copy()

for col in X.columns:
    X_scaled[col] = 0.1 + 0.8 * ((X[col] - X[col].min()) / (X[col].max() - X[col].min()))

X_scaled

X_train, X_test, Y_train, Y_test = train_test_split(X_scaled, Y, test_size = 0.2,random_state=210450)

import numpy as np
import pandas as pd
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
model = MLPRegressor(
    hidden_layer_sizes=(64, 64),   # two hidden layers, 64 neurons each
    activation='relu',
    solver='adam',
    learning_rate='adaptive',
    max_iter=2000,
    random_state=42
)

model.fit(X_train, Y_train)

y_pred = model.predict(X_test)

mse = mean_squared_error(Y_test, y_pred)
print("MSE:", mse)

from sklearn.metrics import r2_score, mean_squared_error

# Predictions
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

# Training accuracy
r2_train = r2_score(Y_train, y_train_pred)
mse_train = mean_squared_error(Y_train, y_train_pred)

# Testing accuracy
r2_test = r2_score(Y_test, y_test_pred)
mse_test = mean_squared_error(Y_test, y_test_pred)

print("=== TRAINING PERFORMANCE ===")
print("R² Score:", r2_train)
print("MSE:", mse_train)

print("\n=== TEST PERFORMANCE ===")
print("R² Score:", r2_test)
print("MSE:", mse_test)

import matplotlib.pyplot as plt
import numpy as np

# --- Predictions ---
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

# Combine for overall
y_all = np.concatenate([Y_train, Y_test])
y_all_pred = np.concatenate([y_train_pred, y_test_pred])

# -------------------------------------------------------------------
# Function to create scatter plot
# -------------------------------------------------------------------
def scatter_plot(actual, predicted, title):
    plt.figure(figsize=(6,6))
    plt.scatter(actual, predicted, alpha=0.6, edgecolor='k')
    # Perfect prediction line
    min_val = min(actual.min(), predicted.min())
    max_val = max(actual.max(), predicted.max())
    plt.plot([min_val, max_val], [min_val, max_val], 'r--', linewidth=2)

    plt.xlabel("Actual Maximum Rainfall")
    plt.ylabel("Predicted Maximum Rainfall")
    plt.title(title)
    plt.grid(True)
    plt.tight_layout()
    plt.show()

# -------------------------------------------------------------------
# Create the three required plots
# -------------------------------------------------------------------
scatter_plot(Y_train, y_train_pred, "Actual vs Predicted Maximum Rainfall (Training Data)")
scatter_plot(Y_test, y_test_pred, "Actual vs Predicted Maximum Rainfall (Test Data)")
scatter_plot(y_all, y_all_pred, "Actual vs Predicted Maximum Rainfall (Overall Data)")

