Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
import pandas as pd
|
5 |
+
from sklearn.preprocessing import LabelEncoder, PolynomialFeatures
|
6 |
+
|
7 |
+
def preprocess_input(input_data):
|
8 |
+
# Convert input_data to DataFrame if it's not already
|
9 |
+
if not isinstance(input_data, pd.DataFrame):
|
10 |
+
input_data = pd.DataFrame([input_data])
|
11 |
+
|
12 |
+
# Label encoding for 'cbwd'
|
13 |
+
label_encoder = LabelEncoder()
|
14 |
+
input_data['cbwd'] = label_encoder.fit_transform(input_data['cbwd'])
|
15 |
+
|
16 |
+
# Feature engineering
|
17 |
+
input_data['season'] = input_data['month'].apply(lambda x: (x % 12 + 3) // 3)
|
18 |
+
input_data['day_of_week'] = pd.to_datetime(input_data[['year', 'month', 'day']]).dt.dayofweek
|
19 |
+
input_data['is_weekend'] = input_data['day_of_week'].apply(lambda x: 1 if x >= 5 else 0)
|
20 |
+
input_data['TEMP_Iws'] = input_data['TEMP'] * input_data['Iws']
|
21 |
+
input_data['DEWP_PRES'] = input_data['DEWP'] * input_data['PRES']
|
22 |
+
|
23 |
+
# Polynomial features
|
24 |
+
poly = PolynomialFeatures(degree=2, include_bias=False)
|
25 |
+
poly_features = poly.fit_transform(input_data[['DEWP', 'TEMP', 'PRES', 'Iws']])
|
26 |
+
poly_feature_names = poly.get_feature_names_out(['DEWP', 'TEMP', 'PRES', 'Iws'])
|
27 |
+
poly_df = pd.DataFrame(poly_features, columns=poly_feature_names, index=input_data.index)
|
28 |
+
|
29 |
+
input_data = pd.concat([input_data, poly_df], axis=1)
|
30 |
+
|
31 |
+
# Select features for prediction
|
32 |
+
features = ['year', 'month', 'day', 'hour', 'DEWP', 'TEMP', 'PRES', 'cbwd', 'Iws', 'Is', 'Ir',
|
33 |
+
'season', 'day_of_week', 'is_weekend', 'TEMP_Iws', 'DEWP_PRES'] + list(poly_feature_names)
|
34 |
+
|
35 |
+
return input_data[features]
|
36 |
+
|
37 |
+
# Load the saved model
|
38 |
+
model = joblib.load('random_forest_predictor_pipeline_model.pkl')
|
39 |
+
|
40 |
+
st.title('PM2.5 Prediction App')
|
41 |
+
|
42 |
+
# Create input fields for all required features
|
43 |
+
year = st.number_input('Year', min_value=2000, max_value=2050, value=2024)
|
44 |
+
month = st.number_input('Month', min_value=1, max_value=12, value=1)
|
45 |
+
day = st.number_input('Day', min_value=1, max_value=31, value=1)
|
46 |
+
hour = st.number_input('Hour', min_value=0, max_value=23, value=0)
|
47 |
+
DEWP = st.number_input('DEWP', value=0.0)
|
48 |
+
TEMP = st.number_input('TEMP', value=0.0)
|
49 |
+
PRES = st.number_input('PRES', value=1000.0)
|
50 |
+
cbwd = st.selectbox('cbwd', ['NE', 'SE', 'NW', 'cv'])
|
51 |
+
Iws = st.number_input('Iws', value=0.0)
|
52 |
+
Is = st.number_input('Is', value=0.0)
|
53 |
+
Ir = st.number_input('Ir', value=0.0)
|
54 |
+
|
55 |
+
if st.button('Predict PM2.5'):
|
56 |
+
# Create a dictionary with the input values
|
57 |
+
input_data = {
|
58 |
+
'year': year, 'month': month, 'day': day, 'hour': hour,
|
59 |
+
'DEWP': DEWP, 'TEMP': TEMP, 'PRES': PRES, 'cbwd': cbwd,
|
60 |
+
'Iws': Iws, 'Is': Is, 'Ir': Ir
|
61 |
+
}
|
62 |
+
|
63 |
+
# Preprocess the input data
|
64 |
+
processed_input = preprocess_input(input_data)
|
65 |
+
|
66 |
+
# Make prediction
|
67 |
+
prediction = model.predict(processed_input)
|
68 |
+
|
69 |
+
st.success(f'The predicted PM2.5 value is: {prediction[0]:.2f}')
|
70 |
+
|
71 |
+
|