-
Notifications
You must be signed in to change notification settings - Fork 0
/
Association_Rule_Based_Recommender_System.py
85 lines (63 loc) · 3.13 KB
/
Association_Rule_Based_Recommender_System.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
############################################
# ASSOCIATION RULE LEARNING (BİRLİKTELİK KURALI ÖĞRENİMİ)
############################################
# 1. Veri Ön İşleme
# 2. ARL Veri Yapısını Hazırlama (Invoice-Product Matrix)
# 3. Birliktelik Kurallarının Çıkarılması
# 4. Çalışmanın Scriptini Hazırlama
# 5. Sepet Aşamasındaki Kullanıcılara Ürün Önerisinde Bulunmak
# !pip install mlxtend
import pandas as pd
pd.set_option('display.max_columns', None)
# pd.set_option('display.max_rows', None)
pd.set_option('display.width', 500)
# çıktının tek bir satırda olmasını sağlar.
pd.set_option('display.expand_frame_repr', False)
from mlxtend.frequent_patterns import apriori, association_rules
from utils.aykiri_degisken_analizi import replace_with_thresholds
df_ = pd.read_excel("CRM Analitiği/datasets/online_retail_II.xlsx", sheet_name="Year 2010-2011")
def retail_data_pred(dataframe):
# 1. Veri Ön İşleme
dataframe.dropna(inplace=True)
dataframe = dataframe[~dataframe["Invoice"].str.contains("C", na=False)]
dataframe = dataframe[dataframe["Quantity"] > 0]
dataframe = dataframe[dataframe["Price"] > 0]
replace_with_thresholds(dataframe, "Quantity")
replace_with_thresholds(dataframe, "Price")
return dataframe
def create_invoice_product_df(dataframe, id=False):
if id:
return dataframe.groupby(['Invoice', "StockCode"])['Quantity'].sum().unstack().fillna(0). \
applymap(lambda x: 1 if x > 0 else 0)
return dataframe.groupby(['Invoice', 'Description'])['Quantity'].sum().unstack().fillna(0). \
applymap(lambda x: 1 if x > 0 else 0)
def check_id(dataframe, stock_code):
product_name = dataframe[dataframe["StockCode"] == stock_code]["Description"].values[0]
print(product_name)
def create_rules(dataframe, id=True, country="France"):
# 2. ARL Veri Yapısını hazırlama
dataframe = dataframe[dataframe["Country"] == country]
dataframe = create_invoice_product_df(dataframe, id)
# 3. Birliktelik kurallarının çıkarılması
frequent_itemsets = apriori(dataframe, min_support=0.01, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="support", min_threshold=0.01)
return rules
df = df_.copy()
df = retail_data_pred(df)
rules = create_rules(df)
rules[(rules["support"] > 0.05) & (rules["lift"] > 5) & (rules["confidence"] > 0.1)] \
.sort_values("confidence", ascending=False)
# Sepet aşamasındaki kullanıcılara ürün önerisinde bulunmak
# Kullanıcı örnek ürün id: 22492
product_id = 22492
check_id(df, product_id)
def arl_recommender(rules_df, rec_count=1, sorting_param="lift"):
sorted_rules = rules_df.sort_values(sorting_param, ascending=False)
recommended_list = []
for i, product in enumerate(sorted_rules["antecedents"]):
if product_id in list(product):
recommended_list.append(list(sorted_rules.iloc[i]["consequents"])[0])
return recommended_list[0: rec_count]
arl_recommender(rules, 2, "confidence")
arl_recommender(rules, 2, "lift")
arl_recommender(rules, 2, "support")