import pandas as pd
import numpy as np
Preprocessing Data
Importing data
bookings_data = pd.read_csv("AncillaryScoring_insurance.csv")
bookings_data.head()
bookings_data.info()
Dropping columns that is not needed in MBA
bookings_data.drop(columns=["PAXCOUNT", "SALESCHANNEL", "TRIPTYPEDESC", "PURCHASELEAD", "LENGTHOFSTAY", "flight_hour", "flight_day", "ROUTE", "geoNetwork_country", "INS_FLAG", "flightDuration_hour"], inplace=True)
bookings_data
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
bookings_data.drop(columns=["Id"], inplace=True)
Frequent Itemsets
Identifying frequent itemsets
frequent_itemsets = apriori(bookings_data, min_support=0.05, use_colnames=True).sort_values("support",ascending=False)
frequent_itemsets.head(10)
Visualizing frequent itemsets
Rephrasing itemset names to alphabets
data = []
for i in range(0, len(frequent_itemsets)):
new_item_set = []
old_item_sets = list(frequent_itemsets.loc[i]['itemsets'])
for j in old_item_sets:
new_item_set.append(j[0])
new_data_item = [new_item_set, frequent_itemsets.loc[i]['support']]
data.append(new_data_item)
df = pd.DataFrame(data, columns=['Frequent Item Sets', 'Support'])
Associative Rule Mining
Associative rules for frequent itemsets
assoc_rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1).sort_values("lift",ascending=False).reset_index(drop=True)
assoc_rules
[3, 5, 10] --> FNB
[6, 9, 11] --> Baggage
[0]
Filtering associative rules for frequent itemsets with higher support scores
FNB_assoc_rules = assoc_rules.filter(items=[3, 5, 10], axis=0)
baggage_assoc_rules = assoc_rules.filter(items=[6, 9, 11], axis=0)
both_assoc_rules = assoc_rules.filter(items=[0], axis=0)
FNB_assoc_rules
def update_consequents(item):
new_item_set = []
old_item_sets = list(item)
for j in old_item_sets:
new_item_set.append(j[0])
return new_item_set
Associative rules for frequent itemset FNB
FNB_assoc_rules['consequents'] = FNB_assoc_rules['consequents'].apply(lambda x: update_consequents(x))
FNB_assoc_rules
Associative rules for frequent itemset baggage
baggage_assoc_rules['consequents'] = baggage_assoc_rules['consequents'].apply(lambda x: update_consequents(x))
baggage_assoc_rules
Associative rule for frequent itemset FNB and Baggage
both_assoc_rules['consequents'] = both_assoc_rules['consequents'].apply(lambda x: update_consequents(x))
both_assoc_rules