Spaces:
Sleeping
Sleeping
Update data_collector.py
Browse files- data_collector.py +27 -0
data_collector.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import mysql.connector
|
2 |
from decimal import Decimal
|
3 |
import pandas as pd
|
|
|
|
|
4 |
# Define the connection parameters
|
5 |
host = "159.138.104.192"
|
6 |
user = "storemate_ml"
|
@@ -8,6 +10,31 @@ password = "bTgZd77VpD^o4Ai6Dw9xs9"
|
|
8 |
database = "lite_version"
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def get_data(b_id,product_name):
|
12 |
# Create a connection to the MySQL server
|
13 |
try:
|
|
|
1 |
import mysql.connector
|
2 |
from decimal import Decimal
|
3 |
import pandas as pd
|
4 |
+
from prophet import Prophet
|
5 |
+
import math
|
6 |
# Define the connection parameters
|
7 |
host = "159.138.104.192"
|
8 |
user = "storemate_ml"
|
|
|
10 |
database = "lite_version"
|
11 |
|
12 |
|
13 |
+
|
14 |
+
def forecast(monthly_sales):
|
15 |
+
# Prepare the data for Prophet
|
16 |
+
monthly_sales.rename(columns={'transaction_date': 'ds', 'sell_qty': 'y'}, inplace=True)
|
17 |
+
|
18 |
+
# Initialize and fit the Prophet model
|
19 |
+
model = Prophet()
|
20 |
+
model.fit(monthly_sales)
|
21 |
+
|
22 |
+
# Make a future dataframe for the next month
|
23 |
+
future = model.make_future_dataframe(periods=1, freq='M')
|
24 |
+
forecast = model.predict(future)
|
25 |
+
|
26 |
+
# Extract the forecasted sales for the next month
|
27 |
+
forecasted_sales = forecast[['ds', 'yhat']].tail(2)
|
28 |
+
|
29 |
+
# Combine historical and forecasted data
|
30 |
+
combined_sales = pd.concat([monthly_sales, forecasted_sales[-1:]], ignore_index=True)
|
31 |
+
original_forecasted_value = combined_sales.tail(1)
|
32 |
+
rounded_value = combined_sales.tail(1)
|
33 |
+
|
34 |
+
rounded_value['yhat'] = rounded_value['yhat'].apply(lambda x: max(0, math.ceil(x)))
|
35 |
+
|
36 |
+
return combined_sales,original_forecasted_value,rounded_value
|
37 |
+
|
38 |
def get_data(b_id,product_name):
|
39 |
# Create a connection to the MySQL server
|
40 |
try:
|