Spaces:
Sleeping
Sleeping
Commit ·
08d2769
1
Parent(s): 5755287
Upload 2 files
Browse files- app.py +67 -0
- knn_model.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 4 |
+
from sklearn.preprocessing import LabelEncoder
|
| 5 |
+
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
st.header("Predict Hanoi Weather")
|
| 10 |
+
lst_wind_d = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']
|
| 11 |
+
wind_directions = [
|
| 12 |
+
"North", "North-Northeast", "Northeast", "East-Northeast",
|
| 13 |
+
"East", "East-Southeast", "Southeast", "South-Southeast",
|
| 14 |
+
"South", "South-Southwest", "Southwest", "West-Southwest",
|
| 15 |
+
"West", "West-Northwest", "Northwest", "North-Northwest"]
|
| 16 |
+
def choose(choose):
|
| 17 |
+
if choose in wind_directions:
|
| 18 |
+
index = wind_directions.index(choose)
|
| 19 |
+
return lst_wind_d[index]
|
| 20 |
+
else:
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
TPmin = st.slider("Minimum Temperatures(Celsius)", 0, 100, 20)
|
| 24 |
+
Tpmax = st.slider("Maximum Temperatures(Celsius)", 0, 100, 30)
|
| 25 |
+
Wd = st.slider("Wind Speed(km/h)", 0, 150, 10)
|
| 26 |
+
wind_d = st.selectbox("Wind Direction", wind_directions)
|
| 27 |
+
Humidity = st.slider("Humidity(%)", 0, 100, 80)
|
| 28 |
+
Cloud = st.slider("Cloud(%)", 0, 100, 10)
|
| 29 |
+
pressure=st.slider("Pressure(Bar)", 900, 1050, 1000)
|
| 30 |
+
|
| 31 |
+
data_list = [[Tpmax, TPmin, Wd, wind_d, Humidity, Cloud, pressure]]
|
| 32 |
+
|
| 33 |
+
# Tạo DataFrame từ danh sách dữ liệu
|
| 34 |
+
df = pd.DataFrame(data_list, columns=["max", "min", "wind", "wind_d", "humidi", "cloud", "pressure"])
|
| 35 |
+
df["wind_d"] = df["wind_d"].astype('category')
|
| 36 |
+
|
| 37 |
+
def encode_data(encoders, df, feature):
|
| 38 |
+
encoder = LabelEncoder()
|
| 39 |
+
df[feature] = encoder.fit_transform(df[feature])
|
| 40 |
+
encoders[feature] = encoder
|
| 41 |
+
return encoders, df
|
| 42 |
+
|
| 43 |
+
numeric_features = ['max', 'min', 'wind', 'humidi', 'cloud', 'pressure']
|
| 44 |
+
categorical_features = ['wind_d']
|
| 45 |
+
label = 'rain'
|
| 46 |
+
|
| 47 |
+
scalers = {}
|
| 48 |
+
encoders = {}
|
| 49 |
+
|
| 50 |
+
df_processed = df.copy(deep=True)
|
| 51 |
+
for feature in categorical_features:
|
| 52 |
+
encoders, df_processed = encode_data(encoders, df_processed, feature)
|
| 53 |
+
|
| 54 |
+
with open('knn_model.pkl', 'rb') as model_file:
|
| 55 |
+
knn_model = pickle.load(model_file)
|
| 56 |
+
|
| 57 |
+
if Tpmax > TPmin:
|
| 58 |
+
btn = st.button("Predict")
|
| 59 |
+
if btn:
|
| 60 |
+
# Thực hiện dự đoán
|
| 61 |
+
predicted_class = knn_model.predict(df_processed)
|
| 62 |
+
if predicted_class[0] == 1:
|
| 63 |
+
st.success("Today, Hanoi has rain.")
|
| 64 |
+
else:
|
| 65 |
+
st.success("Today, Hanoi didn't have rain.")
|
| 66 |
+
else:
|
| 67 |
+
st.error("Maximum Temperature (TPmax) must be greater than Minimum Temperature (TPmin). Please adjust your inputs.")
|
knn_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5a6dac4eea540d72bc948a8da5f3b67e0c0ad7f2d34360617734f6f8cccf5362
|
| 3 |
+
size 262704
|