Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class RecSys ( ) : def _ _ init _ _ ( self , data ) : self.data = data self.allusers = list ( self

class RecSys():
def __init__(self,data):
self.data=data
self.allusers = list(self.data.users['uID'])
self.allmovies = list(self.data.movies['mID'])
self.genres = list(self.data.movies.columns.drop(['mID', 'title', 'year']))
self.mid2idx = dict(zip(self.data.movies.mID,list(range(len(self.data.movies)))))
self.uid2idx = dict(zip(self.data.users.uID,list(range(len(self.data.users)))))
self.Mr=self.rating_matrix()
self.Mm=None
self.sim=np.zeros((len(self.allmovies),len(self.allmovies)))
def rating_matrix(self):
"""
Convert the rating matrix to numpy array of shape (#allusers,#allmovies)
"""
ind_movie =[self.mid2idx[x] for x in self.data.train.mID]
ind_user =[self.uid2idx[x] for x in self.data.train.uID]
rating_train = list(self.data.train.rating)
return np.array(coo_matrix((rating_train, (ind_user, ind_movie)), shape=(len(self.allusers), len(self.allmovies))).toarray())
def predict_everything_to_3(self):
"""
Predict everything to 3 for the test data
"""
# Generate an array with 3s against all entries in test dataset
# your code here
return np.full(len(self.data.test),3)
def predict_to_user_average(self):
"""
Predict to average rating for the user.
Returns numpy array of shape (#users,)
"""
# Generate an array as follows:
# 1. Calculate all avg user rating as sum of ratings of user across all movies/number of movies whose rating >0
# 2. Return the average rating of users in test data
# your code here
sum_ratings = np.sum(self.Mr, axis=1)
total_ratings = np.sum((self.Mr >0).astype(int), axis=1)
avg_ratings = sum_ratings / total_ratings
# avg_ratings = np.nanmean(self.Mr, axis=1)
user_indices =[self.uid2idx[x] for x in self.data.test.uID]
return avg_ratings[user_indices]
# pass
def predict_from_sim(self,uid,mid):
"""
Predict a user rating on a movie given userID and movieID
"""
# Predict user rating as follows:
# 1. Get entry of user id in rating matrix
# 2. Get entry of movie id in sim matrix
# 3. Employ 1 and 2 to predict user rating of the movie
# your code here
user_idx = self.uid2idx[uid]
movie_idx = self.mid2idx[mid]
valid_ratings =(self.Mr[user_idx]!=0)
# print(self.Mr.shape, self.sim.shape)
sim_ratings =(self.Mr[user_idx])*(self.sim[:, movie_idx])
valid_sim =(self.sim[:, movie_idx])*(valid_ratings)
sim_sum = np.sum(valid_sim)
return np.sum(sim_ratings)/(sim_sum if sim_sum !=0 else np.nan)
# movie_rating = self.Mr[user_idx]
# sim_rating = self.sim[movie_idx]
#print(movie_rating[movie_rating >0], sim_rating[sim_rating >0])
#print(sim_ratings[sim_ratings >0])
# sim_sum = np.sum(self.sim[movie_idx, :])
# predicted_rating = np.sum(sim_ratings)/(sim_sum if sim_sum !=0 else 1)
# return np.sum(sim_ratings)/(sim_sum if sim_sum !=0 else 0)
# return predicted_rating
# def predict_from_sim(self, uid, mid):
# Predict a user rating on a movie given userID and movieID
# user_index = self.uid2idx[uid]
# movie_index = self.mid2idx[mid]
# user_rating = self.Mr[user_index, :]
# sim_scores = self.sim[movie_index, :]
# return np.dot(user_rating, sim_scores)/ np.sum(sim_scores[user_rating >0])
# pass
def predict(self):
"""
Predict ratings in the test data. Returns predicted rating in a numpy array of size (# of rows in testdata,)
"""
# your code here
predictions =[]
for i in range(len(self.data.test)):
uid = self.data.test.iloc[i]['uID']
mid = self.data.test.iloc[i]['mID']
predictions.append(self.predict_from_sim(uid, mid))
return np.array(predictions)
# pass
def rmse(self,yp):
yp[np.isnan(yp)]=3 #In case there is nan values in prediction, it will impute to 3.
yt=np.array(self.data.test.rating)
return np.sqrt(((yt-yp)**2).mean())
class ContentBased(RecSys):
def __init__(self,data):
super().__init__(data)
self.data=data
self.Mm = self.calc_movie_feature_matrix()
def calc_movie_feature_matrix(self):
"""
Create movie feature matrix in a numpy array of shape (#allmovies, #genres)
"""
# your code here
movie_features = np.zeros((len(self.allmovies), le

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions