我尝试使用lightfm v.1.14来构建混合推荐系统。
我能够使用以下代码将所有数据放在稀疏矩阵中:
db = DBConnector().getDBConnector()
data = pd.read_sql('call get_UserItemRating();', con=db)
rows = data.loc[data['userID'].idxmax()]['userID'] + 1
cols = data.loc[data['itemID'].idxmax()]['itemID'] + 1
mat = sp.lil_matrix((rows, cols), dtype=np.int32)
for index, row in data.iterrows():
if row['rating'] >= 4:
mat[row['userID'], row['itemID']] = row['rating']
train = mat.tocoo()
data = pd.read_sql('SELECT * FROM wine_grapes;', con=db)
db.close()
rows = data.loc[data['fk_Wine'].idxmax()]['fk_Wine'] + 1
cols = data.loc[data['fk_Grapes'].idxmax()]['fk_Grapes'] + 1
mat = sp.lil_matrix((rows, cols), dtype=np.int32)
for index, row in data.iterrows():
mat[row['fk_Wine'],row['fk_Grapes']] = 1
item_features = mat.tocoo()
model = LightFM(loss='warp')
model.fit(train,
item_features=item_features,
epochs=20)
到目前为止它看起来很不错,只有#34;火车"我得到了推荐。 但是当我添加item_features时,我收到以下错误:
Traceback (most recent call last):
File "/Users/dlimacher/PycharmProjects/deepWine/wineRecommender/WineRecommendation_lightfm.py", line 76, in <module>
sample_recommendation(model, train, [1, 2, 3])
File "/Users/dlimacher/PycharmProjects/deepWine/wineRecommender/WineRecommendation_lightfm.py", line 62, in sample_recommendation
scores = model.predict(user_id, np.arange(n_items))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/lightfm/lightfm.py", line 713, in predict
item_features)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/lightfm/lightfm.py", line 315, in _construct_feature_matrices
item_features.shape[1]
ValueError: The user feature matrix specifies more features than there are estimated feature embeddings: 613 vs 1002.
首先,它在我看来错误消息是错误的,因为它是关于item_features而不是user_features。
但更重要的是,我无法找到错误。有人可以帮我吗?