为简单的文本推荐系统定义函数时出现此语法错误。我觉得这很简单,我忽略了。
from sklearn.feature_extraction.text import TfidfVectorizer
# define tfidvectorizer
tfv = TfidfVectorizer(min_df=3, max_features=None,
strip_accents='unicode', analyzer='word',token_pattern=r'\w{1,}',
ngram_range=(1, 3), use_idf=1,smooth_idf=1,sublinear_tf=1,
stop_words = 'english')
# fills with empty string
df.description = df.description.fillna('')
# fit TF-IDF on 'description' text
tfv_matrix = tfv.fit_transform(df.description)
tfv_matrix.shape
出局:(86, 1033)
from sklearn.metrics.pairwise import sigmoid_kernel
# compute sigmoid kernel
sig = sigmoid_kernel(tfv_matrix, tfv_matrix)
# reverse mapping of indices and strain names
indices = pd.Series(df.index, index=df.name_ocp.drop_duplicates()
# define recommendation function
def give_rec(strain, sig=sig):
# get index corresponding to name_ocp
idx = indices[strain]
# get pairwsie similarity scores
sig_scores = list(enumerate(sig[idx]))
# sort strains
sig_scores = sorted(sig_scores, key=lambda x: x[1], reverse=True)
# scores of 10 most similar strains
sig_scores = sig_scores[1:11]
# strain name indices
strain_indices = [i[0] for i in sig_scores]
# top 10 most similar strains
return df.name_ocp.iloc[strain_indices]
出去:
File "<ipython-input-68-bf5092b4ce7d>", line 10
def give_rec(strain, sig=sig):
^
SyntaxError: invalid syntax
'def' 和函数名称都是它们在 jupyter 笔记本中的适当颜色。间距看起来不错。不知道这笔交易是什么。有什么帮助吗?谢谢你。