Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Lightfm:处理用户和项目冷启动_Python_Recommendation Engine_Matrix Factorization_Cold Start - Fatal编程技术网

Python Lightfm:处理用户和项目冷启动

Python Lightfm:处理用户和项目冷启动,python,recommendation-engine,matrix-factorization,cold-start,Python,Recommendation Engine,Matrix Factorization,Cold Start,我记得lightfm的一个优点是该型号不会出现冷启动问题,无论是用户还是项目冷启动: 然而,我仍然不明白如何使用lightfm解决冷启动问题。我在用户项交互数据上训练了我的模型。据我所知,我只能对数据集中存在的概要文件ID进行预测 def predict(self, user_ids, item_ids, item_features=None, user_features=None, num_threads=1): """ Compute the reco

我记得lightfm的一个优点是该型号不会出现冷启动问题,无论是用户还是项目冷启动:

然而,我仍然不明白如何使用lightfm解决冷启动问题。我在
用户项交互数据
上训练了我的模型。据我所知,我只能对数据集中存在的概要文件ID进行预测

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

任何有助于我理解的建议或建议都将不胜感激。谢谢

LightFM和其他任何推荐算法一样,如果没有提供关于全新用户的额外信息,它无法预测这些用户。尝试为新用户提供建议时的诀窍是根据算法在培训期间看到的特性来描述他们

这可能最好用一个例子来解释。假设您的培训集中有ID介于0和10之间的用户,并且您希望预测新用户ID 11。如果您所拥有的只是新用户的ID,那么该算法将无法进行预测:毕竟,它对用户11的偏好一无所知。但是,假设您有一些功能来描述用户:可能在注册过程中,每个用户都会选择一些他们感兴趣的内容(例如恐怖电影或浪漫喜剧)。如果这些特征在训练期间出现,该算法可以了解与这些特征相关的平均偏好,并且能够为任何可以使用相同特征描述的新用户生成建议。在本例中,如果您可以提供用户11在注册过程中选择的首选项,您将能够对用户11进行预测

在LightFM实现中,所有这些特征都将被编码到特征矩阵中,可能以一种热编码的形式。当为用户11提出建议时,您将为该用户构建一个新的特征矩阵:只要该特征矩阵仅包含培训期间出现的特征,您就能够进行预测

请注意,通常情况下,具有仅对应于单个用户的功能非常有用,例如“is user 0”功能、“is user 1”功能等等。对于新用户来说,这样的功能是无用的,因为培训中没有模型可以用来了解该功能的信息。

这对我来说很有用:

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\u features
是一个稀疏数组,它是从训练模型时使用的特性集仔细组装而成的

例如,如果我获得了一个新用户,并且该用户的功能类似于
用户功能\u列表=[“id-2837”,“开普敦”,“伍德斯托克”,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)
user\u feature\u map
是在拟合后,通过调用原始输入数据集上的LightFM的
mapping()
方法生成的:

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()

hi@Maciej Kula感谢您的解释,现在我了解到,用户或项目的冷启动问题可以通过将功能合并到模型中来解决。然而,我仍然不清楚我应该如何做到这一点。因此,给定一个新用户,我将为该用户构建一个新的特征矩阵,然后1)我是否应该根据该特征矩阵构建一个函数来查找相似的用户?或者2)我可以直接使用我训练过的模型来生成对新用户的推荐吗?再次感谢您为新用户构建了一个特征矩阵(实际上是一行),并直接在
模型中使用它。predict
方法。该模型的部分职责是为您自动建模相似性。啊,明白了,但是对于
ids
参数,我应该放什么?在这种情况下,您可以将所有与ID相关的列设置为零。您好@bohr,您愿意分享您的代码,说明如何解决添加新用户的问题吗?我试图自己解决那个问题,但我有点挣扎。。非常感谢。也许你可以尝试一个随机初始化,基于更多新的真实数据,模型应该能正确预测。如果初始功能全部被替换,则开始涉及用户/id的(在线)培训。在第二个代码段中,您已经编写了
store\u model.user\u feature\u map
,什么是
store\u model
对象。另外,当你得到
user\u id\u映射、user\u feature\u映射、item\u id\u映射、item\u feature\u map=dataset.mapping()
suer\u feature\u映射只是给了我与user\u id\u映射相同的结果,即user vs user encoding映射。最后,这是否意味着,对于冷启动问题,您必须拥有从数据集创建的用户功能。构建用户功能数据集的方法。@petrus jvrensburg您使用索引0```模型有什么特殊原因吗?预测(0,np.array(target\u item\u index),user\u features=user\u features)`,你有关于冷启动的例子吗?