Product Recommendation based on Content Similarity


Content based Recommendation is system that suggesting you item or procudt that might be of interest to you based on anather product content that you already interested (history). Most often, these systems recommend related item based on similarity content such as title, category, product description,etc.

Content based recommendation use actual attributes of the item itself to recommend similar products. This makes a ton of sense, as it's how we actually shop in the real world. We go into the supermarket and look at all the items, which are probably physically arranged on the shelf according to brand, price, or category.

3G External Antena Modem Bolt Slim & Max 3dbi ts9 - e5776

Let say that you interested in above product, from title we know that its "external antena" for 3G Modem. very easy for human to find similar item becouse we can process unstructured data like text, image or sound easely. Computer is very different from human when processing unstructured informatio.

Introduction to dataset

In this project we are going to create product recommendation based on title item. This is not simplified the problem but we chose this strategy becouse it can give us better recommendation compaire to recommendation based on discription.

import numpy as np
import pandas as pd
pd.set_option('display.max_colwidth', -1)

data_source = 'data/Jkt-Index-tiny.csv'
ds = pd.read_csv(data_source)
ds['title']
0     3G External Antena Modem Bolt Slim & Max 3dbi ts9 - e5776 - Black                     
1     4G LTE External Antenna for Modem Routers - CRC9 Connector - White                    
2     4G LTE External Antenna for Modem Routers - SMA Connector - White                     
3     4G LTE External Antenna for Modem Routers - TS9 Connector - White                     
4     4G LTE MIMO External Antenna for Modem Routers - Dual CRC9 Connector - White          
5     4G LTE MIMO External Antenna for Modem Routers - Dual TS9 Connector - White           
6     7dBi GSM 3G Magnetic Base Antenna MB-49A - Black                                      
7     9dBi Rubber Antenna for 2.4Ghz Magnetic Base M24-9-SR-MB - Black                      
8     Antenna DVB-T for Huawei E510 - Black                                                 
9     Antenna for Router Network 2.4GHz 22dbi RP-SMA - Black                                
10    Antenna Penguat Sinyal Handphone 3G 12dbi with Phone Holder - Black                   
11    Antenna Sierra Wireless 875U 595U 8781U - Black                                       
12    External Antenna for Huawei 4G LTE 10dbi - TS9 - White                                
13    FME Extension cable 5 Meter - No Color                                                
14    GSM 3G Magnetic Base Antenna 4.0mm Plug - Black                                       
15    GSM 3G Modem Antenna 4.0mm Plug for Huawei D602 - Black                               
16    Hame Little U USB Connector - White                                                   
17    Huawei Docking Station for Huawei E5830 E5832 - E5-1 Cradle - Gray Silver             
18    Huawei HB4F1 Battery for Mifi E5830 E5832 E585 (E583x) Original - No Color            
19    Kabel Antena Male to Male 1.5 Meter - White                                           
20    Omni Minimax G45 Portable 4G LTE External Antenna 45dBi with TS9 Connector - White    
21    Omni Outdoor Antenna OM-R11-07U + FME Extension Cable 5 meter - No Color              
22    Pigtail Huawei - No Color                                                             
23    Pigtail Huawei E5331 - No Color                                                       
24    Pigtail Huawei U8300 - Black                                                          
25    Pigtail OPTION GlobeTrotter - No Color                                                
26    Pigtail Sierra Wireless 312U - Black                                                  
27    Pigtail Sierra Wireless 753s 754s - Black                                             
28    Pigtail Sierra Wireless 875 - No Color                                                
29    Pigtail ZTE MF633 - No Color                                                          
30    Powerfull 3G Magnetic Antenna MB-11F (Hanya Antena panjang) - No Color                
31    Powerfull 3G Magnetic Antenna MB-15F (Hanya antena pendek) - No Color                 
32    Softcover Edition RP-SMA Male to Female Cable 174 Antenna Extension Cable - 3m - Black
33    Yagi Antenna YG-R12-11U + FME Extension Cable 5 meter - No Color                      
34    Huawei E220 USB 7.2Mbps - Logo Vodafone (14 Days) - White                             
35    Huawei E220 USB 7.2Mbps - Logo Vodafone (NEW) - White                                 
36    Huawei E3276 - 4G LTE 100 Mbps - Logo STC - White                                     
37    Huawei E3372 4G LTE Cat4 USB Modem - 827F - Black                                     
38    Huawei Vodafone K3765 HSDPA USB Stick - White                                         
39    Huawei Vodafone K4305 HSPA 21.6 Mbps - White                                          
40    Onda MDC833UP HSUPA 10.2Mbps - Black                                                  
41    Option iCON XY 7.2 Mbps - White                                                       
42    Sierra Wireless AirCard 308U HSPA+ 21.6Mbps - White                                   
43    WWAN Ericsson KM266 Mini-Card DELL Wireless 5530 HSPA 3G with GPS - No Color          
44    ZTE MF667 HSPA USB Modem Turkcell VINN 21.6 Mbps - Black                              
45    ZTE Vodafone HSPA 3G MiFi 42 Mbps R209-Z MF65 - White                                 
46    ZTE Vodafone K3772-Z HSUPA 7.2 Mbps - White                                           
Name: title, dtype: object

Training a Model

import numpy as np
import pandas as pd

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel

data_source = 'input/Jkt-Index-tiny.csv.zip'
model = 'model/cosine_similarities-20180310'

def train(ds):
    tf = TfidfVectorizer(analyzer='word',
                         ngram_range=(1, 2),
                         min_df=0,
                         stop_words='english')
    tfidf_matrix = tf.fit_transform(ds['title'])
    return linear_kernel(tfidf_matrix, tfidf_matrix)


ds = pd.read_csv(data_source)
np.save(model, train(ds))

Load Model from file

import numpy as np
import pandas as pd

idx = ??? # Index of item

data_source = 'input/Jkt-Index-tiny.csv.zip'
model = 'model/cosine_similarities-20180310.npy'

#Load dataset to get title
ds = pd.read_csv(data_source)

#Load pretraining model
cosine_similarities = np.load(model)

def get_recommendation(idx, num = 10)
    return cosine_similarities[idx].argsort()[:-num:-1][1:]


similar_indices = get_recommendation(idx)

print (ds.loc[idx,'title'])
print('Simmilar to:', '\n')
print(ds.loc[similar_indices,'title'])

Analize Recommendation

idx = 8

Antenna DVB-T for Huawei E510 - Black
Simmilar to:
Out[9]:
24    Pigtail Huawei U8300 - Black                                             
15    GSM 3G Modem Antenna 4.0mm Plug for Huawei D602 - Black                  
22    Pigtail Huawei - No Color                                                
12    External Antenna for Huawei 4G LTE 10dbi - TS9 - White                   
37    Huawei E3372 4G LTE Cat4 USB Modem - 827F - Black                        
14    GSM 3G Magnetic Base Antenna 4.0mm Plug - Black                          
11    Antenna Sierra Wireless 875U 595U 8781U - Black                          
17    Huawei Docking Station for Huawei E5830 E5832 - E5-1 Cradle - Gray Silver
6     7dBi GSM 3G Magnetic Base Antenna MB-49A - Black                         
Name: title, dtype: object

idx = 46

ZTE Vodafone K3772-Z HSUPA 7.2 Mbps - White
Simmilar to:
Out[10]:
45    ZTE Vodafone HSPA 3G MiFi 42 Mbps R209-Z MF65 - White    
39    Huawei Vodafone K4305 HSPA 21.6 Mbps - White             
41    Option iCON XY 7.2 Mbps - White                          
44    ZTE MF667 HSPA USB Modem Turkcell VINN 21.6 Mbps - Black 
29    Pigtail ZTE MF633 - No Color                             
40    Onda MDC833UP HSUPA 10.2Mbps - Black                     
38    Huawei Vodafone K3765 HSDPA USB Stick - White            
35    Huawei E220 USB 7.2Mbps - Logo Vodafone (NEW) - White    
34    Huawei E220 USB 7.2Mbps - Logo Vodafone (14 Days) - White
Name: title, dtype: object