Advertisement
897bhgy

Untitled

Jul 24th, 2023 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | Source Code | 0 0
  1. # Importing necessary modules from the surprise library
  2. from surprise import KNNBasic
  3. from surprise import Dataset
  4. from surprise import Reader
  5. from surprise.model_selection import cross_validate
  6.  
  7. # Pandas is used for data manipulation
  8. import pandas as pd
  9.  
  10. # Load the ratings dataset
  11. df = pd.read_csv("ratings.csv")
  12.  
  13. # Create a Reader object with the rating scale from 1 to 5
  14. # The Reader class is used to parse a file containing ratings
  15. reader = Reader(rating_scale=(1, 5))
  16.  
  17. # Load the data from the dataframe into the surprise Dataset format
  18. # The load_from_df method loads a dataset from a pandas dataframe
  19. # The dataframe must have three columns: user ids, item ids and ratings, in that order
  20. data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], reader)
  21.  
  22. # Define the algorithm to be used, in this case, KNNBasic
  23. algo = KNNBasic()
  24.  
  25. # Use cross-validation to assess the performance of the algorithm
  26. # The cross_validate function performs a cross-validation procedure on a dataset with a given algorithm
  27. # RMSE (Root Mean Squared Error) and MAE (Mean Absolute Error) are computed as accuracy metrics
  28. # cv=5 implies a 5-fold cross-validation
  29. # verbose=True will print messages (like progress and computed measures) during execution
  30. cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement