productizationlabs
commited on
Commit
·
c80d2cb
1
Parent(s):
692c469
Upload app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,15 @@
|
|
1 |
import pandas as pd
|
2 |
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
import gradio as gr
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
return
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
columns='StockCode',
|
17 |
-
values='Quantity',
|
18 |
-
aggfunc='sum'
|
19 |
-
)
|
20 |
-
|
21 |
-
# Update illustration of the matrix, 1 to represent customer have purchased item, 0 to represent customer haven't purchased.
|
22 |
-
CustomerID_Item_matrix = CustomerID_Item_matrix.applymap(lambda x: 1 if x > 0 else 0)
|
23 |
-
|
24 |
-
# Create User to User similarity matrix.
|
25 |
-
user_to_user_similarity_matrix = pd.DataFrame(
|
26 |
-
cosine_similarity(CustomerID_Item_matrix)
|
27 |
-
)
|
28 |
-
|
29 |
-
# Update index to corresponding CustomerID.
|
30 |
-
user_to_user_similarity_matrix.columns = CustomerID_Item_matrix.index
|
31 |
-
user_to_user_similarity_matrix['CustomerID'] = CustomerID_Item_matrix.index
|
32 |
-
user_to_user_similarity_matrix = user_to_user_similarity_matrix.set_index('CustomerID')
|
33 |
-
|
34 |
-
# Display CustomerID (customer_id_1) purchased items.
|
35 |
-
try:
|
36 |
-
items_purchased_by_X = set(CustomerID_Item_matrix.loc[customer_id_1].iloc[
|
37 |
-
CustomerID_Item_matrix.loc[customer_id_1].to_numpy().nonzero()].index)
|
38 |
-
except KeyError:
|
39 |
-
return pd.DataFrame({"Error": ["Customer ID 1 is invalid. Please enter a valid Customer ID"]})
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# Display CustomerID (customer_id_2) purchased items.
|
44 |
-
try:
|
45 |
-
items_purchased_by_Y = set(CustomerID_Item_matrix.loc[customer_id_2].iloc[
|
46 |
-
CustomerID_Item_matrix.loc[customer_id_2].to_numpy().nonzero()].index)
|
47 |
-
except KeyError:
|
48 |
-
return pd.DataFrame({"Error": ["Customer ID 2 is invalid. Please enter a valid Customer ID"]})
|
49 |
-
|
50 |
-
|
51 |
-
# Find out items which purchased by X (customer_id_1) but not yet purchased by Y (customer_id_2).
|
52 |
-
items_to_recommend_to_Y = items_purchased_by_X - items_purchased_by_Y
|
53 |
-
|
54 |
-
# Return the list of items recommended for Y (customer_id_2) with item Description.
|
55 |
-
return df1a.loc[
|
56 |
-
df1a['StockCode'].isin(items_to_recommend_to_Y),
|
57 |
-
['StockCode', 'Description']
|
58 |
-
].drop_duplicates().set_index('StockCode')
|
59 |
-
|
60 |
-
# Create a Gradio interface
|
61 |
-
iface = gr.Interface(
|
62 |
-
fn=recommend_items,
|
63 |
-
inputs=[
|
64 |
-
gr.inputs.Number(label="Customer ID 1"),
|
65 |
-
gr.inputs.Number(label="Customer ID 2"),
|
66 |
-
],
|
67 |
-
outputs=gr.outputs.Dataframe(label="Recommended Items for Customer 2",type="pandas"),
|
68 |
-
theme=gr.themes.Default(primary_hue="slate"),
|
69 |
-
allow_flagging=False
|
70 |
-
)
|
71 |
-
iface.launch()
|
72 |
-
|
|
|
1 |
import pandas as pd
|
2 |
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
import gradio as gr
|
4 |
+
def recommend_items(customer_id_1,customer_id_2):
|
5 |
+
H='Error';G=customer_id_2;F=customer_id_1;D='StockCode';C='CustomerID'
|
6 |
+
try:I=pd.read_excel('UBCF_Online_Retail.xlsx')
|
7 |
+
except FileNotFoundError:return'Error: Excel file not found.'
|
8 |
+
E=I.dropna(subset=[C]);A=E.pivot_table(index=C,columns=D,values='Quantity',aggfunc='sum');A=A.applymap(lambda x:1 if x>0 else 0);B=pd.DataFrame(cosine_similarity(A));B.columns=A.index;B[C]=A.index;B=B.set_index(C)
|
9 |
+
try:J=set(A.loc[F].iloc[A.loc[F].to_numpy().nonzero()].index)
|
10 |
+
except KeyError:return pd.DataFrame({H:['Customer ID 1 is invalid. Please enter a valid Customer ID']})
|
11 |
+
try:K=set(A.loc[G].iloc[A.loc[G].to_numpy().nonzero()].index)
|
12 |
+
except KeyError:return pd.DataFrame({H:['Customer ID 2 is invalid. Please enter a valid Customer ID']})
|
13 |
+
L=J-K;return E.loc[E[D].isin(L),[D,'Description']].drop_duplicates().set_index(D)
|
14 |
+
iface=gr.Interface(fn=recommend_items,inputs=[gr.inputs.Number(label='Customer ID 1'),gr.inputs.Number(label='Customer ID 2')],outputs=gr.outputs.Dataframe(label='Recommended Items for Customer 2',type='pandas'),theme=gr.themes.Default(primary_hue='slate'),allow_flagging=False)
|
15 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|