Question
Up to line 135 the code works as it should, I get RMSE = 0 through the part with cout. But when I add the
Up to line 135 the code works as it should, I get RMSE = 0 through the part with cout. But when I add the part after line 135 to my code, I cannot run the program. Have I added this part in the wrong place? Normally the program needs to write the predicted ratings to a new .csv file, but this does not happen because the code is not working properly. Please help me with this. By the way, is there anything extra I need to do so that the code can read test.csv and train.csv? Other than that, I'm free to change the inside of the function with int main() to lower the RMSE value even more, right?
#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; int user_id; int item_id; float rating; };
// Reads the ratings from a .csv file and returns them as a 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::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 predict_rating_mean_user(int user_id, int item_id, const std::unordered_map
int main() { // Read in the training and test sets std::vector
// Initialize a map to store the ratings given by each user std::unordered_map
// Initialize a map to store the ratings received by each item std::unordered_map
// Predict the ratings for the test set using the mean rating of the user for (auto& predicted_rating : test_set) { predicted_rating.rating = predict_rating_mean_user(predicted_rating.user_id, predicted_rating.item_id, user_ratings); }
// Calculate the RMSE between the predicted ratings and the actual ratings float rmse = calculate_rmse(test_set, training_set); std::cout << "RMSE: " << rmse << std::endl;
return 0; }
// 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
// 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
// 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; }
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