Day 55 基于 M-distance 的推荐

代码参考师姐的代码

新增代码:

    /**
     ********************
     * Leave-one-out prediction. The predicted values are stores in predictions.
     *
     * @see predictions
     *********************
     */
    public void leaveOneOutPrediction2() {
        double tempUserAverageRating;
        // Make each line of the code shorter.
        int tempUser, tempItem, tempRating;
        System.out.println("\r\nLeaveOneOutPrediction2 for radius " + radius);

        numNonNeighbors = 0;
        for (int i = 0; i < numRatings; i++) {
            tempUser = compressedRatingMatrix[i][0];
            tempItem = compressedRatingMatrix[i][1];
            tempRating = compressedRatingMatrix[i][2];

            // Step 1. Recompute average rating of the current item.
            tempUserAverageRating = (userAverageRatings[tempUser] * userDegrees[tempUser] - tempRating)
                    / (userDegrees[tempUser] - 1);
            // Step 2. Recompute neighbors,at the same time obtain the ratings of neighbors.
            int tempNeighbors = 0;
            double tempTotal = 0;
            int tempComparedItem;

            for (int j = 0; j < numUsers; j++) {
                if (j == tempUser)
                    continue;
                for (int k = userStartingIndices[j]; k < userStartingIndices[j + 1]; k++) {
                    tempComparedItem = compressedRatingMatrix[k][1];
                    if (tempComparedItem == tempItem
                            && Math.abs(tempUserAverageRating - userAverageRatings[j]) < radius) {
                        tempTotal += compressedRatingMatrix[k][2];
                        tempNeighbors++;
                    } // Of if
                } // Of for k
            } // Of for j

            // Step 3. Predict as the average value of neighbors.
            if (tempNeighbors > 0) {
                predictions[i] = tempTotal / tempNeighbors;
            } else {
                predictions[i] = DEFAULT_RATING;
                numNonNeighbors++;
            } // Of if
        } // Of for i
    }// Of leaveOneOutPrediction2

    /**
     ********************
     * Transform the compressed matrix.
     *********************
     */
    public void transformMatrix() {
        int[][] resultMatrix = new int[numRatings][3];
        int[] tempItemCounts = new int[numItems];
        int[] tempPointIndex = new int[numItems];
        int[] tempItemStartingIndices = new int[numItems + 1];

        // Count the number of every item.
        for (int i = 0; i < numRatings; i++) {
            tempItemCounts[compressedRatingMatrix[i][1]]++;
        } // Of for i

        // Get every item's starting index and initial the point array.
        tempPointIndex[0] = 0;
        tempItemStartingIndices[0] = 0;
        tempItemStartingIndices[numItems] = numRatings;
        for (int i = 1; i < numItems; i++) {
            tempPointIndex[i] = tempItemCounts[i - 1] + tempPointIndex[i - 1];
            tempItemStartingIndices[i] = tempPointIndex[i];
        } // Of for i

        // Transform the matrix.
        int tempIndex;
        for (int i = 0; i < numRatings; i++) {
            tempIndex = tempPointIndex[compressedRatingMatrix[i][1]];
            resultMatrix[tempIndex][0] = compressedRatingMatrix[i][1];
            resultMatrix[tempIndex][1] = compressedRatingMatrix[i][0];
            resultMatrix[tempIndex][2] = compressedRatingMatrix[i][2];
            tempPointIndex[compressedRatingMatrix[i][1]]++;
        } // Of for i

        // Swap the value between users and items.
        int tempArray[], tempValue;
        double tempAvarageArray[];
        compressedRatingMatrix = resultMatrix;
        userStartingIndices = tempItemStartingIndices;

        tempArray = userDegrees;
        userDegrees = itemDegrees;
        itemDegrees = tempArray;

        tempValue = numUsers;
        numUsers = numItems;
        numItems = tempValue;

        tempAvarageArray = userAverageRatings;
        userAverageRatings = itemAverageRatings;
        itemAverageRatings = tempAvarageArray;
        // leaveOneOutPrediction();
    }// Of transformMatrix

主函数:

/**
     *************************
     * The entrance of the program.
     *
     * @param args
     *            Not used now.
     *************************
     */
    public static void main(String[] args) {
        try {
            MBR tempRecommender = new MBR("C:\\Users\\86183\\IdeaProjects\\deepLearning\\src\\main\\java\\resources\\movielens-943u1682m.txt", 943, 1682, 100000);

            for (double tempRadius = 0.2; tempRadius < 0.6; tempRadius += 0.1) {
                tempRecommender.setRadius(tempRadius);

                tempRecommender.leaveOneOutPrediction();
                double tempMAE = tempRecommender.computeMAE();
                double tempRSME = tempRecommender.computeRSME();

                System.out.println("Radius = " + tempRadius + ", MAE = " + tempMAE + ", RSME = " + tempRSME
                        + ", numNonNeighbors = " + tempRecommender.numNonNeighbors);
            } // Of for tempRadius

            System.out.println("\r\n-------user-based recommendation by transform -------");
            tempRecommender.transformMatrix();

            for (double tempRadius = 0.2; tempRadius < 0.6; tempRadius += 0.1) {
                tempRecommender.setRadius(tempRadius);

                tempRecommender.leaveOneOutPrediction();
                double tempMAE = tempRecommender.computeMAE();
                double tempRSME = tempRecommender.computeRSME();

                System.out.println("Radius = " + tempRadius + ", MAE = " + tempMAE + ", RSME=" + tempRSME
                        + ", numNonNeighbors = " + tempRecommender.numNonNeighbors);
            } // Of for tempRadius
        } catch (Exception ee) {
            System.out.println(ee);
        } // Of try
    }// Of main

结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值