我记得lightfm的优点之一是模型没有冷启动问题,用户和项目都冷启动:lightfm original paper
然而,我仍然不明白如何使用lightfm来解决冷启动问题。我在user-item interaction data上训练我的模型。据我所知,我只能对我的数据集上存在的profile_ids进行预测。
def predict(self, user_ids, item_ids, item_features=None,
user_features=None, num_threads=1):
"""
Compute the recommendation score for user-item pairs.
Arguments
---------
user_ids: integer or np.int32 array of shape [n_pairs,]
single user id or an array containing the user ids for the
user-item pairs for which a prediction is to be computed
item_ids: np.int32 array of shape [n_pairs,]
an array containing the item ids for the user-item pairs for which
a prediction is to be computed.
user_features: np.float32 csr_matrix of shape [n_users, n_user_features], optional
Each row contains that user's weights over features.
item_features: np.float32 csr_matrix of shape [n_items, n_item_features], optional
Each row contains that item's weights over features.
num_threads: int, optional
Number of parallel computation threads to use. Should
not be higher than the number of physical cores.
Returns
-------
np.float32 array of shape [n_pairs,]
Numpy array containing the recommendation scores for pairs defined
by the inputs.
"""
self._check_initialized()
if not isinstance(user_ids, np.ndarray):
user_ids = np.repeat(np.int32(user_ids), len(item_ids))
assert len(user_ids) == len(item_ids)
if user_ids.dtype != np.int32:
user_ids = user_ids.astype(np.int32)
if item_ids.dtype != np.int32:
item_ids = item_ids.astype(np.int32)
n_users = user_ids.max() + 1
n_items = item_ids.max() + 1
(user_features,
item_features) = self._construct_feature_matrices(n_users,
n_items,
user_features,
item_features)
lightfm_data = self._get_lightfm_data()
predictions = np.empty(len(user_ids), dtype=np.float64)
predict_lightfm(CSRMatrix(item_features),
CSRMatrix(user_features),
user_ids,
item_ids,
predictions,
lightfm_data,
num_threads)
return predictions任何有助于我理解的建议或建议都将不胜感激。谢谢
发布于 2017-10-28 13:49:24
就像任何其他推荐算法一样,如果没有给出关于全新用户的额外信息,LightFM就无法对这些用户做出预测。当尝试为新用户提供推荐时,技巧是根据算法在训练期间看到的特征来描述它们。
这可能是最好的解释,用一个例子。假设您的训练集中有ID在0到10之间的用户,并且您希望对ID为11的新用户进行预测。如果您所拥有的全部是新用户的ID,算法将无法进行预测:毕竟,它对用户11的首选项一无所知。然而,假设你有一些特征来描述用户:也许在注册过程中,每个用户都选择了他们的一些兴趣(例如,恐怖电影或浪漫喜剧)。如果在训练过程中存在这些特征,则该算法可以了解平均而言,哪些偏好与这些特征相关联,并且将能够为可以使用相同特征描述的任何新用户产生推荐。在此示例中,如果您可以提供用户11在注册过程中选择的首选项,您将能够对他们进行预测。
在LightFM实现中,所有这些特征都将被编码到特征矩阵中,可能是以one-hot编码的形式。在为用户11提供推荐时,您将为该用户构建一个新的特征矩阵:只要该特征矩阵只包含训练过程中出现的特征,您就能够做出预测。
请注意,通常具有仅对应于单个用户的功能是有用的-因此,“is user 0”功能、“Is user 1”功能等等。在新用户的情况下,这样的功能是无用的,因为在训练中没有模型可以用来学习该功能的信息。
发布于 2018-08-13 17:41:51
这对我很有效:
if user_index is not None:
predictions = model.predict([user_index, ], np.array(target_item_indices))
else:
predictions = model.predict(0, np.array(target_item_indices), user_features=user_features)在这里,user_features是一个稀疏数组,它是从训练模型时使用的特征集仔细组装而成的。
例如,如果我有一个新用户,并且该用户的特征类似于user_feature_list = ["id-2837", "Cape Town", "Woodstock", 7700],那么我将构建特征数组,如下所示:
from scipy import sparse
user_feature_map = store_model.user_feature_map # the feature map was persisted during the previous round of offline training
num_features = len(user_feature_list)
normalised_val = 1.0 / num_features
target_indices = []
for feature in user_feature_list:
try:
target_indices.append(user_feature_map[feature])
except KeyError:
print("new user feature encountered '{}'".format(feature))
pass
print("target indices: {}".format(target_indices))
user_features = np.zeros(len(user_feature_map.keys()))
for i in target_indices:
user_features[i] = normalised_val
user_features = sparse.csr_matrix(user_features)先前通过对原始输入数据集调用LightFM的mapping()方法,在拟合后生成了user_feature_map:
dataset.fit(
unique_user_ids,
unique_item_ids,
item_features=item_feature_list,
user_features=user_feature_list
)
user_id_map, user_feature_map, item_id_map, item_feature_map = dataset.mapping()https://stackoverflow.com/questions/46924119
复制相似问题