Question: Imagine that you are a data scientist working on a recommender system for a music streaming company. You are given the following toy dataset to
Imagine that you are a data scientist working on a recommender system for a music streaming company. You are given the following toy dataset to work on to produce a simple recommender program in Python using objects, classes and methods. You should not use any external libraries when developing your solution to this question.
UserID Username Genre Listens
1 Dave Rock 100
1 Dave Country 1245
1 Dave Pop 1
2 Dolly Country 100312
2 Dolly Trance 100
3 Amadeus Classical 34912390
3 Amadeus Country 5
(a) Write a program that does the following:
Reads in the attached text file. Each field is delimited by a comma. You must ignore the header.
Your program must create an object for each unique user. To do this you need to define a class called User. Each instance of this class must take in the UserID and the Username via its constructor. There should only be one object created for each unique UserID.
Each User object must contain a dictionary representing the listening history of the user. This should be stored as an instance variable. As your program reads in each line of the file, you must update this dictionary with the genre and the number of listens, whereby genre is the key. Assume that each genre will only appear once per user in the input file. You must define a method called addToHistory to add each key-value pairing to the dictionary. You must also define a getter method called getHistory that returns a dictionary of a users listening history.
In the User class you must create a method called printSummary that prints out a summary of the listening history for each user, with the total number of genres listened to, and the total number of listens for that user. An example for Dave would be: Dave has listened to 3 genres a total of 1,346 times. 2 35447 Programming for Data Science 33 35447 Programming for Data Science
Create a main method that takes input from the file, creates a list of User objects containing their listening history. Then for each User, call the method that prints a summary of the Users listening history.
(b) Write a class called Recommender. In its constructor, it should be passed a list of User objects and store these as an instance variable. In this class you should write a method called generateRecommendations. This should iterate through the list of User objects and for each one make a single recommendation based upon the listening history of the next user in the list. A simple recommendation algorithm is as follows:
Load user A and the next user in the list, user B. For all the genres that user B has listened to, if there is one that does not exist in user As listening history, then print this out as a recommendation. e.g sample output could be: Dave is recommended Trance. Only produce a single recommendation, even if there are potentially more. For the last user in the list, they should generate their recommendations based on the first person in the list.
After writing the class, Recommender, create a runnable main method to demonstrate the class working using a list of User objects from part (a). As a comment in the main method, list the output showing what recommendations are generated for each user in the above dataset.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
