Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 7 |
+
from tensorflow.keras.models import Sequential
|
| 8 |
+
from tensorflow.keras.layers import LSTM, Dense, Dropout
|
| 9 |
+
|
| 10 |
+
# Load the dataset
|
| 11 |
+
df = pd.read_csv("/content/TEMP_ANNUAL_SEASONAL_MEAN.csv")
|
| 12 |
+
|
| 13 |
+
# Convert 'YEAR' column to datetime
|
| 14 |
+
df.columns = df.columns.str.upper() # Ensure column names are uppercase
|
| 15 |
+
df['YEAR'] = pd.to_datetime(df['YEAR'], format='%Y')
|
| 16 |
+
df.set_index('YEAR', inplace=True)
|
| 17 |
+
|
| 18 |
+
# Selecting the relevant temperature column
|
| 19 |
+
temp_col = 'ANNUAL'
|
| 20 |
+
df[temp_col] = pd.to_numeric(df[temp_col], errors='coerce') # Convert to numeric, handle errors
|
| 21 |
+
data = df[[temp_col]].dropna()
|
| 22 |
+
|
| 23 |
+
# Normalize the data
|
| 24 |
+
scaler = MinMaxScaler()
|
| 25 |
+
data_scaled = scaler.fit_transform(data)
|
| 26 |
+
|
| 27 |
+
# Prepare sequences for LSTM
|
| 28 |
+
sequence_length = 10 # Using past 10 years to predict next year
|
| 29 |
+
def create_sequences(data, seq_length):
|
| 30 |
+
X, y = [], []
|
| 31 |
+
for i in range(len(data) - seq_length):
|
| 32 |
+
X.append(data[i:i + seq_length])
|
| 33 |
+
y.append(data[i + seq_length])
|
| 34 |
+
return np.array(X), np.array(y)
|
| 35 |
+
|
| 36 |
+
X, y = create_sequences(data_scaled, sequence_length)
|
| 37 |
+
|
| 38 |
+
# Split data into training & testing sets
|
| 39 |
+
split = int(len(X) * 0.8)
|
| 40 |
+
X_train, X_test = X[:split], X[split:]
|
| 41 |
+
y_train, y_test = y[:split], y[split:]
|
| 42 |
+
|
| 43 |
+
# Define LSTM model
|
| 44 |
+
model = Sequential([
|
| 45 |
+
LSTM(100, return_sequences=True, input_shape=(sequence_length, 1)),
|
| 46 |
+
Dropout(0.2),
|
| 47 |
+
LSTM(100, return_sequences=False),
|
| 48 |
+
Dropout(0.2),
|
| 49 |
+
Dense(50, activation='relu'),
|
| 50 |
+
Dense(1)
|
| 51 |
+
])
|
| 52 |
+
|
| 53 |
+
model.compile(optimizer='adam', loss='mse')
|
| 54 |
+
|
| 55 |
+
# Train the model
|
| 56 |
+
model.fit(X_train, y_train, epochs=50, batch_size=8, validation_data=(X_test, y_test))
|
| 57 |
+
|
| 58 |
+
# Make predictions
|
| 59 |
+
predictions = model.predict(X_test)
|
| 60 |
+
predictions = scaler.inverse_transform(predictions) # Convert back to original scale
|
| 61 |
+
|
| 62 |
+
# Streamlit UI
|
| 63 |
+
st.title("India Climate Change Forecast")
|
| 64 |
+
|
| 65 |
+
st.write("### Actual vs Predicted Annual Mean Temperature")
|
| 66 |
+
|
| 67 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 68 |
+
ax.plot(df.index[-len(y_test):], scaler.inverse_transform(y_test.reshape(-1, 1)), label="Actual")
|
| 69 |
+
ax.plot(df.index[-len(y_test):], predictions, label="Predicted", linestyle='dashed')
|
| 70 |
+
ax.set_xlabel("Year")
|
| 71 |
+
ax.set_ylabel("Temperature (°C)")
|
| 72 |
+
ax.set_title("Actual vs Predicted Annual Mean Temperature in India")
|
| 73 |
+
ax.legend()
|
| 74 |
+
st.pyplot(fig)
|