<jupyter_start><jupyter_text><jupyter_code>!pip install scikit-learn==0.23.1 --quiet
!pip install imblearn --quiet
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
insurance_data = pd.read_csv("https://github.com/dphi-official/Datasets/blob/master/travel_insurance/Training_set_label.csv?raw=true")
insurance_data.info()
insurance_data.describe()
insurance_data['Claim'].value_counts()
insurance_data.groupby(['Claim']).agg('mean')
sns.catplot(x='Gender', kind='count', data=insurance_data)
insurance_data.drop(labels=['Claim'], axis=1).groupby(['Gender'], dropna=False).agg('mean')
insurance_data.groupby(['Gender'], dropna=False).agg({'Claim':'count'})
sns.catplot(y='Agency', kind='count', data=insurance_data)
insurance_data.drop(labels=['Claim'], axis=1).groupby(['Agency'], dropna=False).agg('mean')
sns.catplot(y='Agency Type', kind='count', data=insurance_data)
insurance_data.drop(labels=['Claim'], axis=1).groupby(['Agency Type'], dropna=False).agg('mean')
sns.catplot(y='Distribution Channel', kind='count', data=insurance_data)
sns.catplot(y='Product Name', kind='count', data=insurance_data)
sns.catplot(y='Destination', kind='count', data=insurance_data)
sns.histplot(x='Age', data=insurance_data)
sns.histplot(x='Duration', data=insurance_data)
insurance_data_clean = insurance_data.copy(deep=True)<jupyter_output><empty_output><jupyter_text>Fill the missing values in 'Gender' column with 'Not Specified'<jupyter_code>values_fillna = {'Gender': 'Not Specified'}
insurance_data_clean.fillna(value=values_fillna, inplace=True)
insurance_data_clean.groupby(['Gender'], dropna=False).agg({'Claim':'count'})<jupyter_output><empty_output><jupyter_text>Drop 'Distribution Channel', 'Destination' and 'Agency Type'.<jupyter_code>insurance_data_clean.drop(labels=['Distribution Channel', 'Destination', 'Agency Type'], axis=1, inplace=True)
insurance_data_clean.columns
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(handle_unknown='ignore')
insurance_data_clean = pd.concat([insurance_data_clean, (pd.get_dummies(insurance_data_clean[['Agency', 'Gender', 'Product Name']],drop_first=True, sparse=False))], axis=1)
insurance_data_clean.drop(labels=['Agency', 'Product Name', 'Gender'], axis=1, inplace=True)
insurance_data_clean.info()
from sklearn import preprocessing

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(insurance_data_clean.drop('Claim',axis=1), 
                                                    insurance_data_clean['Claim'], test_size=0.30, 
                                                    random_state=101)
import six
import sys
sys.modules['sklearn.externals.six'] = six
from imblearn.over_sampling import SMOTE

sm = SMOTE(random_state=25, sampling_strategy=1.0)
X_train_res, y_train_res = sm.fit_resample(X_train, y_train)
scaler = preprocessing.StandardScaler().fit(X_train_res)
X_train_res_scaled = scaler.transform(X_train_res)
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(max_iter=2000, verbose=1)
lr.fit(X_train_res, y_train_res)
lr_predictions = lr.predict(X_test)

from sklearn.metrics import accuracy_score
accuracy_score(y_test, lr_predictions)
from sklearn.metrics import confusion_matrix
lr_confusion_matrix = confusion_matrix(y_test, lr_predictions)
print(lr_confusion_matrix)
from sklearn.metrics import classification_report
print(classification_report(y_test,lr_predictions))
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()
dt.fit(X_train_res, y_train_res)
dt_predictions = dt.predict(X_test)
accuracy_score(y_test, dt_predictions)
dt_confusion_matrix = confusion_matrix(y_test, dt_predictions)
print(dt_confusion_matrix)
print(classification_report(y_test,dt_predictions))
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier()
rf.fit(X_train_res, y_train_res)
rf_predictions = rf.predict(X_test)
accuracy_score(y_test, rf_predictions)
rf_confusion_matrix = confusion_matrix(y_test, rf_predictions)
print(rf_confusion_matrix)
print(classification_report(y_test,rf_predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.99      0.99      0.99     14283
           1       0.06      0.07      0.07       195

    accuracy                           0.97     14478
   macro avg       0.53      0.53      0.53     14478
weighted avg       0.97      0.97      0.97     14478
{'fraction_non_alphanumeric': 0.12127992839561423, 'fraction_numerical': 0.020362497202953682, 'mean_word_length': 6.174959871589085, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '422272', 'license': 'MIT', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1500, 'n_words': 248, 'path': 'CWA_Try_Classifiers.ipynb', 'repo_name': 'salilathalye/chats-with-austin', 'size': '4469'}