-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineLearning_NextBestProduct.py
More file actions
91 lines (42 loc) · 1.66 KB
/
Copy pathMachineLearning_NextBestProduct.py
File metadata and controls
91 lines (42 loc) · 1.66 KB
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
86
87
#!/usr/bin/env python
# coding: utf-8
# In[16]:
#!pip install mlextend
# In[17]:
import numpy as np
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
# In[18]:
# product_sets = pd.read_csv("C:/NextBestProd/ProductSetDetails.csv")
# product_sets = pd.read_csv("C:/NextBestProd/ProductKeyDetails.csv")
product_sets = pd.read_csv("C:/NextBestProd/ProductWOFC.csv")
product_sets.head(20)
# # Data Preparation
# In[19]:
product_sets = pd.pivot_table(data=product_sets,index='INDNUM',columns='PRODUCT',values='Quantity', aggfunc='sum',fill_value=0)
product_sets.head(10)
# In[20]:
def convert_into_binary(x):
if x > 0:
return 1
else:
return 0
# In[21]:
product_sets = product_sets.applymap(convert_into_binary)
# # Apply Apriori Algorithm
# In[22]:
#call apriori function and pass minimum support here we are passing 0.1%. means 1/10 times in total number of transaction that product was present.
frequent_productsets = apriori(product_sets, min_support=0.01, use_colnames=True)
frequent_productsets.head(50)
# # Apply Association Rules
# In[23]:
# we have association rules which need to put on frequent productset.
# here we are setting based on lift and has minimum lift as 1
rules_mlxtend = association_rules(frequent_productsets, metric="lift", min_threshold=0.5)
rules_mlxtend
# In[24]:
# rules_mlxtend.rename(columns={'antecedents':'lhs','consequents':'rhs'})
# as based business use case we can sort based on confidence and lift.
rules_mlxtend[ (rules_mlxtend['lift'] >= 1) & (rules_mlxtend['confidence'] >= 0.1) ]
# In[ ]: