Gopala Krishna commited on
Commit
8cb45d3
·
1 Parent(s): 46d9da1

working with File, Customer1, Customer2 inputs

Browse files
.vs/UBCFProductRecommendations/FileContentIndex/23001fe7-f3c2-40de-ac4d-18f66948daf0.vsidx DELETED
Binary file (455 Bytes)
 
.vs/UBCFProductRecommendations/FileContentIndex/4de817bd-07d4-46fb-b29a-c52bae7ffd85.vsidx DELETED
Binary file (587 Bytes)
 
.vs/UBCFProductRecommendations/FileContentIndex/56a4bbc1-0e13-4544-9452-68681c9eecbc.vsidx DELETED
Binary file (8.62 kB)
 
.vs/UBCFProductRecommendations/FileContentIndex/7ff40909-b5d8-4c88-8906-d6cc681c52b1.vsidx ADDED
Binary file (11.7 kB). View file
 
.vs/UBCFProductRecommendations/FileContentIndex/800f1087-579e-4bc8-8fc3-587302d5fa2d.vsidx ADDED
Binary file (537 Bytes). View file
 
.vs/UBCFProductRecommendations/FileContentIndex/c3aa39ba-89f8-4970-a24f-fff79ecea051.vsidx ADDED
Binary file (224 Bytes). View file
 
.vs/UBCFProductRecommendations/v17/.wsuo CHANGED
Binary files a/.vs/UBCFProductRecommendations/v17/.wsuo and b/.vs/UBCFProductRecommendations/v17/.wsuo differ
 
.vs/VSWorkspaceState.json CHANGED
@@ -2,6 +2,6 @@
2
  "ExpandedNodes": [
3
  ""
4
  ],
5
- "SelectedNode": "\\C:\\Python\\Programs\\Gradio\\HuggingSpace\\UBCFProductRecommendations",
6
  "PreviewInSolutionExplorer": false
7
  }
 
2
  "ExpandedNodes": [
3
  ""
4
  ],
5
+ "SelectedNode": "\\app.py",
6
  "PreviewInSolutionExplorer": false
7
  }
.vs/slnx.sqlite CHANGED
Binary files a/.vs/slnx.sqlite and b/.vs/slnx.sqlite differ
 
Online_Retail.xlsx → UBCF_Online_Retail.xlsx RENAMED
File without changes
app.py CHANGED
@@ -1,48 +1,62 @@
1
-
2
  import pandas as pd
3
  from sklearn.metrics.pairwise import cosine_similarity
4
-
5
- # Read data source Excel files.
6
- df1 = pd.read_excel('Online_Retail.xlsx')
7
- df1a = df1.dropna(subset=['CustomerID'])
8
-
9
- # Create CustomerID vs Item (Purchased Items, by StockCode) matrix by pivot table function.
10
- CustomerID_Item_matrix = df1a.pivot_table(
11
- index='CustomerID',
12
- columns='StockCode',
13
- values='Quantity',
14
- aggfunc='sum'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  )
16
 
17
- # Update illustration of the matrix, 1 to represent customer have purchased item, 0 to represent customer haven't purchased.
18
- CustomerID_Item_matrix = CustomerID_Item_matrix.applymap(lambda x: 1 if x > 0 else 0)
19
-
20
- # Create User to User similarity matrix.
21
- user_to_user_similarity_matrix = pd.DataFrame(
22
- cosine_similarity(CustomerID_Item_matrix)
23
- )
24
-
25
- # Update index to corresponding CustomerID.
26
- user_to_user_similarity_matrix.columns = CustomerID_Item_matrix.index
27
- user_to_user_similarity_matrix['CustomerID'] = CustomerID_Item_matrix.index
28
- user_to_user_similarity_matrix = user_to_user_similarity_matrix.set_index('CustomerID')
29
-
30
- # Display CustomerID (12702) purchased items.
31
- items_purchased_by_X = set(CustomerID_Item_matrix.loc[12702.0].iloc[
32
- CustomerID_Item_matrix.loc[12702.0].to_numpy().nonzero()].index)
33
-
34
- # Display CustomerID (14608) purchased items.
35
- items_purchased_by_Y = set(CustomerID_Item_matrix.loc[14608.0].iloc[
36
- CustomerID_Item_matrix.loc[14608.0].to_numpy().nonzero()].index)
37
-
38
- # Find out items which purchased by X (12702) but not yet purchased by Y (14608).
39
- items_to_recommend_to_Y = items_purchased_by_X - items_purchased_by_Y
40
-
41
- # Display the list of items recommended for Y (14608) with item Description.
42
- print(df1a.loc[
43
- df1a['StockCode'].isin(items_to_recommend_to_Y),
44
- ['StockCode', 'Description']
45
- ].drop_duplicates().set_index('StockCode'))
46
-
47
-
48
-
 
 
1
  import pandas as pd
2
  from sklearn.metrics.pairwise import cosine_similarity
3
+ import gradio as gr
4
+
5
+ def recommend_items(file, customer_id_1, customer_id_2):
6
+ # Read data source Excel file.
7
+ df1 = pd.read_excel("UBCF_Online_Retail.xlsx")
8
+ df1a = df1.dropna(subset=['CustomerID'])
9
+
10
+ # Create CustomerID vs Item (Purchased Items, " StockCode) matrix by pivot table function.
11
+ CustomerID_Item_matrix = df1a.pivot_table(
12
+ index='CustomerID',
13
+ columns='StockCode',
14
+ values='Quantity',
15
+ aggfunc='sum'
16
+ )
17
+
18
+ # Update illustration of the matrix, 1 to represent customer have purchased item, 0 to represent customer haven't purchased.
19
+ CustomerID_Item_matrix = CustomerID_Item_matrix.applymap(lambda x: 1 if x > 0 else 0)
20
+
21
+ # Create User to User similarity matrix.
22
+ user_to_user_similarity_matrix = pd.DataFrame(
23
+ cosine_similarity(CustomerID_Item_matrix)
24
+ )
25
+
26
+ # Update index to corresponding CustomerID.
27
+ user_to_user_similarity_matrix.columns = CustomerID_Item_matrix.index
28
+ user_to_user_similarity_matrix['CustomerID'] = CustomerID_Item_matrix.index
29
+ user_to_user_similarity_matrix = user_to_user_similarity_matrix.set_index('CustomerID')
30
+
31
+ # Display CustomerID (customer_id_1) purchased items.
32
+ items_purchased_by_X = set(CustomerID_Item_matrix.loc[customer_id_1].iloc[
33
+ CustomerID_Item_matrix.loc[customer_id_1].to_numpy().nonzero()].index)
34
+
35
+ # Display CustomerID (customer_id_2) purchased items.
36
+ items_purchased_by_Y = set(CustomerID_Item_matrix.loc[customer_id_2].iloc[
37
+ CustomerID_Item_matrix.loc[customer_id_2].to_numpy().nonzero()].index)
38
+
39
+ # Find out items which purchased by X (customer_id_1) but not yet purchased by Y (customer_id_2).
40
+ items_to_recommend_to_Y = items_purchased_by_X - items_purchased_by_Y
41
+
42
+ # Return the list of items recommended for Y (customer_id_2) with item Description.
43
+ return df1a.loc[
44
+ df1a['StockCode'].isin(items_to_recommend_to_Y),
45
+ ['StockCode', 'Description']
46
+ ].drop_duplicates().set_index('StockCode')
47
+
48
+ # Create a Gradio interface
49
+ iface = gr.Interface(
50
+ fn=recommend_items,
51
+ inputs=[
52
+ gr.inputs.File(label="Excel file (.xlsx)"),
53
+ gr.inputs.Number(label="Customer ID 1"),
54
+ gr.inputs.Number(label="Customer ID 2"),
55
+ ],
56
+ outputs="dataframe",
57
+ title="Item Recommendation System",
58
+ description="This system recommends items for a customer based on another customer's purchase history.",
59
+ allow_flagging=False
60
  )
61
 
62
+ iface.launch()