{'fraction_non_alphanumeric': 0.035632339047104875, 'fraction_numerical': 0.009346740450803078, 'mean_word_length': 4.987272727272727, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
OPAM: Online Purchasing-behavior Analysis using Machine learning

Customer purchasing behavior analysis plays a key role in developing insightful communication strategies between online vendors and their customers. To support the recent increase in online shopping trends, in this work, we present a customer purchasing behavior analysis system using supervised, unsupervised and semi-supervised learning methods. The proposed system analyzes session and user-journey level purchasing behaviors to identify customer categories/clusters that can be useful for targeted consumer insights at scale. We observe higher sensitivity to the design of online shopping portals for session-level purchasing prediction with accuracy/recall in range 91-98%/73-99%, respectively. The user-journey level analysis demonstrates five unique user clusters, wherein 'New Shoppers' are most predictable and 'Impulsive Shoppers' are most unique with low viewing and high carting behaviors for purchases. Further, cluster transformation metrics and partial label learning demonstrates the robustness of each user cluster to new/unlabelled events. Thus, customer clusters can aid strategic targeted nudge models.


I. INTRODUCTION
Recent times have witnessed a significant increase in online shopping activities, that in turn has necessitated development of personalized recommendation systems for a seamless shopping experience [1]. As shoppers continue to interact with products online, purchasing behavior patterns can be mined from session-level [2] and user-product journey level interactions for targeted communications and personalized shopping strategies for vendors and customers, respectively. For example, accurate purchasing predictions from user groups can ensure adequate product inventory based on user engagement levels and it can lead to directed nudge models beneficial to a specific user group.
The primary bottleneck for designing such a predictive system is the disparity in virtual shopping experiences across products and platforms due to variations in product cost, delivery wait times and ease of platform usage [3] [4]. In this work, we present a novel online shopping behavior analysis system that utilizes supervised, unsupervised and semisupervised learning models to identify and analyze purchasing behavior clusters/categories for accurate inventory and strategic marketing campaign design. We demonstrate the scalability of the proposed system across product cost, category, wait time and data size constraints by analyzing purchasing behaviors for cosmetics [3] and electronics data sets [4] separately. The pro-posed system provides detailed insights into customer-product interactions at a session and journey level by assessing three properties: 1) importance of shopping platform and productlevel features for purchase predictions, 2) formation of userbehavior clusters and the effort required to transform users across clusters, 3) the robustness of sample neighborhoods toward unlabelled/partially labelled data sets representing high volumes of ongoing user journeys.
Each time customers login to an online shopping website, they accept cookies to establish the session. The {session-ID, client-ID} combination can then be used to uniquely log information regarding product-level browsing, additions to cart, removals from cart and purchase etc. The session-level data can then be consolidated to create user-journeys, as shown in Fig. 1, to predictively analyze the purchasing propensity per user-type for each product-type interaction. Existing works so far in [1] [2] have analyzed products with demand and price variations such as cosmetics vs. electronic items at session-levels differently. The work in [5] applied the time-stamps of clicks in a clicking stream per session to model the buying patterns and then predicted purchasing decisions using bidirectional LSTM models. Further, [5] demonstrated that the accuracy from click stream sequences and LSTM models was comparable to that using engineered features and classification models. In this work, we extend the analysis of predictive models at session-level and user-journey level for purchase events in terms of sensitivity to the online shopping portal and product-level features, respectively.
Apart from session-level assessments, the work in [6] shows that there is a need to predict repeat customers and their tendency to return and finish their orders. This motivates our analysis of user-journey level interactions. Here, we analyze session-level and user-journey level features to categorize user-interaction clusters. The knowledge of user clusters can thus lead to improved predictive modeling for purchasing events per cluster to accurately gauge customer-specific demands.
Additionally, probabilistic label propagation using k-NN, SVM and other methods, has been used so far to assess the sensitivity of training data to controlled false labels in existing works [7]. The major difference between the datasets in [7] [8] and this work is that false labels are not naturally occurring for user-journeys. Instead, we use semi-supervised methods to analyze predictive robustness for each user cluster, i.e., how accurately the purchasing behavior can be predicted in case of lost or missing labels.
The proposed system shown in Fig. 2 makes the following three major contributions. First, we analyze the sensitivity of the online shopping portal and product-level features for session-level and user-journey level classification, respectively, for purchase prediction. Second, we apply unsupervised learning to identify unique user-behavior clusters/categories. We observe that predictability of a purchase event per cluster varies significantly. Third, we perform user-cluster analytics that includes cluster formation, effort required for inter-cluster transformation and predictive robustness per cluster using k-NN based label propagation. The Python code for our system is provided for benchmarking and system extendability 1 . Proposed system for analysis of user-behavior categories/clusters. Step 1: To find optimal classifiers for predicting sessions and user-journeys leading to a purchase.
Step 2: Identification of user clusters.
Step 3: Analysis of user clusters for formation, transformation and robustness to unlabelled data.

II. MATERIALS AND METHODS
Descriptions of the data sets under analysis and the methods used to analyze the session and user-journey level data sets are described below.

A. Data and Output Metrics
We analyze two public datasets acquired from Kaggle for a comprehensive experiment series, including eCommerce Events History in Cosmetics Shop [3] (size 2 GB), as well as eCommerce behavior data from multi category store [4] (size 15 GB). For the second dataset, electronics data is retained to keep the study market specific. Both datasets have the same columns: user ID, time stamp of the event, product metadata, i.e., {product category, brand, price}, user session ID and event type. For the cosmetics data set, the event types are {cart, view, remove from cart, purchase}. However, for the electronics data set, event types are {cart, view, purchase}. This variation in data event types aids investigations towards the predictability of a purchasing event based on differing online shopping platforms.
For our analysis, we perform data aggregation to extract features corresponding to each user-product interaction at a session-level and at user-journey levels (denoted by {X, X s }, respectively). If the session or user-journey contained a purchasing event, the label {Y, Y s }, is set to 1, else if the session or journey resulted in product viewing, carting and or removing from cart, then the label is set to 0, respectively.
The session-level data and user journey-level data is subjected to feature ranking followed by data modeling using an AutoML library [9] to identify the best data model that can predictively identify purchasing sessions and journeys from the non-purchasing ones. Given, that the classification models result in true positive (tp), true negative (tn), fale positive (f p) and false negative (f n) samples, the output metrics are accuracy = tp+tn tp+tn+f p+f n , precision = tp tp+f p , recall = tp tp+f n , F 1 = 2tp 2tp+f p+f n . The data sets under analysis here demonstrate high data imbalance with the ratio of non-purchase to purchase records being nearly 7:1 in the cosmetics dataset and 35:1 in the electronics dataset when aggregated at a user-journey level. At session-level aggregation, this ratio is 28:1 and 16:1 in the cosmetics and electronics datasets, respectively. Thus, the metrics that need to be maximized for best data model are recall, F 1 metrics that place a higher weightage on accurate detection of purchase records over the non-purchase records.

B. Session-Level Classification
There are two specific instances where session-level predictive modeling is more beneficial than user-journey level. First, if a user logs in through multiple devices or skips the login process altogether, multiple session records get created wherein the history cannot be retained. In such cases sessionlevel predictions for a purchasing event can guide marketing nudge models. Second, sequence models such as LSTM are capable of learning local and global contextual patterns for purchasing behaviors. Thus, data processing on a session-level can prove to be more storage efficient than user-journeys.
As a first step, hand-engineered features are subjected to feature ranking using Random forest and Fisher scoring [10]. The session-level features selected for cosmetics dataset are: total number of events (viewing, carting removal etc.), number of brands in cart, number of products in cart, number of times carting occurs, number of times removal from cart occurs, number of viewing events, number of brands viewed, and number of products viewed. Similarly, the features selected for electronics dataset are: average price of the products in cart, number of brands in cart, number of categories in cart, number of products in cart, number of times carting occurs, total price in cart, number of events in a session, total interaction time, and number of brands viewed. Next, based on session-level features, LSTM-based models are used to classify a purchase at session-level.

C. User-Journey Level Classification
Similar to the session-level data, the user-journey level data is subjected to feature ranking [10]. Exploratory data analysis and feature ranking demonstrates that there is little to no variance in the distribution of purchase journeys vs. nonpurchase journeys when measured against date-time attributes. An example of feature ranking at user-journey level is shown in Fig. 3, where we observe that features like number of events in user-journey, total interaction time, number of sessions, number of carts, views and removals have significantly higher weightage than features like date, time or month of purchase. Based on this analysis, we select the top ranked 11 features with significant weights that range from total interaction time, number of events, total carting and viewing time to max and min price range. Each feature is then scaled in the range [0,1] for further data modeling and analysis.

D. Clustering of User-journey data
We perform unsupervised clustering on the user-journey data, where, the optimal feature sets extracted above are converted to the t-SNE plane. First, we apply k-means clustering to the converted data samples followed by the Elbow method (using the Yellowbrick Library in Python) to find the optimal number of user-journey clusters (Q) that minimizes overall distortion score per cluster. For both cosmetics and electronics data sets, the optimal number of clusters identified are K = 5, as shown in

E. User-Journey Cluster Analytics
The user journey data is {X, Y }, where the journey-level features, X ∈ R [nXd] , and the journey's purchasing outcome is represented by [Y = (0, 1) ∈ R [nX1] , ∀i = 1 : n]. Here, n represents the number of samples and d represents the selected feature dimensions. Next, the user-journey cluster ID for each sample can be treated as a feature vector representative of the sample neighborhoods as, Q ∈ R [nX1] . It is noteworthy that the distribution of sample clusters is non-linearly dependent on the user-journeys. Hence, combining user journeys with cluster information should intuitively reduce model fitting error.
To analyze the user-clusters, we perform the following three sets of experiments using the samples per cluster (1). Here, n q represents the number of samples in cluster q.

1) Cluster Formation Analysis:
We analyze the formation of the user-clusters in terms of Calinski Harabasz (CH) score defined in (2) and Silhouette Scores (SS) defined in (4) based on prior works in [11]. High CH scores are representative of maximum between cluster separations (B q ) and minimum within cluster distances (W q ) defined in (3). Here,x and x q imply total sample mean and sample mean per cluster, respectively. where, Consistency in the SS metric across cluster definitions is indicative of large separating distances between samples of different clusters (represented by b) over samples of the same cluster (represented by a) in (5). Here, 2) Cluster Transformation Analysis: Next, we analyze the effort required to transform a cluster distribution to another cluster in terms of the EarthMover-Distance (EMD, or Wasserstein-1 distance) [12]. EMD is an asymmetric measure and is computed between each combination pairs of clusters, between cluster q and q using representative histogram distributions in (6). Here, scaled samples (X q ) are subjected to binning operation performed for each feature, such that P q (h) represents the h th bin for the histogram created for all samples across all features in cluster q. Here, we limit the total number of bins to H = 10 6 . Also, in (6), F q (h) represents the quantile function at bin value h, ∀h = [0, 10 −6 , 2 * 10 −6 ...1].
High values for EMD imply large effort required to transform samples across the clusters and vice versa.
3) Predictive Robustness using Partial Label Learning: Finally, we analyze the predictive robustness of each cluster to missing labels or ongoing user-journeys using partial label learning (PLL) in the semi-supervised setting as in [7]. For this analysis, we randomly select user-journey samples per cluster X q and drop their labels (assign y = −1). Next, we apply k-NN based label propagation (here, k-varied as odd numbers in range (1:15)) with α = 0.1, which is representative of low modification rate to propagated labels in a transductive setting [8]. Performing 5-fold cross validation, we determine that k = 3 results in least prediction error. Next, to analyze predictive robustness per cluster, we sub-sample instances by varying the proportion of dropped labels p in the range [0.1-0.9]. Next, we apply label propagation for each subsampled instance followed by plotting the accuracy and F 1 for classification of the samples with dropped labels. This process is repeated 50 times per drop-proportion p and the averaged accuracy and F 1 scores are plotted. Here, the goal is to assess the consistency in trends of accuracy and F 1 across clusters to identify which clusters are robust to missing labels.

III. EXPERIMENTS AND RESULTS
Our primary goal is to assess the predictability of a purchase event at a session and user-journey level in terms of features and sample neighborhood. Thus, we perform three sets of experiments corresponding to the three modules in the proposed system. First, we implement sequence models to predict purchasing events on a session-level. The probabilistic outcome per-session indicates if the next session is likely to result in a purchase or not. Second, we implement classification models on a user-journey level using TPOT library [9]. Third, we analyze the user journey clusters to infer customer insights that are necessary to inform marketing and customer retention strategies.

A. Session-Level Classification
For the session-level data, we implement LSTM models with differing layer and neuron structure combinations to identify the best LSTM model structure for both data sets. At a session-level, the percentage of sessions that end in purchases are 9.22% and 10.94% for the cosmetics and electronics data sets, respectively. Thus, in the absence of a trained LSTM model, if all sessions were assigned the major nonpurchase class, we would still achieve 90.78% and 89.06% baseline accuracy for the cosmetics and electronics data sets, respectively. To counteract the class imbalance, LSTM models are trained on balanced data generated by over sampling the minor class distribution [10]. Here, a variety of LSTM network structures are analyzed, with 1-3 layers of bidirectional LSTM layers and 10-40 neurons per layer.
The network structure resulting in the highest recall is presented in Table I. We empirically determine that a single layer of bidirectional LSTM with 40 neurons is the best model for the cosmetics and electronics data sets, respectively. From Table I, we observe that prediction of a purchase event is more accurate (high recall, F 1) for the cosmetics data set than for the electronics dataset. One reason for the high f p rate in the electronics data set is that view events take up almost 95% of all events, and there is no option to remove from cart. Thus, a viewing session most often gets falsely predicted to end up in a purchase event. Additionally, to baseline the session-level prediction of a purchasing event, we create a sequence model from the sequence of productlevel interactions per session and the time spent per interaction from [5]. To create the baseline sequence model, the sessionlevel events are categorized as: 1=view, 2=cart, 3=remove from cart, 4=purchase. So the input (X s ) is a sequence of maximum 100 such events without the purchasing event e.g. {1,2,1,1,1,1,3,1,,......}, and the time spent on each event, while the output (Y s ) is binary representing a purchase event occured or not. We observe that when compared to proposed Bi- LSTM on session-level features, the sequence models fail to capture product price and brand-related information, that are key factors impacting purchasing decisions. This results in significantly higher session-level recall for the proposed model over the baseline sequence models on both data sets. Lower precision for the proposed model may lead to some over-stocking inventory per session that would not impact the online shopping experience negatively.

B. User-journey level classification
Both the cosmetics and electronics are benchmarked using AutoML TPOT library (v 0.11.6.post3) [9]. Each data set is sub-sampled by stratified sampling based on the cluster IDs into samples of 25,000 each with the selected set of features after feature ranking. These sub-samples are subjected to the AutoML to identify the best data model with optimal hyperparameters. The best data model is then applied to the complete data set in batches of 1-2M samples each, generated by cluster-based stratified sampling. The average classification performances after 25 such runs of stratified sampling for 70/30 train-test split are shown in Table II. For the cosmetics data set, the XGboost Classifier is found to be the best model with the following parameters: (learning rate=0.001, max depth=9, min child weight=7, n estimators=100, n jobs=1). For electronics data, the best model is Decision Tree Classifier with (max depth=10, and minimum samples leaf=3, min samples split=2). The average classification performances of the best data models in comparison with a baseline knearest neighbor (k-NN) classifier with k = 3 is shown in Table II. Here, we observe that although the electronics data set has higher purchase to non-purchase imbalance, it has better classification performance for purchase events than the cosmetics data set. This is primarily due to the fact that product-level feature variations are significantly lesser among cosmetics samples. Most cosmetics have a narrow price range of (5$-11$) while electronics have a wider range of (290$-402$). Additionally, the brand-specific distinction in electronics is significantly higher than for the cosmetics data set. Thus, product-level features have a significant impact on user-journey level prediction of purchase events.

C. User-journey Cluster Analysis
From the data, we know that at user-journey level the complete cosmetics and electronics data sets have purchase ratios around 12% and 1%, respectively. Also, data clustering provided 5 aggregated user clusters for each data set in Section II-D. Sample visualization of these user clusters is shown in Fig. 5. Next, we analyze the samples from each user-cluster and define an aggregated identity per cluster as shown in Fig. 6.
Thereafter, in Table III  . Here, we observe that for both data sets, one major cluster represents 91-99% of all the data samples. Also, as the cluster size decreases, the P uR increases upto 3-8 times of the overall P uR. Ideally, targeted promotional and marketing campaigns should be based on cluster P uR along with the predictability of purchase per-cluster. In Table IV, we analyze the predictability of a purchasing event at per-cluster using the best data model trained in Section III-B. Here, we observe a significant variation in recall and F 1 across clusters. For example, in both data sets, the cluster corresponding to New Shoppers (ID is 1 for cosmetics, 0 for electronics, respectively,) has the lowest P uR but this cluster is the most purchase-event predictable in terms of recall and F 1. This implies that New Shoppers require promotional incentives to return the shopping website to increase product-level interactions (number of sessions) rather that incentives like discount coupons to convert them to other clusters that may have higher P uR but low purchase predictability. Also, we observe that for the cosmetics dataset, sample neighborhoods are well defined (low variations in k-NN performances) and neighborhood based purchase prediction is more robust than feature-based prediction. Next, we analyze the predictive nature of each cluster in terms of cluster formation, the effort required for cluster transformation and the predictive robustness for purchase events as follows.
1) Cluster Formation: We analyze the structure of the userclusters in terms of the similarity between samples of the same cluster and dissimilarity with samples from other clusters using the cluster scores of CH and SS described in Section II-E. We start with samples from the two clusters with maximum sample representations and continue adding samples from additional clusters to verify if the cluster scores improve or remain similar. In Table V, we observe that all the 5 clusters in each data set have similar SS that implies the clusters are stable in terms of distinction from samples of other clusters. Also, we observe that the CH score increases as samples of additional clusters get introduced. This further strengthens the stability of the clusters. 2) Cluster Transformation: So far, we have assessed static clusters for user-journeys. However, since user-journeys are constantly evolving over time, dynamically transforming customers from one cluster to another by targeted campaigns becomes a distinct possibility. Here, we analyze the effort required to transform user-journeys from one cluster to another in terms of EMD computed between each pair of user clusters in Fig. 7.
We observe the following similarities across user-behaviors for both the data sets. First, Impulsive Shoppers are most distinctive (high EMDs) from other clusters since these shoppers have greater interactions, higher views and lower carting events for non-purchasing over purchasing events. Second, all other clusters demonstrate significant variations in featurelevel distribution (non-zero non diagonal EMDs), which implies varying efforts are required to transform users across specific clusters. Additionally, from Fig. 7 (b) and Fig. 6 (b), we observe that for the electronics dataset, New Shoppers are highly distinctive at feature level, with high views for non-purchases and no other visible trend in features. This cluster requires the highest amount of nudge to convert to other clusters. Also, we observe that Decisive Shoppers and Brand Shoppers have similar purchase predicting nature (low EMD between these clusters).
3) Predictive Robustness: Finally, we assess the impact of sample neighborhoods on new/unlabelled data for each user-cluster using PLL. The accuracy and F 1 plots when a fraction p of randomly sampled labels are dropped and recomputed using k-NN, for p = [0.1, 0.2..0.9] are shown in Fig. 8. While the EMD analysis provides a snapshot of the inter-convert ability across clusters, the PLL curves provide a more dynamic view of the predictive stability for purchasing events per-cluster based on sample neighborhoods to support new/ongoing/unlabelled user-journeys. From Fig.  8, we observe that New Shoppers are the most predictable and stable cluster across datasets. However, from Fig. 8 (a), (b), we observe that for the cosmetics data set, Returning Shopper cluster has significantly higher P uR of 32.9 over other clusters (general P uR range {19-22}), which leads to higher relative F 1 scores over relative accuracy for this cluster. Also, all clusters apart from the Impulsive Shopper cluster, have relatively stable neighborhoods (slight variation in PLL curves), which further strengthens or previous finding from EMD analysis that Impulsive Shoppers are most different from other clusters at feature and neighborhood levels.
Further, from Fig. 8 (c), (d) we observe that for the electronics dataset, the cluster ordering in accuracy-based PLL curves and F 1-based PLL curves remain the same. This is intuitive since for this dataset, the P uR has a narrow variation range of {6.4-8.5} for non-New Shopper clusters, which implies similar purchase to non-purchase ratios in most clusters. Further, for the electronics dataset, Brand Shopper cluster has a highly unpredictable neighborhood, implying more productlevel features may be necessary to accurately classify purchase events for this cluster. Thus, predictive models, EMD and PLLbased cluster analysis together can better provide a holistic understanding of the varying shopping behaviors.

IV. CONCLUSIONS AND DISCUSSION
In this work we present an analytical system for prediction of purchase vs. non-purchasing events at a session and userjourney level that can scale across product-level, shopping portal-level and data-size specifications. We analyze two data sets, cosmetics (2GB) [3] and electronics (15GB) [4], with the following three major conclusions. First, at session-level we find that session-level feature based LSTM models have 91-98% accuracy and 73-99% recall for predicting a purchase event compared to eventbased sequence models in [5] that have 90-92% accuracy and 14-25% recall, respectively. Also, our analysis shows that session-level purchase prediction is highly sensitive to shopping platform-related features. For instance, for the cosmetics dataset, the remove from cart event type leads to better classification performance when compared to the electronics dataset that does not have this event type. Thus, we conclude (a) accuracy for PLL on Cosmetics dataset.  that session-level features, and variations in event-types can influence session-based nudge models. Second, we analyze the user-product interaction journey using supervised and unsupervised methods, such that five distinctive clusters representing specific purchasing behaviors are identified. Next, we use the TPOT AutoML package [9] to fit the best classifiers for predicting a purchasing journey. Our analysis shows that for journey level predictions, productlevel features such as variations in product cost, brand etc. represented in the electronics data set are significant for purchase predictions (accuracy/recall of 99/98%). Also, we observe that purchase prediction can vary significantly across clusters. Thus, purchase predictability per customer cluster plays a key role in designing effective strategic marketing campaigns.
Third, we analyze each user-behavior cluster in terms of cluster formation, capability for transformation to other clusters and predictive robustness using semi-supervised learning (PLL method). We observe that for both data sets, majority of the user-journeys samples belong to clusters representing New Shoppers, who have a higher tendency to research a product than to make the actual purchase. Also, there are other minority clusters that demonstrate varying degrees of engagement and purchasing intent. Marketing campaigns for users must consider not only the purchase ratio per-cluster but also the capability of the cluster to handle new/unseen/unlabelled or ongoing user journeys. For instance, the New Shopper clusters have least purchase to non-purchase journey ratio, but they are most stable to unlabelled data. Also, we observe that the Impulsive Shopper cluster is significantly different from the others in terms of the EMD metric, which makes these shoppers easy to detect but difficult to convert. In this work, session-level and user-journey level analysis has been kept separate. Future works may be directed towards utilizing the journey-level cluster information combined with session-level features for enhanced nudge modeling at session-level.