Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Importing necessary modules from the surprise library
- from surprise import KNNBasic
- from surprise import Dataset
- from surprise import Reader
- from surprise.model_selection import cross_validate
- # Pandas is used for data manipulation
- import pandas as pd
- # Load the ratings dataset
- df = pd.read_csv("ratings.csv")
- # Create a Reader object with the rating scale from 1 to 5
- # The Reader class is used to parse a file containing ratings
- reader = Reader(rating_scale=(1, 5))
- # Load the data from the dataframe into the surprise Dataset format
- # The load_from_df method loads a dataset from a pandas dataframe
- # The dataframe must have three columns: user ids, item ids and ratings, in that order
- data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], reader)
- # Define the algorithm to be used, in this case, KNNBasic
- algo = KNNBasic()
- # Use cross-validation to assess the performance of the algorithm
- # The cross_validate function performs a cross-validation procedure on a dataset with a given algorithm
- # RMSE (Root Mean Squared Error) and MAE (Mean Absolute Error) are computed as accuracy metrics
- # cv=5 implies a 5-fold cross-validation
- # verbose=True will print messages (like progress and computed measures) during execution
- cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement