<jupyter_start><jupyter_text>Predicting Prospect Propensity  In this example, we will show you how to predict propensity of prospects. This code example goes with the lesson with the same title. We will use web clicks data about the links clicked by the user while he is browsing to predict his propensity to buy the product. Using that propensity, we will decide whether we want to offer chat to the customer with an agent  Loading and Viewing DataWe will load the data file for this example and checkout summary statistics and columns for that file.<jupyter_code>from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import os
import matplotlib.pylab as plt
from sklearn.model_selection  import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report
import sklearn.metrics

prospect_data = pd.read_csv("browsing.csv")

prospect_data.dtypes<jupyter_output><empty_output><jupyter_text>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.- Session ID : A unique identifier for the web browsing session- Buy : Whether the prospect ended up buying the product- Other columns : a 0 or 1 indicator to show whether the prospect visited that particular page or did the activity mentioned.<jupyter_code># Look at the top records to understand how the data looks like.
prospect_data.head()
#Do summary statistics analysis of the data
prospect_data.describe()<jupyter_output><empty_output><jupyter_text>Perform Correlation Analysis<jupyter_code>prospect_data.corr()['BUY']<jupyter_output><empty_output><jupyter_text>Looking at the correlations above we can see that some features like REVIEWS, BRO_TOGETHER, COMPARE_SIMILAR, WARRANTY and SPONSORED_LINKS have medium correlation to the target variable. We will reduce our feature set to that list of variables.<jupyter_code>#Drop columns with low correlation
predictors = prospect_data[['REVIEWS','BOUGHT_TOGETHER','COMPARE_SIMILAR','WARRANTY','SPONSORED_LINKS']]
targets = prospect_data.BUY<jupyter_output><empty_output><jupyter_text>Training and Testing SplitWe now split the model into training and testing data in the ratio of 70:30<jupyter_code>pred_train, pred_test, tar_train, tar_test  =   train_test_split(predictors, targets, test_size=.3)

print( "Predictor - Training : ", pred_train.shape, "Predictor - Testing : ", pred_test.shape )<jupyter_output>Predictor - Training :  (350, 5) Predictor - Testing :  (150, 5)<jupyter_text>Build Model and Check Accuracy<jupyter_code>from sklearn.naive_bayes import GaussianNB

classifier=GaussianNB()
classifier=classifier.fit(pred_train,tar_train)

predictions=classifier.predict(pred_test)

#Analyze accuracy of predictions
sklearn.metrics.confusion_matrix(tar_test,predictions)

sklearn.metrics.accuracy_score(tar_test, predictions)<jupyter_output><empty_output><jupyter_text>Instead of doing a Yes/No prediction, we can instead do a probability computation to show the probability for the prospect to buy the product<jupyter_code>pred_prob=classifier.predict_proba(pred_test)
pred_prob[0,1]<jupyter_output><empty_output><jupyter_text>The probability above can be read as 35% chance that the prospect will buy the product.  Real time predictionsNow that the model has been built, let us use it for real time predictions. So when the customer starts visiting the pages one by one, we collect that list and then use it to compute the probability. We do that for every new click that comes in.So let us start. The prospect just came to your website. There are no significant clicks. Let us compute the probability. The array of values passed has the values for REVIEWS, BOUGHT_TOGETHER, COMPARE_SIMILAR, WARRANTY and SPONSORED_LINKS. So the array is all zeros to begin with<jupyter_code>browsing_data = np.array([0,0,0,0,0]).reshape(1, -1)
print("New visitor: propensity :",classifier.predict_proba(browsing_data)[:,1] )<jupyter_output>New visitor: propensity : [ 0.03961746]<jupyter_text>So the initial probability is 4%. Now, suppose the customer does a comparison of similar products. The array changes to include a 1 for that function. The new probability will be<jupyter_code>browsing_data = np.array([0,0,1,0,0]).reshape(1, -1)
print("After checking similar products: propensity :",classifier.predict_proba(browsing_data)[:,1] )<jupyter_output>After checking similar products: propensity : [ 0.09898671]<jupyter_text>It goes up. Next, he checks out reviews.<jupyter_code>browsing_data = np.array([1,0,1,0,0]).reshape(1, -1)
print("After checking reviews: propensity :",classifier.predict_proba(browsing_data)[:,1] )<jupyter_output>After checking reviews: propensity : [ 0.57538836]
{'fraction_non_alphanumeric': 0.07111482953357039, 'fraction_numerical': 0.015059610960050198, 'mean_word_length': 6.518867924528302, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '163989', 'license': 'MIT', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1373, 'n_words': 524, 'path': 'Files_Predictive_Customer_Analytics/Notebooks/02_05/.ipynb_checkpoints/Propensity-checkpoint.ipynb', 'repo_name': '99sbr/Predictive-Customer-Analytics', 'size': '4781'}