<jupyter_start><jupyter_text>Case Study 1. Objective1. Predicting most probable policy to a new customer2. Recommending alternate policy to existing customers3. Factors affecting life time value4. Understanding demographics and customer behaviour Let's start with first part 2. Section 1 - Data Extraction<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

#Reading the dataset in a dataframe using Pandas
df = pd.read_csv("policy_data.csv", index_col = 'Customer') 
columns = ['State','Coverage','Education','Gender','Income','Location Code',
          'Marital Status','Sales Channel','Vehicle Class','Vehicle Size']
df.drop(['EmploymentStatus'], axis = 1, inplace = True)
df.drop(['Customer Lifetime Value'], axis = 1, inplace = True)

df.head()
df.corr()<jupyter_output><empty_output><jupyter_text>3. Exploratory Data AnalysisFirst let us see the distribution of existing customers![one](images/5.png)  Selection of Policy based on customer characteristics![one](images/policy_personal.png) Selection of Policy based on vehicle![one](images/7.png)  4. Modeling<jupyter_code>from sklearn.preprocessing import LabelEncoder
categorical_variables = df.dtypes[df.dtypes == 'object'].index
categorical_variables
le = LabelEncoder()
for var in categorical_variables:
    df[var] = le.fit_transform(df[var])

df.head()
X = df.iloc[:, 1:]
y = df.iloc[:, 0]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 0)
from sklearn.tree import DecisionTreeClassifier
decision = DecisionTreeClassifier()
decision = decision.fit(X_train, y_train)
y_pred = decision.predict(X_test)
from sklearn.metrics import accuracy_score
result = accuracy_score(y_test, y_pred) * 100
result
for df, importance in zip(columns, decision.feature_importances_):
    print(df, importance * 100)
from sklearn.naive_bayes import GaussianNB
GNBClassifier = GaussianNB()
GNBClassifier = GNBClassifier.fit(X_train, y_train.ravel())
y_pred = GNBClassifier.predict(X_test)
result = accuracy_score(y_test, y_pred) * 100
result
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier()
knnClassifier = classifier.fit(X_train, y_train.ravel())
y_pred = knnClassifier.predict(X_test)
result = accuracy_score(y_test, y_pred)*100
result
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(max_depth = 10, min_samples_leaf = 10, max_features= 'auto')
rfClassifier = classifier.fit(X_train, y_train.ravel())
y_pred = rfClassifier.predict(X_test)
result = accuracy_score(y_test, y_pred)*100
result
from sklearn.externals import joblib
joblib.dump(rfClassifier, 'model/nb.pkl')<jupyter_output><empty_output><jupyter_text>5. Serving end points with Flask APIOn localhost 5000![one](images/a.png)![one](images/b.png)![one](images/c.png)  Section 2 - CLV 2.1 - Overall![one](images/20.png)2.2 - Based on Complaints![one](images/24.png)2.3 - Based on Demographics![one](images/21.png)2.4 - Based on Sales![one](images/22.png)2.5 - Based on Personal Characteristic![one](images/23.png)<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

#Reading the dataset in a dataframe using Pandas
df = pd.read_csv("data.csv")

#finding missing values
df.isnull().sum()

from sklearn.preprocessing import LabelEncoder
categorical_variables = df.dtypes[df.dtypes == 'object'].index
categorical_variables
le = LabelEncoder()
for var in categorical_variables:
    df[var] = le.fit_transform(df[var])

df.head()
pd.cut(df['Customer Lifetime Value'], 8).head()
custom_bucket_array = np.linspace(0, 20, 9)
custom_bucket_array
df['Customer Lifetime Value'] = pd.cut(df['Customer Lifetime Value'], custom_bucket_array)
X = df.iloc[:, 1:]
y = df.iloc[:, 0]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 0)
from sklearn.tree import DecisionTreeRegressor
decision = DecisionTreeRegressor(max_depth=2)
decision = decision.fit(X_train, y_train)
y_pred = decision.predict(X_test)
for df, importance in zip(df, decision.feature_importances_):
    print(df, importance * 100)<jupyter_output>('State', 0.0)
('Customer Lifetime Value', 0.0)
('Response', 0.0)
('Coverage', 0.0)
('Education', 0.0)
('Effective To Date', 0.0)
('EmploymentStatus', 0.0)
('Gender', 0.0)
('Income', 0.0)
('Location Code', 0.0)
('Marital Status', 39.16369938736544)
('Monthly Premium Auto', 0.0)
('Months Since Last Claim', 34.031533891309593)
('Months Since Policy Inception', 0.0)
('Number of Open Complaints', 0.0)
('Number of Policies', 0.0)
('Policy Type', 0.0)
('Policy', 0.0)
('Renew Offer Type', 26.804766721324981)
('Sales Channel', 0.0)
('Total Claim Amount', 0.0)
('Vehicle Class', 0.0)
{'fraction_non_alphanumeric': 0.12589122020778162, 'fraction_numerical': 0.033204318598492565, 'mean_word_length': 6.4057315233785825, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '503953', 'license': 'MIT', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1644, 'n_words': 438, 'path': 'master.ipynb', 'repo_name': 'warmachine0609/CaseStudy_InsurancePolicy', 'size': '4909'}