Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
from sklearn.model_selection import train_test_split
|
|
|
|
| 6 |
from sklearn.metrics import accuracy_score
|
| 7 |
-
import pickle
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
import seaborn as sns
|
| 10 |
from PIL import Image
|
|
|
|
| 11 |
|
| 12 |
# Set page configuration
|
| 13 |
st.set_page_config(
|
|
@@ -17,281 +17,196 @@ st.set_page_config(
|
|
| 17 |
initial_sidebar_state="expanded"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Custom CSS for styling
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
.main {
|
| 24 |
-
background-color: #
|
| 25 |
}
|
| 26 |
.stButton>button {
|
| 27 |
-
background:
|
| 28 |
color: white;
|
| 29 |
-
border-radius:
|
| 30 |
border: none;
|
| 31 |
-
padding:
|
| 32 |
text-align: center;
|
| 33 |
text-decoration: none;
|
| 34 |
display: inline-block;
|
| 35 |
font-size: 16px;
|
| 36 |
-
margin:
|
| 37 |
cursor: pointer;
|
| 38 |
-
transition: all 0.3s;
|
| 39 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 40 |
}
|
| 41 |
.stButton>button:hover {
|
| 42 |
-
background:
|
| 43 |
-
box-shadow: 0 6px 10px rgba(0,0,0,0.15);
|
| 44 |
-
transform: translateY(-2px);
|
| 45 |
}
|
| 46 |
.stSelectbox, .stNumberInput, .stSlider {
|
| 47 |
background-color: white;
|
| 48 |
-
border-radius:
|
| 49 |
-
padding: 12px;
|
| 50 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
| 51 |
}
|
| 52 |
.css-1aumxhk {
|
| 53 |
-
background-color: #
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
}
|
| 57 |
-
.reportview-container .main .block-container {
|
| 58 |
-
padding-top: 2rem;
|
| 59 |
-
padding-bottom: 2rem;
|
| 60 |
-
}
|
| 61 |
-
.header {
|
| 62 |
-
font-size: 2.5em;
|
| 63 |
-
color: #2c3e50;
|
| 64 |
-
text-align: center;
|
| 65 |
-
margin-bottom: 0.5em;
|
| 66 |
-
background: linear-gradient(135deg, #4b6cb7, #182848);
|
| 67 |
-
-webkit-background-clip: text;
|
| 68 |
-
-webkit-text-fill-color: transparent;
|
| 69 |
-
font-weight: 700;
|
| 70 |
}
|
| 71 |
-
.
|
| 72 |
-
|
| 73 |
-
color: #5d6d7e;
|
| 74 |
text-align: center;
|
| 75 |
-
margin-bottom: 2em;
|
| 76 |
-
}
|
| 77 |
-
.sidebar .sidebar-content {
|
| 78 |
-
background: linear-gradient(180deg, #e0f7fa, #b2ebf2);
|
| 79 |
-
border-right: 1px solid #b2ebf2;
|
| 80 |
-
}
|
| 81 |
-
.st-bb {
|
| 82 |
-
background-color: white;
|
| 83 |
-
}
|
| 84 |
-
.st-at {
|
| 85 |
-
background-color: #4b6cb7;
|
| 86 |
-
}
|
| 87 |
-
.st-ae {
|
| 88 |
-
background-color: #f5f9ff;
|
| 89 |
-
}
|
| 90 |
-
.st-cg {
|
| 91 |
-
color: #4b6cb7;
|
| 92 |
-
}
|
| 93 |
-
.st-cn {
|
| 94 |
-
background-color: #4b6cb7;
|
| 95 |
-
}
|
| 96 |
-
.stTab {
|
| 97 |
-
background-color: #f5f9ff;
|
| 98 |
-
border-radius: 8px;
|
| 99 |
-
padding: 10px;
|
| 100 |
-
}
|
| 101 |
-
.stTab > div > div {
|
| 102 |
-
background: linear-gradient(135deg, #e0f7fa, #b2ebf2);
|
| 103 |
-
border-radius: 8px;
|
| 104 |
}
|
| 105 |
</style>
|
| 106 |
-
""", unsafe_allow_html=True)
|
| 107 |
-
|
| 108 |
-
# App header
|
| 109 |
-
st.markdown('<p class="header">🌧️ Rainfall Prediction Model</p>', unsafe_allow_html=True)
|
| 110 |
-
st.markdown('<p class="subheader">Predict whether it will rain tomorrow based on weather data</p>', unsafe_allow_html=True)
|
| 111 |
-
|
| 112 |
-
# Load or train model
|
| 113 |
-
@st.cache_resource
|
| 114 |
-
def load_model():
|
| 115 |
-
try:
|
| 116 |
-
# Try to load pre-trained model
|
| 117 |
-
model = pickle.load(open('rain_prediction_model.pkl', 'rb'))
|
| 118 |
-
return model
|
| 119 |
-
except:
|
| 120 |
-
# If no model exists, train a new one (for demo purposes)
|
| 121 |
-
# This would normally be done separately
|
| 122 |
-
from sklearn.datasets import make_classification
|
| 123 |
-
X, y = make_classification(n_samples=1000, n_features=10, random_state=42)
|
| 124 |
-
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 125 |
-
model.fit(X, y)
|
| 126 |
-
return model
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
#
|
| 131 |
@st.cache_data
|
| 132 |
def load_data():
|
|
|
|
|
|
|
| 133 |
data = {
|
| 134 |
-
'MinTemp': np.random.
|
| 135 |
-
'MaxTemp': np.random.
|
| 136 |
-
'Rainfall': np.random.
|
| 137 |
-
'Evaporation': np.random.
|
| 138 |
-
'Sunshine': np.random.
|
| 139 |
-
'
|
| 140 |
-
'
|
| 141 |
-
'
|
| 142 |
-
'
|
| 143 |
-
'
|
| 144 |
-
'
|
| 145 |
-
'
|
|
|
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
return pd.DataFrame(data)
|
| 148 |
|
| 149 |
df = load_data()
|
| 150 |
|
| 151 |
-
# Sidebar for
|
| 152 |
-
|
| 153 |
-
st.title("Navigation")
|
| 154 |
-
app_mode = st.selectbox("Choose a page", ["Prediction", "Data Exploration", "About"])
|
| 155 |
-
|
| 156 |
-
st.markdown("---")
|
| 157 |
-
st.markdown("### Weather Parameters")
|
| 158 |
-
st.markdown("Adjust the sliders to set weather conditions for prediction.")
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
with col2:
|
| 173 |
-
st.subheader("Additional Parameters")
|
| 174 |
-
|
| 175 |
-
sunshine = st.slider("Sunshine (hours)", 0.0, 14.0, 8.0)
|
| 176 |
-
wind_gust = st.slider("Wind Gust Speed (km/h)", 30, 100, 50)
|
| 177 |
-
humidity_9am = st.slider("Humidity at 9am (%)", 0, 100, 70)
|
| 178 |
-
humidity_3pm = st.slider("Humidity at 3pm (%)", 0, 100, 50)
|
| 179 |
-
pressure_9am = st.slider("Pressure at 9am (hPa)", 990.0, 1040.0, 1015.0)
|
| 180 |
-
pressure_3pm = st.slider("Pressure at 3pm (hPa)", 990.0, 1040.0, 1013.0)
|
| 181 |
-
|
| 182 |
-
# Prediction button
|
| 183 |
-
if st.button("Predict Rainfall Tomorrow"):
|
| 184 |
-
# Prepare input data
|
| 185 |
-
input_data = np.array([[min_temp, max_temp, rainfall, evaporation, sunshine,
|
| 186 |
-
wind_gust, humidity_9am, humidity_3pm,
|
| 187 |
-
pressure_9am, pressure_3pm]])
|
| 188 |
-
|
| 189 |
-
# Make prediction
|
| 190 |
-
prediction = model.predict(input_data)
|
| 191 |
-
prediction_proba = model.predict_proba(input_data)
|
| 192 |
-
|
| 193 |
-
# Display results
|
| 194 |
-
st.markdown("---")
|
| 195 |
-
st.subheader("Prediction Results")
|
| 196 |
-
|
| 197 |
-
col_result1, col_result2 = st.columns([1, 1])
|
| 198 |
-
|
| 199 |
-
with col_result1:
|
| 200 |
-
if prediction[0] == 1:
|
| 201 |
-
st.markdown("### 🌧️ Result: It will rain tomorrow!")
|
| 202 |
-
st.image("https://cdn-icons-png.flaticon.com/512/4150/4150904.png", width=150)
|
| 203 |
-
else:
|
| 204 |
-
st.markdown("### ☀️ Result: No rain expected tomorrow!")
|
| 205 |
-
st.image("https://cdn-icons-png.flaticon.com/512/3222/3222807.png", width=150)
|
| 206 |
-
|
| 207 |
-
with col_result2:
|
| 208 |
-
st.markdown("### Probability")
|
| 209 |
-
st.write(f"Probability of rain: {prediction_proba[0][1]*100:.2f}%")
|
| 210 |
-
st.write(f"Probability of no rain: {prediction_proba[0][0]*100:.2f}%")
|
| 211 |
-
|
| 212 |
-
# Create a gauge chart with new colors
|
| 213 |
-
fig, ax = plt.subplots(figsize=(6, 1))
|
| 214 |
-
ax.barh(['Rain Probability'], [prediction_proba[0][1]], color='#6e8efb')
|
| 215 |
-
ax.set_xlim(0, 1)
|
| 216 |
-
ax.set_xticks([])
|
| 217 |
-
ax.text(0.5, 0, f"{prediction_proba[0][1]*100:.1f}%",
|
| 218 |
-
ha='center', va='center', color='white', fontsize=12,
|
| 219 |
-
bbox=dict(facecolor='rgba(0,0,0,0.2)', edgecolor='none', boxstyle='round,pad=0.2'))
|
| 220 |
-
st.pyplot(fig)
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
with tab2:
|
| 235 |
-
st.write("### Temperature Distribution")
|
| 236 |
-
fig, ax = plt.subplots()
|
| 237 |
-
sns.histplot(df['MaxTemp'], kde=True, ax=ax, color='#4b6cb7')
|
| 238 |
-
ax.set_facecolor('#f5f9ff')
|
| 239 |
-
fig.patch.set_facecolor('#f5f9ff')
|
| 240 |
-
st.pyplot(fig)
|
| 241 |
-
|
| 242 |
-
st.write("### Rainfall vs. Humidity")
|
| 243 |
-
fig, ax = plt.subplots()
|
| 244 |
-
sns.scatterplot(data=df, x='Humidity9am', y='Rainfall', hue='RainTomorrow',
|
| 245 |
-
palette=['#a777e3', '#6e8efb'], ax=ax)
|
| 246 |
-
ax.set_facecolor('#f5f9ff')
|
| 247 |
-
fig.patch.set_facecolor('#f5f9ff')
|
| 248 |
-
st.pyplot(fig)
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
cbar_kws={'label': 'Correlation Coefficient'})
|
| 255 |
-
ax.set_facecolor('#f5f9ff')
|
| 256 |
-
fig.patch.set_facecolor('#f5f9ff')
|
| 257 |
-
st.pyplot(fig)
|
| 258 |
-
|
| 259 |
-
st.write("### Rain Tomorrow Distribution")
|
| 260 |
-
fig, ax = plt.subplots()
|
| 261 |
-
df['RainTomorrow'].value_counts().plot(kind='bar', color=['#6e8efb', '#a777e3'], ax=ax)
|
| 262 |
-
ax.set_facecolor('#f5f9ff')
|
| 263 |
-
fig.patch.set_facecolor('#f5f9ff')
|
| 264 |
-
st.pyplot(fig)
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
- Data exploration and visualization tools
|
| 276 |
-
- Beautiful and responsive UI design
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
- Output: Probability of rain tomorrow
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
-
This
|
|
|
|
| 289 |
""")
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
from sklearn.metrics import accuracy_score
|
|
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import seaborn as sns
|
| 9 |
from PIL import Image
|
| 10 |
+
import pickle
|
| 11 |
|
| 12 |
# Set page configuration
|
| 13 |
st.set_page_config(
|
|
|
|
| 17 |
initial_sidebar_state="expanded"
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Custom CSS for styling
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
.main {
|
| 24 |
+
background-color: #f0f2f6;
|
| 25 |
}
|
| 26 |
.stButton>button {
|
| 27 |
+
background-color: #4CAF50;
|
| 28 |
color: white;
|
| 29 |
+
border-radius: 5px;
|
| 30 |
border: none;
|
| 31 |
+
padding: 10px 24px;
|
| 32 |
text-align: center;
|
| 33 |
text-decoration: none;
|
| 34 |
display: inline-block;
|
| 35 |
font-size: 16px;
|
| 36 |
+
margin: 4px 2px;
|
| 37 |
cursor: pointer;
|
|
|
|
|
|
|
| 38 |
}
|
| 39 |
.stButton>button:hover {
|
| 40 |
+
background-color: #45a049;
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
.stSelectbox, .stNumberInput, .stSlider {
|
| 43 |
background-color: white;
|
| 44 |
+
border-radius: 5px;
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
.css-1aumxhk {
|
| 47 |
+
background-color: #e8f4fc;
|
| 48 |
+
border-radius: 10px;
|
| 49 |
+
padding: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
+
.title {
|
| 52 |
+
color: #1e3d6b;
|
|
|
|
| 53 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
</style>
|
| 56 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Title and description
|
| 59 |
+
st.title("🌧️ Rainfall Prediction App")
|
| 60 |
+
st.markdown("""
|
| 61 |
+
Predict the probability of rainfall tomorrow based on weather data.
|
| 62 |
+
This machine learning model uses Random Forest Classifier for accurate predictions.
|
| 63 |
+
""")
|
| 64 |
|
| 65 |
+
# Load sample data or create synthetic data
|
| 66 |
@st.cache_data
|
| 67 |
def load_data():
|
| 68 |
+
# In a real app, you would load your actual dataset here
|
| 69 |
+
# This is synthetic data for demonstration
|
| 70 |
data = {
|
| 71 |
+
'MinTemp': np.random.uniform(5, 25, 1000),
|
| 72 |
+
'MaxTemp': np.random.uniform(15, 40, 1000),
|
| 73 |
+
'Rainfall': np.random.uniform(0, 50, 1000),
|
| 74 |
+
'Evaporation': np.random.uniform(0, 20, 1000),
|
| 75 |
+
'Sunshine': np.random.uniform(0, 14, 1000),
|
| 76 |
+
'Humidity9am': np.random.randint(20, 100, 1000),
|
| 77 |
+
'Humidity3pm': np.random.randint(20, 100, 1000),
|
| 78 |
+
'Pressure9am': np.random.uniform(990, 1040, 1000),
|
| 79 |
+
'Pressure3pm': np.random.uniform(990, 1040, 1000),
|
| 80 |
+
'Cloud9am': np.random.randint(0, 9, 1000),
|
| 81 |
+
'Cloud3pm': np.random.randint(0, 9, 1000),
|
| 82 |
+
'Temp9am': np.random.uniform(10, 35, 1000),
|
| 83 |
+
'Temp3pm': np.random.uniform(15, 40, 1000),
|
| 84 |
+
'RainToday': np.random.choice([0, 1], 1000, p=[0.7, 0.3]),
|
| 85 |
+
'RainTomorrow': np.random.choice([0, 1], 1000, p=[0.7, 0.3])
|
| 86 |
}
|
| 87 |
return pd.DataFrame(data)
|
| 88 |
|
| 89 |
df = load_data()
|
| 90 |
|
| 91 |
+
# Sidebar for user input
|
| 92 |
+
st.sidebar.header("User Input Features")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Function to get user input
|
| 95 |
+
def user_input_features():
|
| 96 |
+
min_temp = st.sidebar.slider('Minimum Temperature (°C)', 0.0, 30.0, 15.0)
|
| 97 |
+
max_temp = st.sidebar.slider('Maximum Temperature (°C)', 10.0, 45.0, 25.0)
|
| 98 |
+
rainfall = st.sidebar.slider('Rainfall (mm)', 0.0, 100.0, 0.0)
|
| 99 |
+
humidity_9am = st.sidebar.slider('Humidity at 9am (%)', 0, 100, 60)
|
| 100 |
+
humidity_3pm = st.sidebar.slider('Humidity at 3pm (%)', 0, 100, 50)
|
| 101 |
+
pressure_9am = st.sidebar.slider('Pressure at 9am (hPa)', 990.0, 1040.0, 1015.0)
|
| 102 |
+
pressure_3pm = st.sidebar.slider('Pressure at 3pm (hPa)', 990.0, 1040.0, 1015.0)
|
| 103 |
+
cloud_9am = st.sidebar.slider('Cloud cover at 9am (oktas)', 0, 8, 3)
|
| 104 |
+
cloud_3pm = st.sidebar.slider('Cloud cover at 3pm (oktas)', 0, 8, 4)
|
| 105 |
+
rain_today = st.sidebar.selectbox('Rain Today?', ('No', 'Yes'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
data = {
|
| 108 |
+
'MinTemp': min_temp,
|
| 109 |
+
'MaxTemp': max_temp,
|
| 110 |
+
'Rainfall': rainfall,
|
| 111 |
+
'Humidity9am': humidity_9am,
|
| 112 |
+
'Humidity3pm': humidity_3pm,
|
| 113 |
+
'Pressure9am': pressure_9am,
|
| 114 |
+
'Pressure3pm': pressure_3pm,
|
| 115 |
+
'Cloud9am': cloud_9am,
|
| 116 |
+
'Cloud3pm': cloud_3pm,
|
| 117 |
+
'RainToday': 1 if rain_today == 'Yes' else 0
|
| 118 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
features = pd.DataFrame(data, index=[0])
|
| 121 |
+
return features
|
| 122 |
+
|
| 123 |
+
input_df = user_input_features()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# Main panel
|
| 126 |
+
st.subheader("User Input Parameters")
|
| 127 |
+
st.write(input_df)
|
| 128 |
+
|
| 129 |
+
# Train model
|
| 130 |
+
@st.cache_resource
|
| 131 |
+
def train_model():
|
| 132 |
+
# Prepare data
|
| 133 |
+
X = df[['MinTemp', 'MaxTemp', 'Rainfall', 'Humidity9am', 'Humidity3pm',
|
| 134 |
+
'Pressure9am', 'Pressure3pm', 'Cloud9am', 'Cloud3pm', 'RainToday']]
|
| 135 |
+
y = df['RainTomorrow']
|
| 136 |
|
| 137 |
+
# Train-test split
|
| 138 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 139 |
|
| 140 |
+
# Train model
|
| 141 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 142 |
+
model.fit(X_train, y_train)
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# Evaluate model
|
| 145 |
+
y_pred = model.predict(X_test)
|
| 146 |
+
accuracy = accuracy_score(y_test, y_pred)
|
|
|
|
| 147 |
|
| 148 |
+
return model, accuracy
|
| 149 |
+
|
| 150 |
+
model, accuracy = train_model()
|
| 151 |
+
|
| 152 |
+
# Make predictions
|
| 153 |
+
prediction = model.predict(input_df)
|
| 154 |
+
prediction_proba = model.predict_proba(input_df)
|
| 155 |
+
|
| 156 |
+
# Display results
|
| 157 |
+
st.subheader('Prediction')
|
| 158 |
+
rain_tomorrow = 'Yes' if prediction[0] == 1 else 'No'
|
| 159 |
+
st.markdown(f"<h3 style='text-align: center; color: {'red' if rain_tomorrow == 'Yes' else 'green'};'>"
|
| 160 |
+
f"Will it rain tomorrow? {rain_tomorrow}</h3>",
|
| 161 |
+
unsafe_allow_html=True)
|
| 162 |
+
|
| 163 |
+
st.subheader('Prediction Probability')
|
| 164 |
+
st.write(f"Probability of rain tomorrow: {prediction_proba[0][1]*100:.2f}%")
|
| 165 |
+
st.write(f"Probability of no rain tomorrow: {prediction_proba[0][0]*100:.2f}%")
|
| 166 |
+
|
| 167 |
+
# Show model accuracy
|
| 168 |
+
st.sidebar.subheader('Model Accuracy')
|
| 169 |
+
st.sidebar.write(f"Accuracy: {accuracy*100:.2f}%")
|
| 170 |
+
|
| 171 |
+
# Visualization section
|
| 172 |
+
st.subheader("Data Visualization")
|
| 173 |
+
|
| 174 |
+
col1, col2 = st.columns(2)
|
| 175 |
+
|
| 176 |
+
with col1:
|
| 177 |
+
st.write("**Temperature Distribution**")
|
| 178 |
+
fig, ax = plt.subplots()
|
| 179 |
+
sns.histplot(df['MaxTemp'], kde=True, ax=ax)
|
| 180 |
+
plt.xlabel('Maximum Temperature (°C)')
|
| 181 |
+
st.pyplot(fig)
|
| 182 |
+
|
| 183 |
+
with col2:
|
| 184 |
+
st.write("**Humidity at 9am vs 3pm**")
|
| 185 |
+
fig, ax = plt.subplots()
|
| 186 |
+
sns.scatterplot(x='Humidity9am', y='Humidity3pm', hue='RainTomorrow', data=df, ax=ax)
|
| 187 |
+
plt.xlabel('Humidity at 9am (%)')
|
| 188 |
+
plt.ylabel('Humidity at 3pm (%)')
|
| 189 |
+
st.pyplot(fig)
|
| 190 |
+
|
| 191 |
+
# Add some information
|
| 192 |
+
st.markdown("""
|
| 193 |
+
### About This App
|
| 194 |
+
This app predicts the probability of rainfall tomorrow using weather data.
|
| 195 |
+
The model was trained using Random Forest Classifier on historical weather data.
|
| 196 |
|
| 197 |
+
**Note:** This is a demonstration app using synthetic data. For real-world applications,
|
| 198 |
+
you should train the model with actual weather data from your region.
|
| 199 |
""")
|
| 200 |
+
|
| 201 |
+
# Add download button for sample data
|
| 202 |
+
@st.cache_data
|
| 203 |
+
def convert_df(df):
|
| 204 |
+
return df.to_csv().encode('utf-8')
|
| 205 |
+
|
| 206 |
+
csv = convert_df(df.head(100))
|
| 207 |
+
st.download_button(
|
| 208 |
+
label="Download Sample Data (CSV)",
|
| 209 |
+
data=csv,
|
| 210 |
+
file_name='sample_weather_data.csv',
|
| 211 |
+
mime='text/csv'
|
| 212 |
+
)
|