What exactly the Dynamic Pricing is?
Dynamic Pricing is a data science-driven strategy that adjusts prices in real time based on market demand, customer behavior, and competitor pricing. By leveraging algorithms and data insights, businesses optimize revenue by setting flexible prices.
For example:
Consider an e-commerce platform that sells electronics. Instead of using fixed prices, the platform applies dynamic pricing by analyzing factors like product demand, stock levels, customer browsing behavior, and competitor prices. When demand for a product is high, such as during a sale or holiday season, the platform can increase prices to maximize revenue. Conversely, when demand is low or competitors offer discounts, prices can be reduced to attract more buyers and stay competitive.
This approach helps businesses optimize pricing in real-time, driving both profitability and customer engagement.
Dynamic Pricing Strategy: An Overview
In a dynamic pricing strategy, the goal is to maximize revenue and profitability by setting prices that effectively balance supply and demand. This approach enables businesses to adjust prices in real-time based on factors such as time of day, day of the week, customer segments, inventory levels, seasonal trends, competitor pricing, and market conditions.
To build a data-driven dynamic pricing strategy, businesses need access to data that provides valuable insights into customer behavior, market trends, and other relevant factors. Key datasets required for implementing such a strategy include:
- Historical sales data
- Customer purchase patterns
- Market demand forecasts
- Cost data
- Customer segmentation data
- Real-time market data
Lets start coding :-
Let’s start the task of building a dynamic pricing strategy by importing the necessary Python libraries and Dataset
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
data=pd.read_csv('/content/drive/MyDrive/dynamic_pricing.csv')
data.head()
fig = px.scatter(data, x='Expected_Ride_Duration',
y='Historical_Cost_of_Ride',
title='Expected Ride Duration vs. Historical Cost of Ride',
trendline='ols')
fig.show()
Now let’s have a look at the distribution of the historical cost of rides based on the vehicle type:
fig = px.box(data, x='Vehicle_Type',
y='Historical_Cost_of_Ride',
title='Historical Cost of Ride Distribution by Vehicle Type')
fig.show()
Now let’s have a look at the correlation matrix:
corr_matrix = data.corr()fig = go.Figure(data=go.Heatmap(z=corr_matrix.values,
x=corr_matrix.columns,
y=corr_matrix.columns,
colorscale='Viridis'))
fig.update_layout(title='Correlation Matrix')
fig.show()
Adopting a Dynamic Pricing Strategy
The company’s data indicates that its pricing model currently relies solely on the expected ride duration to set the price. In this case, we will implement a dynamic pricing strategy that adjusts ride costs based on demand and supply levels observed in the data. This model will increase prices during high-demand periods and low-supply scenarios, while lowering prices during low-demand times and high-supply conditions.
Here’s how to implement this dynamic pricing strategy using Python:
import numpy as np# Calculate demand_multiplier based on percentile for high and low demand
high_demand_percentile = 75
low_demand_percentile = 25
data['demand_multiplier'] = np.where(data['Number_of_Riders'] > np.percentile(data['Number_of_Riders'], high_demand_percentile),
data['Number_of_Riders'] / np.percentile(data['Number_of_Riders'], high_demand_percentile),
data['Number_of_Riders'] / np.percentile(data['Number_of_Riders'], low_demand_percentile))
# Calculate supply_multiplier based on percentile for high and low supply
high_supply_percentile = 75
low_supply_percentile = 25
data['supply_multiplier'] = np.where(data['Number_of_Drivers'] > np.percentile(data['Number_of_Drivers'], low_supply_percentile),
np.percentile(data['Number_of_Drivers'], high_supply_percentile) / data['Number_of_Drivers'],
np.percentile(data['Number_of_Drivers'], low_supply_percentile) / data['Number_of_Drivers'])
# Define price adjustment factors for high and low demand/supply
demand_threshold_high = 1.2 # Higher demand threshold
demand_threshold_low = 0.8 # Lower demand threshold
supply_threshold_high = 0.8 # Higher supply threshold
supply_threshold_low = 1.2 # Lower supply threshold
# Calculate adjusted_ride_cost for dynamic pricing
data['adjusted_ride_cost'] = data['Historical_Cost_of_Ride'] * (
np.maximum(data['demand_multiplier'], demand_threshold_low) *
np.maximum(data['supply_multiplier'], supply_threshold_high)
)
In the above code, we begin by calculating the demand multiplier, which compares the number of riders to percentiles indicating high and low demand levels. If the number of riders exceeds the high-demand percentile, the demand multiplier is set as the ratio of riders to the high-demand percentile. If the number of riders is below the low-demand percentile, the multiplier is set as the ratio of riders to the low-demand percentile.
Next, we calculate the supply multiplier by comparing the number of drivers to percentiles for high and low supply levels. If the number of drivers exceeds the low-supply percentile, the supply multiplier is set as the ratio of the high-supply percentile to the number of drivers. If the number of drivers falls below the low-supply percentile, the supply multiplier is set as the ratio of the low-supply percentile to the number of drivers.
Finally, we calculate the adjusted ride cost for dynamic pricing by multiplying the historical ride cost by the maximum of the demand multiplier and a lower threshold (demand_threshold_low
), as well as by the maximum of the supply multiplier and an upper threshold (supply_threshold_high
). This ensures the adjusted ride cost reflects the combined effects of demand and supply, with the thresholds acting as limits to control price fluctuations.
Now, let’s calculate the profit percentage resulting from the implementation of this dynamic pricing strategy.
# Calculate the profit percentage for each ride
data['profit_percentage'] = ((data['adjusted_ride_cost'] - data['Historical_Cost_of_Ride']) / data['Historical_Cost_of_Ride']) * 100
# Identify profitable rides where profit percentage is positive
profitable_rides = data[data['profit_percentage'] > 0]# Identify loss rides where profit percentage is negative
loss_rides = data[data['profit_percentage'] < 0]
import plotly.graph_objects as go
# Calculate the count of profitable and loss rides
profitable_count = len(profitable_rides)
loss_count = len(loss_rides)
# Create a donut chart to show the distribution of profitable and loss rides
labels = ['Profitable Rides', 'Loss Rides']
values = [profitable_count, loss_count]
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=0.4)])
fig.update_layout(title='Profitability of Rides (Dynamic Pricing vs. Historical Pricing)')
fig.show()
Now let’s have a look at the relationship between the expected ride duration and the cost of the ride based on the dynamic pricing strategy:
fig = px.scatter(data,
x='Expected_Ride_Duration',
y='adjusted_ride_cost',
title='Expected Ride Duration vs. Cost of Ride',
trendline='ols')
fig.show()
Training a Predictive Model
Now, as we have implemented a dynamic pricing strategy, let’s train a Machine Learning model. Before training the model, let’s preprocess the data:
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScalerdef data_preprocessing_pipeline(data):
#Identify numeric and categorical features
numeric_features = data.select_dtypes(include=['float', 'int']).columns
categorical_features = data.select_dtypes(include=['object']).columns
#Handle missing values in numeric features
data[numeric_features] = data[numeric_features].fillna(data[numeric_features].mean())
#Detect and handle outliers in numeric features using IQR
for feature in numeric_features:
Q1 = data[feature].quantile(0.25)
Q3 = data[feature].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - (1.5 * IQR)
upper_bound = Q3 + (1.5 * IQR)
data[feature] = np.where((data[feature] < lower_bound) | (data[feature] > upper_bound),
data[feature].mean(), data[feature])
#Handle missing values in categorical features
data[categorical_features] = data[categorical_features].fillna(data[categorical_features].mode().iloc[0])
return data
In the above code, we have created a data preprocessing pipeline to prepare the data. Since vehicle type is an important factor, let’s convert it into a numerical feature before proceeding further.
data["Vehicle_Type"] = data["Vehicle_Type"].map("Premium": 1,
"Economy": 0)
Now let’s split the data and train a Machine Learning model to predict the cost of a ride:
#splitting data
from sklearn.model_selection import train_test_split
x = np.array(data[["Number_of_Riders", "Number_of_Drivers", "Vehicle_Type", "Expected_Ride_Duration"]])
y = np.array(data[["adjusted_ride_cost"]])x_train, x_test, y_train, y_test = train_test_split(x,
y,
test_size=0.2,
random_state=42)
# Reshape y to 1D array
y_train = y_train.ravel()
y_test = y_test.ravel()
# Training a random forest regression model
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(x_train, y_train)
Now let’s test this Machine Learning model using some input values:
def get_vehicle_type_numeric(vehicle_type):
vehicle_type_mapping =
"Premium": 1,
"Economy": 0
vehicle_type_numeric = vehicle_type_mapping.get(vehicle_type)
return vehicle_type_numeric# Predicting using user input values
def predict_price(number_of_riders, number_of_drivers, vehicle_type, Expected_Ride_Duration):
vehicle_type_numeric = get_vehicle_type_numeric(vehicle_type)
if vehicle_type_numeric is None:
raise ValueError("Invalid vehicle type")
input_data = np.array([[number_of_riders, number_of_drivers, vehicle_type_numeric, Expected_Ride_Duration]])
predicted_price = model.predict(input_data)
return predicted_price
# Example prediction using user input values
user_number_of_riders = 50
user_number_of_drivers = 25
user_vehicle_type = "Economy"
Expected_Ride_Duration = 30
predicted_price = predict_price(user_number_of_riders, user_number_of_drivers, user_vehicle_type, Expected_Ride_Duration)
print("Predicted price:", predicted_price)
Predicted price: [244.44059707]
Here’s a comparison of the actual and predicted results:
import plotly.graph_objects as go# Predict on the test set
y_pred = model.predict(x_test)
# Create a scatter plot with actual vs predicted values
fig = go.Figure()
fig.add_trace(go.Scatter(
x=y_test.flatten(),
y=y_pred,
mode='markers',
name='Actual vs Predicted'
))
# Add a line representing the ideal case
fig.add_trace(go.Scatter(
x=[min(y_test.flatten()), max(y_test.flatten())],
y=[min(y_test.flatten()), max(y_test.flatten())],
mode='lines',
name='Ideal',
line=dict(color='red', dash='dash')
))
fig.update_layout(
title='Actual vs Predicted Values',
xaxis_title='Actual Values',
yaxis_title='Predicted Values',
showlegend=True,
)
fig.show()
So this is how you can use Machine Learning to implement a data-driven dynamic pricing strategy using Python.
Summary
A dynamic pricing strategy aims to optimize revenue and profitability by adjusting prices to strike the perfect balance between supply and demand. This approach enables businesses to dynamically set prices based on various factors such as time of day, day of the week, customer segments, inventory levels, seasonal trends, competitor pricing, and market conditions. I hope you found this article on Dynamic Pricing Strategy using Python insightful. Feel free to share your thoughts and ask any questions in the comments section below!