1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <iostream> #include <vector> #include <cmath> #include <iomanip> const int NUM_USERS = 5; const int NUM_ITEMS = 4; // Function to compute the similarity between two users using cosine similarity double computeSimilarity(const std::vector<double>& user1, const std::vector<double>& user2) { double dotProduct = 0.0; double norm1 = 0.0; double norm2 = 0.0; for (int i = 0; i < NUM_ITEMS; ++i) { dotProduct += user1[i] * user2[i]; norm1 += user1[i] * user1[i]; norm2 += user2[i] * user2[i]; } norm1 = std::sqrt(norm1); norm2 = std::sqrt(norm2); if (norm1 == 0.0 || norm2 == 0.0) { return 0.0; } return dotProduct / (norm1 * norm2); } // Function to recommend items for a user based on similarities with other users void recommendItems(const std::vector<std::vector<double>>& ratings, int targetUser) { std::vector<double> userRatings = ratings[targetUser]; std::vector<double> itemScores(NUM_ITEMS, 0.0); std::vector<double> totalSimilarities(NUM_ITEMS, 0.0); for (int i = 0; i < NUM_USERS; ++i) { if (i == targetUser) continue; double similarity = computeSimilarity(ratings[targetUser], ratings[i]); for (int j = 0; j < NUM_ITEMS; ++j) { if (ratings[i][j] > 0) { itemScores[j] += similarity * ratings[i][j]; totalSimilarities[j] += similarity; } } } std::cout << "Recommendations for User " << targetUser + 1 << ":\n"; for (int i = 0; i < NUM_ITEMS; ++i) { if (userRatings[i] == 0) { // Only recommend items not yet rated by the user double predictedRating = (totalSimilarities[i] > 0) ? itemScores[i] / totalSimilarities[i] : 0.0; std::cout << "Item " << i + 1 << ": " << std::fixed << std::setprecision(2) << predictedRating << "\n"; } } } int main() { // Example ratings matrix (users x items) std::vector<std::vector<double>> ratings = { {4, 0, 0, 2}, {0, 5, 0, 0}, {3, 0, 0, 0}, {0, 0, 4, 0}, {5, 0, 0, 0} }; int targetUser; std::cout << "Enter the user number (1-" << NUM_USERS << ") for recommendations: "; std::cin >> targetUser; if (targetUser < 1 || targetUser > NUM_USERS) { std::cerr << "Invalid user number.\n"; return 1; } recommendItems(ratings, targetUser - 1); return 0; } |
Explanation
- Constants:
- NUM_USERS: Number of users in the system.
- NUM_ITEMS: Number of items available for rating.
- Function
computeSimilarity(const std::vector<double>& user1, const std::vector<double>& user2)
:- Purpose: Computes the cosine similarity between two users based on their item ratings.
- Parameters:
user1
,user2
: Vectors of ratings for two different users.
- Implementation:
- Computes the dot product and norms of the vectors.
- Calculates similarity using the formula
dotProduct / (norm1 * norm2)
.
Advertisement
- Function
recommendItems(const std::vector<std::vector<double>>& ratings, int targetUser)
:- Purpose: Recommends items to a specific user based on the ratings of other users.
- Parameters:
ratings
: 2D vector representing the ratings matrix.targetUser
: Index of the user for whom recommendations are to be generated.
- Implementation:
- Uses similarity scores to compute weighted item scores.
- Predicts ratings for items not yet rated by the target user.
- Outputs the recommended items with their predicted ratings.
- Main Function:
- Setup: Defines an example ratings matrix.
- User Input: Prompts the user to enter the target user number.
- Recommendation: Calls
recommendItems
Advertisement
Usage
- Recommendation System Simulation: Demonstrates a basic collaborative filtering approach to recommend items based on user similarities.
- User Preferences: Shows how ratings from similar users can influence recommendations for an individual user.