File size: 2,436 Bytes
8c3633d
 
8cb45d3
 
ff26c0e
8cb45d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff26c0e
 
8cb45d3
c9c6e23
 
8c3633d
c9c6e23
 
 
 
 
8c3633d
8cb45d3
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
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
import gradio as gr

def recommend_items(customer_id_1, customer_id_2):
    # Read data source Excel file.
    df1 = pd.read_excel("UBCF_Online_Retail.xlsx")
    df1a = df1.dropna(subset=['CustomerID'])

    # Create CustomerID vs Item (Purchased Items, " StockCode) matrix by pivot table function.
    CustomerID_Item_matrix = df1a.pivot_table(
        index='CustomerID',
        columns='StockCode',
        values='Quantity',
        aggfunc='sum'
    )

    # Update illustration of the matrix, 1 to represent customer have purchased item, 0 to represent customer haven't purchased.
    CustomerID_Item_matrix = CustomerID_Item_matrix.applymap(lambda x: 1 if x > 0 else 0)

    # Create User to User similarity matrix.
    user_to_user_similarity_matrix = pd.DataFrame(
        cosine_similarity(CustomerID_Item_matrix)
    )

    # Update index to corresponding CustomerID.
    user_to_user_similarity_matrix.columns = CustomerID_Item_matrix.index
    user_to_user_similarity_matrix['CustomerID'] = CustomerID_Item_matrix.index
    user_to_user_similarity_matrix = user_to_user_similarity_matrix.set_index('CustomerID')

    # Display CustomerID (customer_id_1) purchased items.
    items_purchased_by_X = set(CustomerID_Item_matrix.loc[customer_id_1].iloc[
        CustomerID_Item_matrix.loc[customer_id_1].to_numpy().nonzero()].index)

    # Display CustomerID (customer_id_2) purchased items.
    items_purchased_by_Y = set(CustomerID_Item_matrix.loc[customer_id_2].iloc[
        CustomerID_Item_matrix.loc[customer_id_2].to_numpy().nonzero()].index)

    # Find out items which purchased by X (customer_id_1) but not yet purchased by Y (customer_id_2).
    items_to_recommend_to_Y = items_purchased_by_X - items_purchased_by_Y

    # Return the list of items recommended for Y (customer_id_2) with item Description.
    return df1a.loc[
        df1a['StockCode'].isin(items_to_recommend_to_Y),
        ['StockCode', 'Description']
    ].drop_duplicates().set_index('StockCode')

# Create a Gradio interface
iface = gr.Interface(
    fn=recommend_items,
    inputs=[
        gr.inputs.Number(label="Customer ID 1",default=12702),
        gr.inputs.Number(label="Customer ID 2",default=14608),
    ],
    outputs=gr.outputs.Dataframe(label="Recommended Items for Customer 2",type="pandas"),
    allow_flagging=False
)
iface.launch()





iface.launch()