<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 The following code snippet will parse the books data provided at the training.
Step1:  Visually explore the above data using GraphLab Canvas.
Step2:  Recommendation systems
In this section we will make a model that can be used to recommend new tags to users.
Creating a Model
Use gl.recommender.create() to create a model that can be used to recommend tags to each user.
Step3:  Print a summary of the model by simply entering the name of the object.
Step4:  Get all unique users from the first 10000 observations and save them as a variable called users.
Step5:  Get 20 recommendations for each user in your list of users. Save these as a new SFrame called recs.
Step6:  Inspecting your model
Get an SFrame of the 20 most similar items for each observed item.
Step7:  This dataset has multiple rows corresponding to the same book, e.g., in situations where reprintings were done by different publishers in different year.
For each unique value of 'book' in the items SFrame, select one of the of the available values for author, publisher, and year. Hint
Step8:  Computing the number of times each book was rated, and add a column containing these counts to the items SFrame using SFrame.join.
Step9:  Print the first few books, sorted by the number of times they have been rated. Do these values make sense?
Step10:  Now print the most similar items per item, sorted by the most common books. Hint
Step11:  Experimenting with other models
Create a dataset called implicit that contains only ratings data where rating was 4 or greater.
Step12:  Create a train/test split of the implicit data created above. Hint
Step13:  Print the first 5 rows of the training set.
Step14:  Create a ranking_factorization_recommender model using just the training set and 20 factors.
Step15:  Evaluate how well this model recommends items that were seen in the test set you created above. Hint
Step16:  Create an SFrame containing only one observation, where 'Billy Bob' has rated 'Animal Farm' with score 5.0.
Step17:  Use this data when querying for recommendations.
<ASSISTANT_TASK:>
Python Code:
import os
if os.path.exists('books/ratings'):
    ratings = gl.SFrame('books/ratings')
    items = gl.SFrame('books/items')
    users = gl.SFrame('books/users')
else:
    ratings = gl.SFrame.read_csv('books/book-ratings.csv')
    ratings.save('books/ratings')
    items = gl.SFrame.read_csv('books/book-data.csv')
    items.save('books/items')
    users = gl.SFrame.read_csv('books/user-data.csv')
    users.save('books/users')
Explanation: The following code snippet will parse the books data provided at the training.
End of explanation
ratings.show()
Explanation: Visually explore the above data using GraphLab Canvas.
End of explanation
m = gl.recommender.create(ratings, user_id='name', item_id='book')
Explanation: Recommendation systems
In this section we will make a model that can be used to recommend new tags to users.
Creating a Model
Use gl.recommender.create() to create a model that can be used to recommend tags to each user.
End of explanation
m
Explanation: Print a summary of the model by simply entering the name of the object.
End of explanation
users = ratings.head(10000)['name'].unique()
Explanation: Get all unique users from the first 10000 observations and save them as a variable called users.
End of explanation
recs = m.recommend(users, k=20)
Explanation: Get 20 recommendations for each user in your list of users. Save these as a new SFrame called recs.
End of explanation
sims = m.get_similar_items()
Explanation: Inspecting your model
Get an SFrame of the 20 most similar items for each observed item.
End of explanation
items = items.groupby('book', {k: gl.aggregate.SELECT_ONE(k) for k in ['author', 'publisher', 'year']})
Explanation: This dataset has multiple rows corresponding to the same book, e.g., in situations where reprintings were done by different publishers in different year.
For each unique value of 'book' in the items SFrame, select one of the of the available values for author, publisher, and year. Hint: Try using SFrame.groupby and gl.aggregate.SELECT_ONE.
End of explanation
num_ratings_per_book = ratings.groupby('book', gl.aggregate.COUNT)
items = items.join(num_ratings_per_book, on='book')
Explanation: Computing the number of times each book was rated, and add a column containing these counts to the items SFrame using SFrame.join.
End of explanation
items.sort('Count', ascending=False)
Explanation: Print the first few books, sorted by the number of times they have been rated. Do these values make sense?
End of explanation
sims = sims.join(items[['book', 'Count']], on='book')
sims = sims.sort(['Count', 'book', 'rank'], ascending=False)
sims.print_rows(1000, max_row_width=150)
Explanation: Now print the most similar items per item, sorted by the most common books. Hint: Join the two SFrames you created above.
End of explanation
implicit = ratings[ratings['rating'] >= 4]
Explanation: Experimenting with other models
Create a dataset called implicit that contains only ratings data where rating was 4 or greater.
End of explanation
train, test = gl.recommender.util.random_split_by_user(implicit, user_id='name', item_id='book')
Explanation: Create a train/test split of the implicit data created above. Hint: Use random_split_by_user.
End of explanation
train.head(5)
Explanation: Print the first 5 rows of the training set.
End of explanation
m = gl.ranking_factorization_recommender.create(train, 'name', 'book', target='rating', num_factors=20)
Explanation: Create a ranking_factorization_recommender model using just the training set and 20 factors.
End of explanation
m.evaluate_precision_recall(test, cutoffs=[50])['precision_recall_overall']
Explanation: Evaluate how well this model recommends items that were seen in the test set you created above. Hint: Check out m.evaluate_precision_recall().
End of explanation
new_observation_data = gl.SFrame({'name': ['Me'], 'book': ['Animal Farm'], 'rating': [5.0]})
Explanation: Create an SFrame containing only one observation, where 'Billy Bob' has rated 'Animal Farm' with score 5.0.
End of explanation
m.recommend(users=['Me'], new_observation_data=new_observation_data)
Explanation: Use this data when querying for recommendations.
End of explanation
<END_TASK>