{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ " #
Predicting Customer Propensity using Classification Algorithms
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we will show you how to predict customer propensity to buy the product based on his/her interactions on the website. Using that propensity, if a certain threshold are reached we will then decide whether to assign an agent to offer a chat to the customer. We will also be using different classification algorithms to show how each algorithms are coded.\n", "\n", "Because we will only be using sample data for exercise purposes, the data set that we'll be using here are very small (500 rows of data). Thus, we might not get a real accurate predictions out of it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - - -" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Call to Action\n", "* Analyse real-time customer's actions on website\n", "* Predict the propensity score\n", "* Offer chat once propensity score exceeds threshold" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading and Viewing Data\n", "We will load the data file then checkout the summary statistics and columns for that file." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "SESSION_ID int64\n", "IMAGES int64\n", "REVIEWS int64\n", "FAQ int64\n", "SPECS int64\n", "SHIPPING int64\n", "BOUGHT_TOGETHER int64\n", "COMPARE_SIMILAR int64\n", "VIEW_SIMILAR int64\n", "WARRANTY int64\n", "SPONSORED_LINKS int64\n", "BUY int64\n", "dtype: object" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "from sklearn.model_selection import train_test_split, cross_val_score\n", "from sklearn.naive_bayes import GaussianNB\n", "from sklearn.linear_model import LogisticRegression, SGDClassifier\n", "from sklearn.neighbors import KNeighborsClassifier\n", "from sklearn.tree import DecisionTreeClassifier\n", "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.svm import SVC\n", "import sklearn.metrics\n", "\n", "customer_data = pd.read_csv(\"Data/browsing.csv\")\n", "\n", "# After loading the data, we look at the data types to make sure that the data has been loaded correctly.\n", "customer_data.dtypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data contains information about the various links on the website that are clicked by the user during his browsing. This is past data that will be used to build the model.\n", "\n", "- Session ID : A unique identifier for the web browsing session\n", "- Buy : Whether the prospect ended up buying the product\n", "- Other columns : a boolean indicator to show whether the prospect visited that particular page or did the activity mentioned.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " | SESSION_ID | \n", "IMAGES | \n", "REVIEWS | \n", "FAQ | \n", "SPECS | \n", "SHIPPING | \n", "BOUGHT_TOGETHER | \n", "COMPARE_SIMILAR | \n", "VIEW_SIMILAR | \n", "WARRANTY | \n", "SPONSORED_LINKS | \n", "BUY | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1001 | \n", "0 | \n", "0 | \n", "1 | \n", "0 | \n", "1 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "0 | \n", "0 | \n", "
1 | \n", "1002 | \n", "0 | \n", "1 | \n", "1 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "0 | \n", "
2 | \n", "1003 | \n", "1 | \n", "0 | \n", "1 | \n", "1 | \n", "1 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "0 | \n", "0 | \n", "
3 | \n", "1004 | \n", "1 | \n", "0 | \n", "0 | \n", "0 | \n", "1 | \n", "1 | \n", "1 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "
4 | \n", "1005 | \n", "1 | \n", "1 | \n", "1 | \n", "0 | \n", "1 | \n", "0 | \n", "1 | \n", "0 | \n", "0 | \n", "0 | \n", "0 | \n", "
\n", " | SESSION_ID | \n", "IMAGES | \n", "REVIEWS | \n", "FAQ | \n", "SPECS | \n", "SHIPPING | \n", "BOUGHT_TOGETHER | \n", "COMPARE_SIMILAR | \n", "VIEW_SIMILAR | \n", "WARRANTY | \n", "SPONSORED_LINKS | \n", "BUY | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
count | \n", "500.000000 | \n", "500.000000 | \n", "500.0000 | \n", "500.000000 | \n", "500.0000 | \n", "500.000000 | \n", "500.000000 | \n", "500.000000 | \n", "500.000000 | \n", "500.000000 | \n", "500.000000 | \n", "500.000000 | \n", "
mean | \n", "1250.500000 | \n", "0.510000 | \n", "0.5200 | \n", "0.440000 | \n", "0.4800 | \n", "0.528000 | \n", "0.500000 | \n", "0.580000 | \n", "0.468000 | \n", "0.532000 | \n", "0.550000 | \n", "0.370000 | \n", "
std | \n", "144.481833 | \n", "0.500401 | \n", "0.5001 | \n", "0.496884 | \n", "0.5001 | \n", "0.499715 | \n", "0.500501 | \n", "0.494053 | \n", "0.499475 | \n", "0.499475 | \n", "0.497992 | \n", "0.483288 | \n", "
min | \n", "1001.000000 | \n", "0.000000 | \n", "0.0000 | \n", "0.000000 | \n", "0.0000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "
25% | \n", "1125.750000 | \n", "0.000000 | \n", "0.0000 | \n", "0.000000 | \n", "0.0000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "0.000000 | \n", "
50% | \n", "1250.500000 | \n", "1.000000 | \n", "1.0000 | \n", "0.000000 | \n", "0.0000 | \n", "1.000000 | \n", "0.500000 | \n", "1.000000 | \n", "0.000000 | \n", "1.000000 | \n", "1.000000 | \n", "0.000000 | \n", "
75% | \n", "1375.250000 | \n", "1.000000 | \n", "1.0000 | \n", "1.000000 | \n", "1.0000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "
max | \n", "1500.000000 | \n", "1.000000 | \n", "1.0000 | \n", "1.000000 | \n", "1.0000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "
\n",
"Definition: Naïve Bayes algorithm based on Bayes’ theorem with the assumption of independence between every pair of features. Naive Bayes classifiers work well in many real-world situations such as document classification and spam filtering.\n",
"
\n",
"Advantages: This algorithm requires a small amount of training data to estimate the necessary parameters. Naive Bayes classifiers are extremely fast compared to more sophisticated methods.\n",
"
\n",
"Disadvantages: Naive Bayes is is known to be a bad estimator.\n",
"
\n",
"Definition: Logistic regression is a machine learning algorithm for classification. In this algorithm, the probabilities describing the possible outcomes of a single trial are modelled using a logistic function.\n",
"
\n",
"Advantages: Logistic regression is designed for this purpose (classification), and is most useful for understanding the influence of several independent variables on a single outcome variable.\n",
"
\n",
"Disadvantages: Works only when the predicted variable is binary, assumes all predictors are independent of each other, and assumes data is free of missing values.\n",
"
\n",
"Definition: Stochastic gradient descent is a simple and very efficient approach to fit linear models. It is particularly useful when the number of samples is very large. It supports different loss functions and penalties for classification.\n",
"
\n",
"Advantages: Efficiency and ease of implementation.\n",
"
\n",
"Disadvantages: Requires a number of hyper-parameters and it is sensitive to feature scaling.\n",
"
\n",
"Definition: Neighbours based classification is a type of lazy learning as it does not attempt to construct a general internal model, but simply stores instances of the training data. Classification is computed from a simple majority vote of the k nearest neighbours of each point.\n",
"
\n",
"Advantages: This algorithm is simple to implement, robust to noisy training data, and effective if training data is large.\n",
"
\n",
"Disadvantages: Need to determine the value of K and the computation cost is high as it needs to computer the distance of each instance to all the training samples.\n",
"
\n",
"Definition: Given a data of attributes together with its classes, a decision tree produces a sequence of rules that can be used to classify the data.\n",
"
\n",
"Advantages: Decision Tree is simple to understand and visualise, requires little data preparation, and can handle both numerical and categorical data.\n",
"
\n",
"Disadvantages: Decision tree can create complex trees that do not generalise well, and decision trees can be unstable because small variations in the data might result in a completely different tree being generated.\n",
"
\n",
"Definition: Random forest classifier is a meta-estimator that fits a number of decision trees on various sub-samples of datasets and uses average to improve the predictive accuracy of the model and controls over-fitting. The sub-sample size is always the same as the original input sample size but the samples are drawn with replacement.\n",
"
\n",
"Advantages: Reduction in over-fitting and random forest classifier is more accurate than decision trees in most cases.\n",
"
\n",
"Disadvantages: Slow real time prediction, difficult to implement, and complex algorithm.\n",
"
\n",
"Definition: Support vector machine is a representation of the training data as points in space separated into categories by a clear gap that is as wide as possible. New examples are then mapped into that same space and predicted to belong to a category based on which side of the gap they fall.\n",
"
\n",
"Advantages: Effective in high dimensional spaces and uses a subset of training points in the decision function so it is also memory efficient.\n",
"
\n",
"Disadvantages: The algorithm does not directly provide probability estimates, these are calculated using an expensive five-fold cross-validation.\n",
"