Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. from surprise import Reader, Dataset, KNNBasic
  2. from surprise.model_selection import cross_validate
  3. import pandas as pd
  4.  
  5. # Read the data from the test.csv file
  6. ratings = pd.read_csv('test.csv', sep=',');
  7.  
  8. # Prepare the data to be used in Surprise
  9. ratings_dict = {'userid': list(ratings.userid),
  10. 'itemid': list(ratings.itemid),
  11. 'rating': list(ratings.rating)}
  12.  
  13. df = pd.DataFrame(ratings_dict)
  14. reader = Reader(rating_scale=(0,5))
  15. data = Dataset.load_from_df(df[['userid', 'itemid', 'rating']], reader=reader)
  16.  
  17. sim_options = {
  18. 'name': 'cosine',
  19. 'user_based': True
  20. }
  21. algo = KNNBasic(sim_options=sim_options)
  22.  
  23. # Retrieve the trainset.
  24. trainset = data.build_full_trainset()
  25. algo.fit(trainset)
  26.  
  27. # Predict
  28. print(algo.predict(1, 5, r_ui=None, verbose=True))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement