Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rewrite the following code to get more accurate predictions in the second part : #include #include #include #include #include // A struct to store the

Rewrite the following code to get more accurate predictions in the second part :

#include  #include  #include  #include  #include  // A struct to store the movie ratings for a single user struct UserRatings { int userId; std::map ratings; }; int main() { // Open the input file std::ifstream file("train.csv"); if (!file.is_open()) { std::cerr << "Error opening file" << std::endl; return 1; } // Map to store the number of ratings for each user std::map user_ratings; // Map to store the number of ratings for each movie std::map movie_ratings; std::string line; // Read the file line by line while (std::getline(file, line)) { std::stringstream linestream(line); std::string user, movie, rating; // Extract the user, movie, and rating from the line std::getline(linestream, user, ','); std::getline(linestream, movie, ','); std::getline(linestream, rating); // Increment the number of ratings for the user and movie ++user_ratings[user]; ++movie_ratings[movie]; } file.close(); // Sort the user_ratings map by value in decreasing order std::vector> user_ratings_vec(user_ratings.begin(), user_ratings.end()); std::sort(user_ratings_vec.begin(), user_ratings_vec.end(), [](const std::pair &a, const std::pair &b) { return a.second > b.second; }); // Sort the movie_ratings map by value in decreasing order std::vector> movie_ratings_vec( movie_ratings.begin(), movie_ratings.end()); std::sort(movie_ratings_vec.begin(), movie_ratings_vec.end(), [](const std::pair &a, const std::pair &b) { return a.second > b.second; }); std::cout << "Table 1: Top users who have the most ratings:" << std::endl; // Print the top 10 users auto user_it = user_ratings_vec.begin(); auto user_end = std::next(user_it, std::min((int) user_ratings_vec.size(), 10)); for (; user_it != user_end; ++user_it) { std::cout << user_it->second << " " << user_it->first << std::endl; } std::cout << "Table 2: Top movies which have the most ratings:" << std::endl; // Print the top 10 movies auto movie_it = movie_ratings_vec.begin(); auto movie_end = std::next(movie_it, std::min((int) movie_ratings_vec.size(), 10)); for (; movie_it != movie_end; ++movie_it) { std::cout << movie_it->second << " " << movie_it->first << std::endl; } // Second Part // Open the first CSV file for reading std::ifstream ratingsFile("train.csv"); // Read the first line (column headers) and ignore it std::string line1; std::getline(ratingsFile, line); // Create a vector to store the movie ratings for all users std::vector allRatings; // Read each line of the CSV file while (std::getline(ratingsFile, line)) { std::istringstream lineStream(line); // Extract the userId, movieId, and rating from the line int userId, movieId; float rating; std::string cell; std::getline(lineStream, cell, ','); userId = std::stoi(cell); std::getline(lineStream, cell, ','); movieId = std::stoi(cell); std::getline(lineStream, cell, ','); rating = std::stof(cell); // Find the UserRatings struct for this user, or create a new one if it doesn't exist UserRatings *ratings = nullptr; for (auto &ur: allRatings) { if (ur.userId == userId) { ratings = &ur; break; } } if (ratings == nullptr) { allRatings.emplace_back(UserRatings{userId}); ratings = &allRatings.back(); } // Add the rating for this movie to the user's ratings ratings->ratings[movieId] = rating; } // Close the first CSV file ratingsFile.close(); // Open the second CSV file for reading std::ifstream predictionsFile("test.csv"); // Read the first line (column headers) and ignore it std::getline(predictionsFile, line); // Open the output file for writing std::ofstream outputFile("Predictions.csv"); // Write the column headers to the output file outputFile << "id,predicted" << std::endl; // Read each line of the second CSV file while (std::getline(predictionsFile, line)) { std::istringstream lineStream(line); // Extract the id, userId, and movieId from the line int id, userId, movieId; std::string cell; std::getline(lineStream, cell, ','); id = std::stoi(cell); std::getline(lineStream, cell, ','); userId = std::stoi(cell); std::getline(lineStream, cell, ','); movieId = std::stoi(cell); // Predict the rating for the current user and movie float prediction = 0.0; for (auto &ur: allRatings) { // If the current user has rated the movie, use their rating as the prediction if (ur.userId == userId && ur.ratings.count(movieId) > 0) { prediction = ur.ratings[movieId]; break; } } // Write the prediction to the output file outputFile << id << "," << prediction << std::endl; } // Close the second CSV file and the output file predictionsFile.close(); outputFile.close(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions