Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class RecSys ( ) : def _ _ init _ _ ( self , data ) : self.data = data self.allusers = list ( self
class RecSys:
def initselfdata:
self.datadata
self.allusers listselfdata.usersuID
self.allmovies listselfdata.moviesmID
self.genres listselfdata.movies.columns.dropmID 'title', 'year'
self.mididx dictzipselfdata.movies.mID,listrangelenselfdata.movies
self.uididx dictzipselfdata.users.uID,listrangelenselfdata.users
self.Mrself.ratingmatrix
self.MmNone
self.simnpzeroslenselfallmovieslenselfallmovies
def ratingmatrixself:
Convert the rating matrix to numpy array of shape #allusers,#allmovies
indmovie selfmididxx for x in self.data.train.mID
induser selfuididxx for x in self.data.train.uID
ratingtrain listselfdata.train.rating
return nparraycoomatrixratingtrain, induser, indmovie shapelenselfallusers lenselfallmoviestoarray
def predicteverythingtoself:
Predict everything to for the test data
# Generate an array with s against all entries in test dataset
# your code here
return npfulllenselfdata.test
def predicttouseraverageself:
Predict to average rating for the user.
Returns numpy array of shape #users,
# Generate an array as follows:
# Calculate all avg user rating as sum of ratings of user across all moviesnumber of movies whose rating
# Return the average rating of users in test data
# your code here
sumratings npsumselfMr axis
totalratings npsumselfMr astypeint axis
avgratings sumratings totalratings
# avgratings npnanmeanselfMr axis
userindices selfuididxx for x in self.data.test.uID
return avgratingsuserindices
# pass
def predictfromsimselfuid,mid:
Predict a user rating on a movie given userID and movieID
# Predict user rating as follows:
# Get entry of user id in rating matrix
# Get entry of movie id in sim matrix
# Employ and to predict user rating of the movie
# your code here
useridx self.uididxuid
movieidx self.mididxmid
validratings selfMruseridx
# printselfMrshape, self.sim.shape
simratings selfMruseridxselfsim: movieidx
validsim selfsim: movieidxvalidratings
simsum npsumvalidsim
return npsumsimratingssimsum if simsum else npnan
# movierating self.Mruseridx
# simrating self.simmovieidx
#printmovieratingmovierating simratingsimrating
#printsimratingssimratings
# simsum npsumselfsimmovieidx, :
# predictedrating npsumsimratingssimsum if simsum else
# return npsumsimratingssimsum if simsum else
# return predictedrating
# def predictfromsimself uid, mid:
# Predict a user rating on a movie given userID and movieID
# userindex self.uididxuid
# movieindex self.mididxmid
# userrating self.Mruserindex, :
# simscores self.simmovieindex, :
# return npdotuserrating, simscores npsumsimscoresuserrating
# pass
def predictself:
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 rangelenselfdata.test:
uid self.data.test.ilociuID
mid self.data.test.ilocimID
predictions.appendselfpredictfromsimuid mid
return nparraypredictions
# pass
def rmseselfyp:
ypnpisnanyp #In case there is nan values in prediction, it will impute to
ytnparrayselfdata.test.rating
return npsqrtytypmean
class ContentBasedRecSys:
def initselfdata:
superinitdata
self.datadata
self.Mm self.calcmoviefeaturematrix
def calcmoviefeaturematrixself:
Create movie feature matrix in a numpy array of shape #allmovies, #genres
# your code here
moviefeatures npzeroslenselfallmovies le
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started