Suduk commited on
Commit
0633092
·
verified ·
1 Parent(s): c84c6f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -5,22 +5,21 @@ 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'])
@@ -28,18 +27,17 @@ def preprocess_input(input_data):
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)
@@ -53,17 +51,15 @@ 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}')
 
5
  from sklearn.preprocessing import LabelEncoder, PolynomialFeatures
6
 
7
  def preprocess_input(input_data):
8
+ #konvert input ke df
9
  if not isinstance(input_data, pd.DataFrame):
10
  input_data = pd.DataFrame([input_data])
11
 
12
+ #Pake label encoder
13
  label_encoder = LabelEncoder()
14
  input_data['cbwd'] = label_encoder.fit_transform(input_data['cbwd'])
15
 
16
+ #fitur 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
  poly = PolynomialFeatures(degree=2, include_bias=False)
24
  poly_features = poly.fit_transform(input_data[['DEWP', 'TEMP', 'PRES', 'Iws']])
25
  poly_feature_names = poly.get_feature_names_out(['DEWP', 'TEMP', 'PRES', 'Iws'])
 
27
 
28
  input_data = pd.concat([input_data, poly_df], axis=1)
29
 
30
+ #pilih fitur buat prediksi
31
  features = ['year', 'month', 'day', 'hour', 'DEWP', 'TEMP', 'PRES', 'cbwd', 'Iws', 'Is', 'Ir',
32
  'season', 'day_of_week', 'is_weekend', 'TEMP_Iws', 'DEWP_PRES'] + list(poly_feature_names)
33
 
34
  return input_data[features]
35
 
36
+ #loading model
37
  model = joblib.load('random_forest_predictor_pipeline_model.pkl')
38
 
39
+ st.title('Beijing PM2.5 Prediction')
40
 
 
41
  year = st.number_input('Year', min_value=2000, max_value=2050, value=2024)
42
  month = st.number_input('Month', min_value=1, max_value=12, value=1)
43
  day = st.number_input('Day', min_value=1, max_value=31, value=1)
 
51
  Ir = st.number_input('Ir', value=0.0)
52
 
53
  if st.button('Predict PM2.5'):
54
+ #tombol buat inp
55
  input_data = {
56
  'year': year, 'month': month, 'day': day, 'hour': hour,
57
  'DEWP': DEWP, 'TEMP': TEMP, 'PRES': PRES, 'cbwd': cbwd,
58
  'Iws': Iws, 'Is': Is, 'Ir': Ir
59
  }
60
 
61
+ #prediksi inputnya
62
  processed_input = preprocess_input(input_data)
 
 
63
  prediction = model.predict(processed_input)
64
 
65
  st.success(f'The predicted PM2.5 value is: {prediction[0]:.2f}')