Question
I'm getting some errors when I try to run the code below, can you make the code executable? Also, how can I implement the second
I'm getting some errors when I try to run the code below, can you make the code executable? Also, how can I implement the second code below into the first code? Thanks in advance.
Here is a minimal C++ code that reads in two .csv files, one for training and one for testing, and estimates the ratings for the items in the test set using either user-based collaborative filtering (UBCF) or item-based collaborative filtering (IBCF). The code also calculates the root mean squared error (RMSE) between the predicted ratings and the actual ratings (which are hidden in the test set). The RMSE should be less than 1.0 in order to meet the requirement. This code reads in the training and test sets, stores the ratings given by each user and received by each item in maps, and then uses either user-based collaborative filtering (UBCF) or item-based collaborative filtering (IBCF) to calculate the predicted ratings for the items in the test set. The code then calculates the root mean squared error (RMSE) between the predicted ratings and the actual ratings, and writes the predicted ratings to a .csv file.
#include
#include
#include
#include
#include
// A struct to represent a rating given by a user to an item
struct Rating {
int user_id;
int item_id;
float rating;
};
// A struct to represent a predicted rating for an item
struct PredictedRating {
int id;
float rating;
};
// Reads the ratings from a .csv file and returns them as a vector
std::vector
std::vector
std::ifstream file(filename);
if (file.is_open()) {
std::string line;
// Skip the first line (header)
std::getline(file, line);
while (std::getline(file, line)) {
Rating rating;
std::sscanf(line.c_str(), "%d,%d,%f", &rating.user_id, &rating.item_id, &rating.rating);
ratings.push_back(rating);
}
file.close();
}
return ratings;
}
// Reads the test cases from a .csv file and returns them as a vector
std::vector
std::vector
std::ifstream file(filename);
if (file.is_open()) {
std::string line;
// Skip the first line (header)
std::getline(file, line);
while (std::getline(file, line)) {
PredictedRating test_case;
std::sscanf(line.c_str(), "%d,%d,%d", &test_case.id, &test_case.user_id, &test_case.item_id);
test_cases.push_back(test_case);
}
file.close();
}
return test_cases;
}
// Calculates the root mean squared error between the predicted ratings and the actual ratings
float calculate_rmse(const std::vector
float sum_squared_error = 0.0f;
for (const auto& predicted_rating : predicted_ratings) {
auto actual_rating_iter = std::find_if(actual_ratings.begin(), actual_ratings.end(), [&](const Rating& r) {
return r.user_id == predicted_rating.user_id && r.item_id == predicted_rating.item_id;
});
if (actual_rating_iter != actual_ratings.end()) {
sum_squared_error += std::pow(predicted_rating.rating - actual_rating_iter->rating, 2);
}
}
return std::sqrt(sum_squared_error / predicted_ratings.size());
}
int main() {
// Read in the training and test sets
std::vector
std::vector
// Initialize a map to store the ratings given by each user
std::unordered_map
for (const auto& rating : training_set) {
user_ratings[rating.user_id].push_back(rating);
}
// Initialize a map to store the ratings received by each item
std::unordered_map
for (const auto& rating : training_set) {
item_ratings[rating.item_id].push_back(rating);
}
// Initialize a vector to store the predicted ratings
std::vector
for (const auto& test_case : test_set) {
// Calculate the predicted rating using either UBCF or IBCF
float rating;
if (use_ubcf) {
// Calculate the predicted rating using UBCF
rating = calculate_predicted_rating_using_ubcf(test_case.user_id, test_case.item_id, user_ratings, training_set);
} else {
// Calculate the predicted rating using IBCF
rating = calculate_predicted_rating_using_ibcf(test_case.user_id, test_case.item_id, item_ratings, training_set);
}
// Add the predicted rating to the vector
predicted_ratings.push_back({ test_case.id, rating });
}
// Calculate the RMSE between the predicted ratings and the actual ratings
float rmse = calculate_rmse(predicted_ratings, training_set);
std::cout << "RMSE: " << rmse << std::endl;
// Write the predicted ratings to a .csv file
std::ofstream output_file("predicted_ratings.csv");
output_file << "ID,Rating" << std::endl;
for (const auto& predicted_rating : predicted_ratings) {
output_file << predicted_rating.id << "," << predicted_rating.rating << std::endl;
}
output_file.close();
return 0;
}
Here are implementations of the calculate_predicted_rating_using_ubcf and calculate_predicted_rating_using_ibcf functions using user-based collaborative filtering (UBCF) and item-based collaborative filtering (IBCF), respectively:
// Calculates the predicted rating for an item using user-based collaborative filtering (UBCF)
float calculate_predicted_rating_using_ubcf(int user_id, int item_id, const std::unordered_map
// Get the ratings given by the user to other items
std::vector
// Initialize variables to store the numerator and denominator of the prediction formula
float numerator = 0.0f;
float denominator = 0.0f;
// Calculate the numerator and denominator for each item rated by the user
for (const auto& rating : user_item_ratings) {
// Calculate the similarity between the current item and the item being rated
float similarity = calculate_similarity(item_id, rating.item_id, all_ratings);
numerator += similarity * rating.rating;
denominator += similarity;
}
// Return the predicted rating
return numerator / denominator;
}
// Calculates the predicted rating for an item using item-based collaborative filtering (IBCF)
float calculate_predicted_rating_using_ibcf(int user_id, int item_id, const std::unordered_map
// Get the ratings received by the item from other users
std::vector
// Initialize variables to store the numerator and denominator of the prediction formula
float numerator = 0.0f;
float denominator = 0.0f;
// Calculate the numerator and denominator for each user who rated the item
for (const auto& rating : item_user_ratings) {
// Calculate the similarity between the current user and the user who rated the item
float similarity = calculate_similarity(user_id, rating.user_id, all_ratings);
numerator += similarity * rating.rating;
denominator += similarity;
}
// Return the predicted rating
return numerator / denominator;
}
These functions both take in the user_id and item_id of the user and item for which the rating is being predicted, as well as the user_ratings map and the all_ratings vector. They use these inputs to calculate the predicted rating using UBCF or IBCF, respectively.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started