Spaces:
Sleeping
Sleeping
File size: 5,174 Bytes
a55ae21 | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import modal
import pandas as pd
import time
from src.pricing.pricing_algorithm import grid_search_lambda
import numpy as np
xgb_image = modal.Image.debian_slim().pip_install("pandas==1.4.2", "xgboost", "scikit-learn")
stub = modal.Stub("example-xgb-started")
if stub.is_inside():
from xgboost import XGBRegressor
import pandas as pd
# ignore warnings
import warnings
warnings.filterwarnings('ignore')
@stub.function(image=xgb_image)
def perform_inference(train_data, test_data):
# Fit the XGBoost model
xgboost_model = XGBRegressor()
xgboost_model.fit(train_data.drop(columns=['SKU', 'sales']), train_data['sales'])
# Group test data by SKU
grouped = test_data.groupby('SKU')
# Create an empty dataframe to store the results
results = pd.DataFrame(columns=['SKU', 'price', 'predicted_sales_xgboost'])
# Perform inference for each SKU in test data
for sku, group_data in grouped:
print(f"Performing inference for SKU: {sku}")
# Select one row from group data (one set of feature values for this SKU)
feature_values = group_data.iloc[0]
#print(f"Feature values: {feature_values.shape}")
# Set the base price from data
base_price = feature_values['price']
# Define the price points
price_points = [base_price,
base_price * 0.9, # base_price - 10%
base_price * 0.85, # base_price - 15%
base_price * 1.1, # base_price + 10%
base_price * 1.15] # base_price + 15%
# For each price point, use the trained model to predict sales and print the result
for price in price_points:
# Change the price while keeping other features constant
test_features = feature_values.copy()
test_features['price'] = price
# Remove the SKU field to match the input shape expected by the model
test_features = test_features.drop(['SKU', 'sales'])
#print(f"Test features: {test_features.shape}")
# Start the timer
start_time = time.time()
# Predict sales using the XGBoost model and print the result
predicted_sales_xgboost = xgboost_model.predict(test_features.values.reshape(1, -1))[0]
# Stop the timer
elapsed_time = time.time() - start_time
print(f"Price: {price}, XGBoost Predicted Sales: {predicted_sales_xgboost}, Elapsed Time: {elapsed_time:.4f} seconds")
# Append the results to the dataframe
results = results.append({'SKU': sku, 'price': price, 'predicted_sales_xgboost': predicted_sales_xgboost}, ignore_index=True)
print("\n")
return results
@stub.function(image=xgb_image)
def find_optimal_price(sku_row, c=10, s=2, P0=100):
# Extracting SKU, price and predicted sales
sku = sku_row['SKU']
price = sku_row['price']
predicted_sale = sku_row['predicted_sales_xgboost']
print(f'\nProcessing SKU: {sku}')
print(f'Original Price: {price}, Predicted Sales: {predicted_sale}')
# Call grid_search_lambda function to find optimal price for the current SKU
prices = np.array([price])
predicted_sales = np.array([predicted_sale])
# start the timer
start_time = time.time()
print(f'Starting grid search for SKU: {sku}')
best_prices, best_lambdas = grid_search_lambda(P0, prices, predicted_sales, c, s)
# stop the timer
elapsed_time = time.time() - start_time
# Extract the optimal price
optimal_price = best_prices[0]
print(f'Optimal Price for SKU {sku}: {optimal_price}, Elapsed Time: {elapsed_time:.4f} seconds\n')
# Return the optimal price
return optimal_price
@stub.local_entrypoint()
def main():
# load train.pickle and test.pickle from ..data/processed folder
train_data = pd.read_pickle('/Users/servandodavidtorresgarcia/Servando/Relu/Relu/data/processed/train.pkl')
test_data = pd.read_pickle('/Users/servandodavidtorresgarcia/Servando/Relu/Relu/data/processed/test.pkl')
# start the timer
start_time = time.time()
# Perform inference using the trained model
df = perform_inference.call(train_data, test_data)
df.to_csv('/Users/servandodavidtorresgarcia/Servando/Relu/Relu/data/processed/predictions.csv', index=False)
# stop the timer
elapsed_time = time.time() - start_time
print(f"Total Elapsed Time for perform_inference: {elapsed_time:.4f} seconds")
# start the timer
start_time = time.time()
predicted_sales = pd.read_csv('/Users/servandodavidtorresgarcia/Servando/Relu/Relu/data/processed/predictions.csv')
optimal_prices = predicted_sales.apply(find_optimal_price, axis=1)
result_df = pd.DataFrame({'SKU': df['SKU'], 'optimal_prices': optimal_prices})
result_df.to_csv('/Users/servandodavidtorresgarcia/Servando/Relu/Relu/data/processed/optimal_prices.csv', index=False)
# stop the timer
elapsed_time = time.time() - start_time
print(f"Total Elapsed Time for find_optimal_price: {elapsed_time:.4f} seconds")
|