【问题标题】:Alternating Least Square parameters tuning交替最小二乘参数调整
【发布时间】:2020-04-19 13:43:09
【问题描述】:

上下文:我正在使用 python 中的隐式库构建一个使用隐式反馈(订单)的推荐系统。

问题:当尝试调整参数以了解要使用的最佳参数时,输出没有循环所有变量,也没有计算 auc。如何确保它遍历所有组合,并在组合导致最高 AUC 分数时添加到字典中? 另外,如果有一个库可以用来调整它,请随时告诉我,因为我不知道如何将 gridsearchCV 用于这个用例(ALS 模型)。

在代码中: training_set2 - 原始 training_set 的更改版本,其中一定比例的用户-项目对最初具有交互设置回零。

validation_set - 原始 training_set 矩阵的副本,未更改,因此可用于查看排名顺序与实际交互的比较情况。

预期输出:是包含所有组合的字典,按降序排列,最后一个是具有最高 AUC 分数的组合。这个组合将是我将用于我的测试集的组合。


    def auc_score(predictions, test):
        fpr, tpr, thresholds = metrics.roc_curve(test, predictions)
        return metrics.auc(fpr, tpr)

    def calc_mean_auc(training_set, altered_users, predictions, test_set):
        '''
        This function will calculate the mean AUC by user for any user that had their user-item matrix altered. 
        '''
        store_auc = [] # An empty list to store the AUC for each user that had an item removed from the training set
        item_vecs = predictions[1]
        for user in altered_users: # Iterate through each user that had an item altered
            training_row = training_set[user,:].toarray().reshape(-1) # Get the training set row
            zero_inds = np.where(training_row == 0) # Find where the interaction had not yet occurred
            # Get the predicted values based on our user/item vectors
            user_vec = predictions[0][user,:]
            pred = user_vec.dot(item_vecs).toarray()[0,zero_inds].reshape(-1)
            # Get only the items that were originally zero
            # Select all ratings from the MF prediction for this user that originally had no iteraction
            actual = test_set[user,:].toarray()[0,zero_inds].reshape(-1) 
            # Select the binarized yes/no interaction pairs from the original full data
            # that align with the same pairs in training 
            store_auc.append(auc_score(pred, actual)) # Calculate AUC for the given user and store
        # End users iteration

        return float('%.3f'%np.mean(store_auc)) 
    ...
    latent_factors = [5, 10, 20, 40, 80]
    regularizations = [0.01, 0.1, 1., 10., 100.]
    regularizations.sort()
    iter_array = [1, 2, 5, 10, 25, 50, 100]

    best_params = {}
    best_params['n_factors'] = latent_factors[0]
    best_params['reg'] = regularizations[0]
    best_params['n_iter'] = 0
    best_params['auc_result'] = np.inf
    best_params['model'] = None

    for fact in latent_factors:
        print('Factors: {}'.format(fact))
        for reg in regularizations:
            print ('Regularization: {}'.format(reg))
            for ite in iter_array:
                print ('Iteration: {}'.format(ite))
                model = implicit.als.AlternatingLeastSquares(
                    factors=fact,
                    regularization=reg, 
                    iterations=ite)



    model.fit((training2_set.T * 15).astype('double'))
                customers_vecs = model.user_factors
                restaurant_vecs = model.item_factors
                auc_result = calc_mean_auc(training2_set, cust_altered2, 
                  [sparse.csr_matrix(customers_vecs), sparse.csr_matrix(restaurant_vecs.T)], validation_set)
                if auc_result > best_params['auc_result']:
                    best_params['n_factors'] = fact
                    best_params['reg'] = reg
                    best_params['n_iter'] = ite
                    best_params['auc_result'] = auc_result
                    best_params['model'] = 'AlternatingLeastSquare'
                    print ('New optimal hyperparameters')
                    print (pd.Series(best_params))

我无法发布图片,但这是我得到的输出:

</b>
Factors: 5</b>
Regularization: 0.01</b>
Iteration: 1</b>

n_factors     5.00</b>
reg           0.01</b>
n_iter        0.00</b>
auc_result     inf</b>
model          NaN</b>
dtype: float64</b>
Iteration: 2</b>

n_factors     5.00</b>
reg           0.01</b>
n_iter        0.00</b>
auc_result     inf</b>
model          NaN</b>
dtype: float64</b>
Iteration: 5</b>

【问题讨论】:

    标签: python recommendation-engine least-squares hyperparameters collaborative-filtering


    【解决方案1】:

    您只保留 最佳 参数 - if auc_error &lt; best_params['auc_error']:

    使用collections.defaultdict 来累积列表中的所有参数

    ...
    from collections import defaultdict
    best_params = defaultdict(list)
    ##best_params = {}
    ##best_params['n_factors'] = latent_factors[0]
    ##best_params['reg'] = regularizations[0]
    ##best_params['n_iter'] = 0
    ##best_params['auc_error'] = np.inf
    ##best_params['model'] = None
    ...
    for fact in latent_factors:
        ...
            ...
                ##if auc_error < best_params['auc_error']:
                best_params['n_factors'].append(fact)
                best_params['reg'].append(reg)
                best_params['n_iter'].append(ite)
                best_params['auc_error'].append(auc_error)
                best_params['model'].append('AlternatingLeastSquare')
                ##print ('New optimal hyperparameters')
    

    看来您正在使用 Pandas,所以制作一个 DataFrame 并对其进行排序。

    df = pd.DataFrame(best_params)
    df = df.sort_values('auc_error')
    

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 2017-08-27
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      相关资源
      最近更新 更多