如何使用tf-idf和余弦相似性构建推荐系统?

时间:2016-08-07 05:09:26

标签: python scikit-learn tf-idf

我一直在努力建立一个啤酒推荐引擎,我决定简单地使用tf-idf和余弦相似性。

到目前为止,这是我的代码:`

import pandas as pd     
import re
import numpy as np 
from bs4 import BeautifulSoup 
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
wnlzer = WordNetLemmatizer()


train = pd.read_csv("labeledTrainData.tsv" , header = 0 ,  \
    delimiter = '\t' , quoting  = 3)


def raw_string_to_list_clean_string( raw_train_review ):
    remove_html = BeautifulSoup( raw_train_review ).text
    remove_punch = re.sub('[^A-Za-z ]' , "" , remove_html)
    token = remove_punch.lower().split()
    srm_token = [wnlzer.lemmatize(i) for i in token if not i in set(stopwords.words('english'))]
    clean_text = " ".join(srm_token)
    return(clean_text)

ready_train_list = []
length  = len(train['review'])
for i in range(0 , length):
    if (i%100 == 0):
        print "doing  %d of  %d of training data set" % (i+1 , length)
    a = raw_string_to_list_clean_string(train['review'][i])
    ready_train_list.append(a)

vectorizer = TfidfVectorizer(analyzer = "word" , tokenizer = None , preprocessor = None , \
    stop_words = None , max_features = 20000)
training_our_vectorizer = vectorizer.fit_transform(ready_train_list)`

现在我知道如何使用余弦相似度,但我无法弄清楚:

  1. 如何使用余弦
  2. 如何将建议限制为最多5瓶啤酒

1 个答案:

答案 0 :(得分:0)

一个简单的实施方法是使用cdist计算与其他啤酒的距离,然后使用argsort返回您的建议:

from scipy.spatial.distance import cdist
import numpy as np

vec = TfidfVectorizer()
beerlist = np.array(['heinekin lager', 'corona lager', 'heinekin ale', 'budweiser lager'])
beerlist_tfidf = vec.fit_transform(beerlist).toarray()
beer_tfidf = vec.transform(['heinekin lager']).toarray()
rec_idx = cdist(beer_tfidf, beerlist_tfidf, 'cosine').argsort()
print(beerlist[rec_idx[0][1:]])

#['heinekin ale' 'corona lager' 'budweiser lager']