A Classification Problem with Python — Homesite Quote Conversion | by Shirley Chen | Towards Data Science

Homesite, a leading provider of homeowners insurance, does not currently have a dynamic conversion rate model that can give them confidence a quoted price will lead to a purchase. Using an anonymized database of information on customer and sales activity, including property and coverage information, they are challenging us to predict which customers will purchase a given quote. Accurately predicting conversion would help Homesite better understand the impact of proposed pricing changes and maintain an ideal portfolio of customer segments.
The dataset represents the activity of a large number of customers who are interested in buying policies from Homesite. Each QuoteNumber corresponds to a potential customer and the QuoteConversion_Flag indicates whether the customer purchased a policy. The provided features are anonymized and provide a rich representation of the prospective customer and policy. They include specific coverage information, sales information, personal information, property information, and geographic information. Our task is to predict QuoteConversion_Flag for each QuoteNumber in the test set.
Let’s read the data first and take a look at this data.
Training Dataset: a subset to train a model.
Test Dataset: a subset to test the trained model.
trainfile = r'RevisedHomesiteTrain.csv'train_data = pd.read_csv(trainfile)testfile = r'RevisedHomesiteTest.csv'test_data = pd.read_csv(testfile)print(train_data.shape)print(train_data.head())print(test_data.shape)print(test_data.head())
We copy train data excluding target and separate train data and test data. Then, we select just target column.
trainData_Copy = train_data.drop('QuoteNumber', axis=1).iloc[:, :-1].copy()testData_Copy = test_data.drop('QuoteNumber', axis=1).iloc[:, :-1].copy()X_train = trainData_CopyX_test = testData_Copyy_train = train_data["QuoteConversion_Flag"]y_train = train_data.iloc[:, -1]y_test = test_data.iloc[:, -1]
from sklearn.model_selection import train_test_splitX_train1, X_val, y_train1, y_val = train_test_split(X_train, y_train, test_size = 0.2)
A decision tree is a decision support tool that uses a tree-like graph or model of decisions and their possible consequences, including chance event outcomes, resource costs, and utility. It is one way to display an algorithm that only contains conditional control statements. A Decision Tree Classifier functions by breaking down a dataset into smaller and smaller subsets based on different criteria. Different sorting criteria will be used to divide the dataset, with the number of examples getting smaller with every division.
from sklearn.tree import DecisionTreeClassifierclf = DecisionTreeClassifier()clf.fit(X_train1, y_train1)clf_predict = clf.predict(X_val)
Kaggle score of Decision Tree Classifier: 0.94499
Random Forests are an ensemble learning method that fit multiple Decision Trees on subsets of the data and average the results.
from sklearn.ensemble import RandomForestClassifierrfc = RandomForestClassifier()rfc.fit(X_train, y_train)rfc_predict = rfc.predict(X_test)
Kaggle score of Random Forest Classifier: 0.91963
K-Nearest Neighbors operates by checking the distance from some test example to the known values of some training example. The group of data points/class that would give the smallest distance between the training points and the testing point is the class that is selected.
from sklearn.neighbors import KNeighborsClassifierknc = KNeighborsClassifier()knc.fit(X_train, y_train)knc_predict = knc.predict(X_test)
Kaggle score of K-Nearest Neighbor Classifier: 0.60289
Neural Networks are a machine learning algorithm that involves fitting many hidden layers used to represent neurons that are connected with synaptic activation functions. These essentially use a very simplified model of the brain to model and predict data.
from sklearn.neural_network import MLPClassifiermlp = MLPClassifier()mlp.fit(X_train, y_train)mlp_predict = mlp.predict(X_test)
Kaggle score of Neural Networks Classifier: 0.86759
Gradient boosting classifiers are a group of machine learning algorithms that combine many weak learning models together to create a strong predictive model. Decision trees are usually used when doing gradient boosting.
from sklearn.ensemble import GradientBoostingClassifierabc = GradientBoostingClassifier()abc.fit(X_train, y_train)abc_predict = abc.predict(X_test)
Kaggle score of Neural Networks Classifier: 0.95882
Based on the model I create, first we can realize that gradient boosting classifier get the highest Kaggle score. Also, gradient boosting models are becoming popular because of their effectiveness at classifying complex datasets, and have recently been used to win many Kaggle data science competitions.
To sum up, we use the different classification methods(Decision Tree, Random Forest, K-Nearest Neighbors, Neural Networks, and Gradient Boosting) from Scikit Learn to predict the probability that a customer would buy the quoted insurance plan and focus on the prediction accuracy. The purpose is to learn a model that can maximize the prediction accuracy, compare different algorithms, and select the best-performing one.
Source code that created this post can be found here.
Thank you so much for reading my article! Hi, I’m Shirley, currently studying for a Master Degree in MS-Business Analytics at ASU. If you have questions, please don’t hesitate to contact me!
Email me at kchen122@asu.edu and feel free to connect me on LinkedIn!