File size: 2,691 Bytes
30da874
 
 
 
 
 
 
0633092
30da874
 
 
0633092
30da874
 
 
0633092
30da874
 
 
 
 
 
 
 
 
 
 
 
 
0633092
30da874
 
 
 
 
0633092
30da874
 
0633092
30da874
 
 
 
 
 
 
 
 
 
 
 
 
 
0633092
30da874
 
 
 
 
 
0633092
30da874
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import pandas as pd
import joblib
import pandas as pd
from sklearn.preprocessing import LabelEncoder, PolynomialFeatures

def preprocess_input(input_data):
    #konvert input ke df
    if not isinstance(input_data, pd.DataFrame):
        input_data = pd.DataFrame([input_data])
    
    #Pake label encoder
    label_encoder = LabelEncoder()
    input_data['cbwd'] = label_encoder.fit_transform(input_data['cbwd'])
    
    #fitur engineering
    input_data['season'] = input_data['month'].apply(lambda x: (x % 12 + 3) // 3)
    input_data['day_of_week'] = pd.to_datetime(input_data[['year', 'month', 'day']]).dt.dayofweek
    input_data['is_weekend'] = input_data['day_of_week'].apply(lambda x: 1 if x >= 5 else 0)
    input_data['TEMP_Iws'] = input_data['TEMP'] * input_data['Iws']
    input_data['DEWP_PRES'] = input_data['DEWP'] * input_data['PRES']
    
    poly = PolynomialFeatures(degree=2, include_bias=False)
    poly_features = poly.fit_transform(input_data[['DEWP', 'TEMP', 'PRES', 'Iws']])
    poly_feature_names = poly.get_feature_names_out(['DEWP', 'TEMP', 'PRES', 'Iws'])
    poly_df = pd.DataFrame(poly_features, columns=poly_feature_names, index=input_data.index)
    
    input_data = pd.concat([input_data, poly_df], axis=1)
    
    #pilih fitur buat prediksi
    features = ['year', 'month', 'day', 'hour', 'DEWP', 'TEMP', 'PRES', 'cbwd', 'Iws', 'Is', 'Ir',
                'season', 'day_of_week', 'is_weekend', 'TEMP_Iws', 'DEWP_PRES'] + list(poly_feature_names)
    
    return input_data[features]

#loading model
model = joblib.load('random_forest_predictor_pipeline_model.pkl')

st.title('Beijing PM2.5 Prediction')

year = st.number_input('Year', min_value=2000, max_value=2050, value=2024)
month = st.number_input('Month', min_value=1, max_value=12, value=1)
day = st.number_input('Day', min_value=1, max_value=31, value=1)
hour = st.number_input('Hour', min_value=0, max_value=23, value=0)
DEWP = st.number_input('DEWP', value=0.0)
TEMP = st.number_input('TEMP', value=0.0)
PRES = st.number_input('PRES', value=1000.0)
cbwd = st.selectbox('cbwd', ['NE', 'SE', 'NW', 'cv'])
Iws = st.number_input('Iws', value=0.0)
Is = st.number_input('Is', value=0.0)
Ir = st.number_input('Ir', value=0.0)

if st.button('Predict PM2.5'):
    #tombol buat inp
    input_data = {
        'year': year, 'month': month, 'day': day, 'hour': hour,
        'DEWP': DEWP, 'TEMP': TEMP, 'PRES': PRES, 'cbwd': cbwd,
        'Iws': Iws, 'Is': Is, 'Ir': Ir
    }
    
    #prediksi inputnya
    processed_input = preprocess_input(input_data)
    prediction = model.predict(processed_input)
    
    st.success(f'The predicted PM2.5 value is: {prediction[0]:.2f}')