Skip to content

Online shopping decisions rely on how consumers engage with online store content. You work for a new startup company that has just launched a new online shopping website. The marketing team asks you, a new data scientist, to review a dataset of online shoppers' purchasing intentions gathered over the last year. Specifically, the team wants you to generate some insights into customer browsing behaviors in November and December, the busiest months for shoppers. You have decided to identify two groups of customers: those with a low purchase rate and returning customers. After identifying these groups, you want to determine the probability that any of these customers will make a purchase in a new marketing campaign to help gauge potential success for next year's sales.

Data description:

You are given an online_shopping_session_data.csv that contains several columns about each shopping session. Each shopping session corresponded to a single user.

ColumnDescription
SessionIDunique session ID
Administrativenumber of pages visited related to the customer account
Administrative_Durationtotal amount of time spent (in seconds) on administrative pages
Informationalnumber of pages visited related to the website and the company
Informational_Durationtotal amount of time spent (in seconds) on informational pages
ProductRelatednumber of pages visited related to available products
ProductRelated_Durationtotal amount of time spent (in seconds) on product-related pages
BounceRatesaverage bounce rate of pages visited by the customer
ExitRatesaverage exit rate of pages visited by the customer
PageValuesaverage page value of pages visited by the customer
SpecialDaycloseness of the site visiting time to a specific special day
Weekendindicator whether the session is on a weekend
Monthmonth of the session date
CustomerTypecustomer type
Purchaseclass label whether the customer make a purchase
# Import required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom

# Load and view your data
shopping_data = pd.read_csv("online_shopping_session_data.csv")
shopping_data.head()
# Start your code here!
nov_dec_shopping = shopping_data[shopping_data['Month'].isin(['Nov','Dec'])]
# Use as many cells as you like
nov_dec_shopping['Month'].unique()
# Start your code here!
purchase_rates = nov_dec_shopping.groupby(['CustomerType'])['Purchase'].mean()

# Sort the index in descending order
purchase_rates = purchase_rates.sort_index(ascending=False)

# Convert the Series to a dictionary
purchase_rates = purchase_rates.to_dict()

purchase_rates
corr_matrix = nov_dec_shopping[['Administrative_Duration', 'Informational_Duration', 'ProductRelated_Duration']].corr()
corr_matrix
# Reset the index to convert the matrix into a DataFrame for easier processing
correlation_df = corr_matrix.stack().reset_index()
correlation_df
correlation_df.columns = ['Column1', 'Column2', 'Correlation']
correlation_df
# Filter out duplicate correlations and self-correlations
correlation_df = correlation_df[correlation_df['Column1'] != correlation_df['Column2']]
correlation_df
# Identify the row with the highest correlation
max_corr_row = correlation_df.loc[correlation_df['Correlation'].idxmax()]
# Extract the pair and the correlation
top_correlation = {
    "pair": (max_corr_row['Column1'], max_corr_row['Column2']),
    "correlation": max_corr_row['Correlation']
}

print(top_correlation)
returning_customer_boost = 1.15*purchase_rates['Returning_Customer']
returning_customer_boost
prob_at_least_100_sales = 1 - binom.cdf(100, 500, returning_customer_boost)
prob_at_least_100_sales
# Parameters
n = 500  # Number of trials
p = returning_customer_boost  # Probability of success
x = np.arange(0, n + 1)  # Possible values of successes
pmf = binom.pmf(x, n, p)  # Binomial PMF

# Plot
plt.figure(figsize=(8, 5))
plt.bar(x, pmf, color='skyblue', alpha=0.7, label='Binomial PMF')
plt.vlines(100, 0, 0.08, color='r', linestyle='dashed', label="sales=100")
plt.xlabel('Number of Successes (k)')
plt.ylabel('Probability')
plt.title(f'Binomial Distribution (n={n}, p={p})')
plt.legend()
plt.show()