Lab 8
User-CF Recommender System – Part I
50 points
In this lab, we will focus on understanding the design of a user-based collaborative filtering
recommender system. Then we will implement half that design in this lab, and the other half in
the next lab.
System Description:
A user-based collaborative filtering (User-CF for short) recommender works by making use of the
similarities between users in a system. To recommend items for a specific user u, first User-CF finds the
n-most similar users to u. These are called the neighbors of u. Then, the items that ratings that were given
by the neighbors of u for the different items are used to generate recommendation scores for each item.
Finally, the m items with the highest recommendation scores are recommended to u. The number of
neighbors n, and the number of items m can vary based on the application in which the recommender is
used.
The overall flow of the user-based collaborative filtering recommendation process can be summarized in
these steps:
1. Read movie information from file
2. Read user information from file
3. Generate movie recommendations for each user as follows. For each user u:
a. Find u’s n most similar neighbors
i. Compute the Jaccard similarity between u and all other users
ii. Select the n users with the highest similarity values. Call these n users the
neighbors of u.
b. Compute movie recommendation scores for u as follows.
i. For each movie, compute its recommendation score using the rating information
for the neighbors of u.
ii. Select the m movies with the highest recommendation scores.
System Design:
1. The system will include a class named User to represent a user. That class should include the user’s
ID, name, number of watched items, and a list containing the IDs of the watched items. The class
should include a method for computing the number of similar items between two users.
2. To allow for representing diverse items, the system will include an abstract class named Item to
represent a generic item. This class will include the item’s ID, name and description.
3. To represent movie items, the system will include a class named Movie that inherits from the
abstract Item class. It will include the movie’s genre and director.
4. The system will contain a generic Recommender class that can be used to represent the
basic information and behavior of any recommender technique. Specifically, it will
contain the list of users, list of items, and an abstract method for generating
recommendations. This class is abstract.
5. The system will contain a more specific recommender class, UserCFRecommender, that
inherits from the generic Recommender class. It will contain methods that are specific
to this recommendation technique.
UML Diagram for the System Design:
Recall the meanings of the UML Symbols:
- Private
+ Public
# Protected
Lab Work:
1. You are provided with the java fil ...
Lab 8 User-CF Recommender System – Part I 50 points .docx
1. Lab 8
User-CF Recommender System – Part I
50 points
In this lab, we will focus on understanding the design of a user-
based collaborative filtering
recommender system. Then we will implement half that design
in this lab, and the other half in
the next lab.
System Description:
A user-based collaborative filtering (User-CF for short)
recommender works by making use of the
similarities between users in a system. To recommend items for
a specific user u, first User-CF finds the
n-most similar users to u. These are called the neighbors of u.
Then, the items that ratings that were given
by the neighbors of u for the different items are used to
generate recommendation scores for each item.
Finally, the m items with the highest recommendation scores are
recommended to u. The number of
neighbors n, and the number of items m can vary based on the
application in which the recommender is
2. used.
The overall flow of the user-based collaborative filtering
recommendation process can be summarized in
these steps:
1. Read movie information from file
2. Read user information from file
3. Generate movie recommendations for each user as follows.
For each user u:
a. Find u’s n most similar neighbors
i. Compute the Jaccard similarity between u and all other users
ii. Select the n users with the highest similarity values. Call
these n users the
neighbors of u.
b. Compute movie recommendation scores for u as follows.
i. For each movie, compute its recommendation score using the
rating information
for the neighbors of u.
ii. Select the m movies with the highest recommendation scores.
System Design:
1. The system will include a class named User to represent a
3. user. That class should include the user’s
ID, name, number of watched items, and a list containing the
IDs of the watched items. The class
should include a method for computing the number of similar
items between two users.
2. To allow for representing diverse items, the system will
include an abstract class named Item to
represent a generic item. This class will include the item’s ID,
name and description.
3. To represent movie items, the system will include a class
named Movie that inherits from the
abstract Item class. It will include the movie’s genre and
director.
4. The system will contain a generic Recommender class that
can be used to represent the
basic information and behavior of any recommender technique.
Specifically, it will
contain the list of users, list of items, and an abstract method
for generating
recommendations. This class is abstract.
5. The system will contain a more specific recommender class,
UserCFRecommender, that
4. inherits from the generic Recommender class. It will contain
methods that are specific
to this recommendation technique.
UML Diagram for the System Design:
Recall the meanings of the UML Symbols:
- Private
+ Public
# Protected
Lab Work:
1. You are provided with the java files containing all the class
names along with the class variables
and the method definitions.
2. You are required to implement the class methods that are
shown in Bold Red in the UML diagram
above.
3. The algorithm for each method is written, and commented,
inside each method in the java files.
4. Don’t forget to implement a constructor for each class to
5. initialize its data. Let your constructor
print a message indicating successful initialization of the object
data.
5. In your main method, create a UserCFRecommender object,
and use it to read the items and users
files. Then, print the user and item information to the console.
The users and items files are also
provided with the lab materials.
6. Run your main function and make sure that it is properly
reading the users and items information.
Lab Deliverables:
1. Zip all your code, along with a screen shot showing the
output obtained after you compiled and
run your code.
Movie.txt
1 , Toy story , Adventure Animation
2 , Jumanji , Adventure Fantasy
3 , Grumpy Old Men , Comedy Romance
4 , Waiting to Exhale , Comedy Drama Romance
5 , Father of the Bride Part II , Comedy
6 , Heat , Action Crime Thriller
7 , Sabrina , Comedy Romance
8 , Tom and Huck , Adventure Children
9 , Sudden Death , Action
6. 10 , GoldenEye , Action Adventure Thriller
11 , The American President , Comedy Drama Romance
12 , Dracula: Dead and Loving It , Comedy Horror
13 , Balto , Animation Children
14 , Nixon , Drama
15 , Cutthroat Island , Action Adventure Romance
16 , Casino , Crime Drama
17 , Sense and Sensibility , Comedy Drama Romance
18 , Four Rooms , Comedy Drama Thriller
19 , Ace Ventura: When Nature Calls , Comedy
20 , Money Train , Action Comedy Crime Drama Thriller
item.java
abstract public class Item{ //data protected int ID;
protected String name; protected String description;
//methods public Item() { ID = -1; name =
""; description = ""; } public Item(int ID, String n,
String d) { this.ID = ID; this.name = n;
this.description = d; } public int get_ID() {return ID;}
public void set_ID(int i) {ID = i;} public String
get_name() {return name;} public void set_name(String i)
{name = i;} public String get_desciption() {return
description;} public void set_description(String i)
{description = i;}}
movie.java
public class Movie extends Item{ private String genre;
private String director; //methods public Movie(int id,
String n, String d, String g, String dir) { super(id, n,
d); genre = g; director = dir; } public void
set_genre(String g) {genre = g;} public String
get_genre(String g) {return genre;} public void
set_director(String g) {director = g;} public String
7. get_director(String g) {return director;}}
RecApp.java
public class RecApp{ public static void main(String[] args)
{ UserCFRecommender ucf = new
UserCFRecommender("", "movies.txt"); }}
Recommender.java
import java.util.Scanner;import java.io.*;public abstract class
Recommender{ protected User[] users = new User[10];
protected Item[] items = new Item[10];
//=========================
//========================= //reads items from a
file protected void read_items(String file_name) { /*
1. Open the file 2. Read file line by line
3. For each line, Extract ID, Name, & Genre
Create a Movie object Set Item ID,
Name & Genre Add the Movie object into the Items[]
items 6. Close the file */ try {
File f = new File(file_name);
Scanner s = new Scanner(f);
while(s.hasNext()) { String[]
vals = (s.nextLine()).split(" , "); for(int i=0;
i< vals.length; i++)
System.out.println(vals[i]);
System.out.println(); } }
catch (FileNotFoundException e) {} }
//=========================
//========================= protected void
read_users(String file_name) { /* 1. Open the
file 2. Read file line by line 3. For each line,
Extract ID, n_items For i = 0 to n_items
Extract the Movie IDs Create a
User Object Set User ID, n_items, and add all
items to that user. Add the User object into the
8. Users[] users 4. Close the file */ }
//=========================
//========================= public User[]
get_users() { return users; }
//=========================
//========================= public Item[]
get_items() { return items; }
//=========================
//========================= public abstract void
make_recommendations();}
UserCFRecommender.java
public class UserCFRecommender extends Recommender{
public UserCFRecommender() {} public
UserCFRecommender(String ufile, String ifile) {
read_items(ifile); read_users(ufile); }
//computes the Jaccard similarity between two users give
the //indices of both users in the Users[] users list. public
double compute_jaccard_sim(int u1_ind, int u2_ind) {
//1. get n_common_items: int n_common_items =
(users[u1_ind]).similarity(users[u2_ind]); //2. get
total_n_items using u1 n_items, u2 n_items & n_common_items
//3. compute Jaccard sim as
n_common_items/total_n_items return 0; } //finds
the nieghbors of a user given the user index in the //Users[]
users, and given the number of neighbors//returns neighbors
indices, and neighbors similarity values public void
find_user_neighbors(int u_ind, int n_neighbors, int[] n_ind,
double[] n_sim) { /* 1. Create an array
sim[] with size = n_users to save all similarity
values in 2. For each other user u compute
similarity between u_ind and u by calling the
compute_jaccard_sim method, and save the result in sim[u]
3. Scan sim[] and select the "n_neighbors" users with
the highest similarities. for i = 0 -> n_neighbors -1:
9. ind = -1 max = -1
for j = 0 -> n_users -1: if
sim[j]>max: max = sim[j]
ind = j save max & ind
into n_sim[i] and n_ind[i] set sim[ind] = -2
*/ } // public void
compute_recommendation_scores(int u_ind, int[] n_ind,
double[] n_sim, double[] scores) { } // public void
make_recommendations() {}}
users.txt
1 4 1 2 10 192 3 1 6 193 3 1 2 74 2 1 105 4 1 5 7 116 6 1 7 11
18 19 207 5 1 2 7 11 178 5 5 9 11 16 199 7 1 2 3 6 9 10 1910 5
1 2 10 18 1911 3 1 2 1612 7 1 2 3 5 10 11 1913 9 1 2 3 5 10 11
13 19 2014 5 2 6 16 17 1915 6 1 3 5 6 10 16
User.java
public class User{ //data private int ID; private String
name; private int n_items; private int[] items = new
int[100]; //methods public User() { ID = -1;
name = ""; n_items = 0; } public User(int
id, String n) { ID = id; name = n; n_items
= 0; } public void add_item(int i) {
items[n_items] = i; n_items++; } //computes
the number of common items between this and u. public int
similarity(User u) { int n = u.get_n_items();
int[] uitems = u.get_items(); int sim = 0;
for(int i=0; i<n; i++) for(int j=0; j<n_items;
j++) if(uitems[i] == items[j]){
sim++; break; }
return sim; } public int get_ID() {return ID;}
public int[] get_items() {return items;} public int
get_n_items() {return n_items;} public int set_ID(int i)
{ID = i;}}