day55

/**
     *************************
     * Leave-one-out prediction. The predicted values are stored in predictions.
     *************************
     */
    public void leaveOneOutPredictionUsers() {
        double tempUserAverageRating;
        // Make each line of the code shorter.
        int tempUser, tempItem, tempRating;
        System.out.println("\r\nLeaveOneOutPrediction 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 users.
            tempUserAverageRating = (userAverageRatings[tempUser] * userDegrees[tempUser] - tempRating)
                    / (userDegrees[tempUser] - 1);

            // Step 2. Recompute neighbors, at the same time obtain the ratings
            //Neighbors.
            int tempNeighbors = 0;
            double tempTotal = 0;
            int tempComparedUser;
            for (int j = userStartingIndices[tempUser]; j < userStartingIndices[tempUser + 1]; j++) {
                tempComparedUser = compressedRatingMatrix[j][0];
                if (tempUser == tempComparedUser) {
                    continue;
                    //Ignore itself.
                } //of if

                if (Math.abs(tempUserAverageRating - userAverageRatings[tempComparedUser]) < radius) {
                    tempTotal += compressedRatingMatrix[j][2];
                    tempNeighbors++;
                } //of if
            } //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 leaveOneOutPrediction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值