ARBAJSSHAIKH commited on
Commit
153d4bf
·
verified ·
1 Parent(s): 98eae6e

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +64 -64
utils.py CHANGED
@@ -1,64 +1,64 @@
1
- import streamlit as st
2
- import pandas as pd
3
- from datetime import datetime
4
- import joblib
5
- import numpy as np
6
- import random
7
- import os
8
- from keras.models import Sequential
9
- from keras.layers import Dense, LSTM
10
- from sklearn.preprocessing import MinMaxScaler
11
- from sklearn.tree import DecisionTreeRegressor
12
- from statsmodels.tsa.arima.model import ARIMA
13
- import tensorflow as tf
14
- import utils
15
-
16
-
17
- def predict_medicine_demand(location: str, year: int, month: int):
18
- results = []
19
- products=('Medicine_4', 'Medicine_10', 'Medicine_5', 'Medicine_7','Medicine_3', 'Medicine_2', 'Medicine_8', 'Medicine_1','Medicine_6', 'Medicine_9')
20
- for product in products:
21
- try:
22
- monthly_data=pd.read_csv('monthly_data.csv')
23
- # Get last 3 values from the data
24
- ts_df = monthly_data[(monthly_data['product'] == product) & (monthly_data['location'] == location)].copy()
25
- ts_df.set_index('date', inplace=True)
26
- ts_df = ts_df.sort_index()
27
- ts = ts_df['quantity']
28
- if len(ts) < 6:
29
- continue
30
-
31
- # Load ARIMA model
32
- arima_model = joblib.load(f"models/arima_{product}_{location}.pkl")
33
- arima_forecast = arima_model.forecast(steps=1)[0]
34
-
35
- # Load ANN
36
- ann_model = tf.keras.models.load_model(f"models/ann_{product}_{location}.h5")
37
- scaler_X = joblib.load(f"models/ann_scalerX_{product}_{location}.pkl")
38
- scaler_y = joblib.load(f"models/ann_scalerY_{product}_{location}.pkl")
39
-
40
- last_3 = ts[-3:].values.reshape(1, -1)
41
- last_scaled = scaler_X.transform(last_3)
42
- ann_pred_scaled = ann_model.predict(last_scaled, verbose=0)
43
- ann_forecast = scaler_y.inverse_transform(ann_pred_scaled)[0][0]
44
-
45
- # Load LSTM
46
- lstm_model = tf.keras.models.load_model(f"models/lstm_{product}_{location}.h5")
47
- lstm_scaler = joblib.load(f"models/lstm_scaler_{product}_{location}.pkl")
48
-
49
- series_scaled = lstm_scaler.transform(ts.values.reshape(-1, 1)).flatten()
50
- last_seq = series_scaled[-3:].reshape(1, 3, 1)
51
- lstm_pred_scaled = lstm_model.predict(last_seq, verbose=0)[0][0]
52
- lstm_forecast = lstm_scaler.inverse_transform([[lstm_pred_scaled]])[0][0]
53
-
54
- results.append({
55
- "Product": product,
56
- "ARIMA Pred": round(arima_forecast, 2),
57
- "ANN Pred": round(ann_forecast, 2),
58
- "LSTM Pred": round(lstm_forecast, 2),
59
- "Average Requirement":round((arima_forecast+ann_forecast+lstm_forecast)/3,2)
60
- })
61
-
62
- except Exception as e:
63
- print(f"Failed to predict for {product} - {location}: {e}")
64
- return pd.DataFrame(results)
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from datetime import datetime
4
+ import joblib
5
+ import numpy as np
6
+ import random
7
+ import os
8
+ from keras.models import Sequential
9
+ from keras.layers import Dense, LSTM
10
+ from sklearn.preprocessing import MinMaxScaler
11
+ from sklearn.tree import DecisionTreeRegressor
12
+ from statsmodels.tsa.arima.model import ARIMA
13
+ import tensorflow as tf
14
+ import utils
15
+
16
+ monthly_data=pd.read_csv('monthly_data.csv')
17
+ def predict_medicine_demand(location: str, year: int, month: int):
18
+ results = []
19
+ products=('Medicine_4', 'Medicine_10', 'Medicine_5', 'Medicine_7','Medicine_3', 'Medicine_2', 'Medicine_8', 'Medicine_1','Medicine_6', 'Medicine_9')
20
+ for product in products:
21
+ try:
22
+
23
+ # Get last 3 values from the data
24
+ ts_df = monthly_data[(monthly_data['product'] == product) & (monthly_data['location'] == location)].copy()
25
+ ts_df.set_index('date', inplace=True)
26
+ ts_df = ts_df.sort_index()
27
+ ts = ts_df['quantity']
28
+ if len(ts) < 6:
29
+ continue
30
+
31
+ # Load ARIMA model
32
+ arima_model = joblib.load(f"models/arima_{product}_{location}.pkl")
33
+ arima_forecast = arima_model.forecast(steps=1)[0]
34
+
35
+ # Load ANN
36
+ ann_model = tf.keras.models.load_model(f"models/ann_{product}_{location}.h5")
37
+ scaler_X = joblib.load(f"models/ann_scalerX_{product}_{location}.pkl")
38
+ scaler_y = joblib.load(f"models/ann_scalerY_{product}_{location}.pkl")
39
+
40
+ last_3 = ts[-3:].values.reshape(1, -1)
41
+ last_scaled = scaler_X.transform(last_3)
42
+ ann_pred_scaled = ann_model.predict(last_scaled, verbose=0)
43
+ ann_forecast = scaler_y.inverse_transform(ann_pred_scaled)[0][0]
44
+
45
+ # Load LSTM
46
+ lstm_model = tf.keras.models.load_model(f"models/lstm_{product}_{location}.h5")
47
+ lstm_scaler = joblib.load(f"models/lstm_scaler_{product}_{location}.pkl")
48
+
49
+ series_scaled = lstm_scaler.transform(ts.values.reshape(-1, 1)).flatten()
50
+ last_seq = series_scaled[-3:].reshape(1, 3, 1)
51
+ lstm_pred_scaled = lstm_model.predict(last_seq, verbose=0)[0][0]
52
+ lstm_forecast = lstm_scaler.inverse_transform([[lstm_pred_scaled]])[0][0]
53
+
54
+ results.append({
55
+ "Product": product,
56
+ "ARIMA Pred": round(arima_forecast, 2),
57
+ "ANN Pred": round(ann_forecast, 2),
58
+ "LSTM Pred": round(lstm_forecast, 2),
59
+ "Average Requirement":round((arima_forecast+ann_forecast+lstm_forecast)/3,2)
60
+ })
61
+
62
+ except Exception as e:
63
+ print(f"Failed to predict for {product} - {location}: {e}")
64
+ return pd.DataFrame(results)