当前位置: 首页>>代码示例>>Python>>正文


Python SVD.create_matrix方法代码示例

本文整理汇总了Python中recsys.algorithm.factorize.SVD.create_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python SVD.create_matrix方法的具体用法?Python SVD.create_matrix怎么用?Python SVD.create_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在recsys.algorithm.factorize.SVD的用法示例。


在下文中一共展示了SVD.create_matrix方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SVD

# 需要导入模块: from recsys.algorithm.factorize import SVD [as 别名]
# 或者: from recsys.algorithm.factorize.SVD import create_matrix [as 别名]
#This is the recommendation algorithm based on the SVD 	
#This code can be run in real time but the model has to be pre-computed

import recsys.algorithm
from recsys.algorithm.factorize import SVD


#Lets make things Verbose
recsys.algorithm.VERBOSE = True
#Loading the computed model
svd = SVD(filename='movielens_small')
svd.load_data(filename='ratings_small.csv', sep=',', format={'col':0, 'row':1, 'value':2, 'ids':int})
svd.create_matrix()
#Loading the movielens file of movies which has a mapping of movies to movie-id
loop = True

while (loop):
    ratings_file = open('ratings_small.csv', 'r+')
    movie_lens = open('movies.csv', 'r+')
    user_found = False
    movie_found = False
    USERID = int(input("Enter user id: "))
    #Check if the user_id exists. Since currently we are using the small database, we need to check each and every field.
    #If using the complete database, just check if the number lies in the range.
    for rating_row in ratings_file:
        rating_item = rating_row.split(',')
        if (int(rating_item[0]) == USERID):
            user_found = True
            break 
    if (movie_found):
        for movie_row in movie_lens:
开发者ID:tbarnes94,项目名称:Movie-Recommendation-Project,代码行数:33,代码来源:svd_recomm_unknown.py

示例2: RecommendationSystem

# 需要导入模块: from recsys.algorithm.factorize import SVD [as 别名]
# 或者: from recsys.algorithm.factorize.SVD import create_matrix [as 别名]
class RecommendationSystem():
    # To run on your own machine, you need to initialize with your datapath to the frontend folder
    def __init__(self, sc, datapath='/media/psf/Home/CS/GIT_HUB/Movie-Recommendation-Project/frontend/', rating_file='ratings_small.csv', complete_rating_file='ratings.csv', movie_file='movies.csv', detail_file='modified.csv', model='movielens_small'):
        self.sc = sc
        self.start = True
        self.rating_file = datapath+rating_file
        self.complete_rating_file = datapath+complete_rating_file
        self.movie_file = datapath+movie_file
        self.detail_file = datapath+detail_file
        self.integration_folder = datapath
        self.svd = SVD(filename=datapath+model)
        self.svd.load_data(filename=self.rating_file, sep=',', format={'col': 0, 'row': 1, 'value': 2, 'ids': int})
        self.svd.create_matrix()
        self.ia = imdb.IMDb(accessSystem='http')

        # als stuff
        self.sqlContext = SQLContext(self.sc)
        self.movie_data = self.sc.textFile(self.movie_file)
        self.ratings_data = self.sc.textFile(self.complete_rating_file).map(lambda line: line.split(",")).map(lambda x: (int(x[0]), int(x[1]), float(x[2])))
        self.als_model_path = datapath + 'Model_Collaborative_Filtering'
        self.als_model = MatrixFactorizationModel.load(sc, self.als_model_path)
        self.movie_df = self.sqlContext.read.load(datapath+'tables/movies')
        self.detail_df = self.sqlContext.read.load(datapath+'tables/detail')
        self.rating_df = self.sqlContext.read.load(datapath+'tables/ratings')


    # call this function to get all recommendations
    def get_all_recomm(self, userid, moviename):
        movieid = self.get_movie_id(moviename)

        # all recommendation algorithms return a list of movie ids
        recom1 = self.svd_recomm(userid, only_unknown=True)
        recom2 = self.svd_similar(movieid)
        recom3 = self.als_new(userid)

        #get info about the movie based on movie ids
        brief_info1 = self.get_brief_list(recom1)
        brief_info2 = self.get_brief_list(recom2)
        brief_info3 = self.get_brief_list(recom3)

        # print to terminal
        for l1 in brief_info1:
            print l1
        for l2 in brief_info2:
            print l2
        for l3 in brief_info3:
            print l3

        return [brief_info1, brief_info2, brief_info3]

    # get movie id based on movie name input
    def get_movie_id(self, moviename):
        r = self.movie_df.where(self.movie_df['name'].startswith(moviename)).first()

        # return movie id 1 if not found
        if r is None:
            return 1

        return r['movieId']

    # svd recommendation algorithm based on the user's rating history, set only_known to True for unseen movies
    def svd_recomm(self, userid, only_unknown):
        user_found = False
        ratings = open(self.rating_file, 'r')
        for rating_row in ratings:
            rating_item = rating_row.split(',')
            if int(rating_item[0]) == userid:
                user_found = True
                break

        ratings.close()
        if not user_found:
            return None

        # output format: (movieid, similarity value)
        if only_unknown:
            similar_list = self.svd.recommend(userid, n=10, only_unknowns=True, is_row=True)
        else:
            similar_list = self.svd.recommend(userid, n=10, only_unknowns=False, is_row=False)

        movieid_list = self.get_id_list(similar_list)
        return movieid_list

    # svd recommendation algorithm based on similar movie
    def svd_similar(self, movieid):
        movie_found = False
        movies = open(self.movie_file, 'r')
        for movie_row in movies:
            row_item = movie_row.split(',')
            if int(row_item[0]) == movieid:
                movie_found = True
                break

        movies.close()
        if not movie_found:
            return None

        similar_list = self.svd.similar(movieid)
        movieid_list = self.get_id_list(similar_list)
        return movieid_list
#.........这里部分代码省略.........
开发者ID:ece4813-movie-recommendation,项目名称:Movie-Recommendation-Project,代码行数:103,代码来源:engine.py

示例3: RecommendationSystem

# 需要导入模块: from recsys.algorithm.factorize import SVD [as 别名]
# 或者: from recsys.algorithm.factorize.SVD import create_matrix [as 别名]
class RecommendationSystem():
    #def __init__(self, spark_context, rating_file='ratings_small.csv', movie_file='movies.csv', detail_file='modified.csv', model='movielens_small'):
    def __init__(self, rating_file='ratings_small.csv', movie_file='movies.csv', detail_file='modified.csv', model='movielens_small'):
        self.start = True
        self.rating_file = rating_file
        self.movie_file = movie_file
        self.detail_file = detail_file
        self.svd = SVD(filename=model)
        self.svd.load_data(filename=rating_file, sep=',', format={'col': 0, 'row': 1, 'value': 2, 'ids': int})
        self.svd.create_matrix()
        self.ia = imdb.IMDb(accessSystem='http')

    def get_all_recomm(self, userid, movieid):
        recom1 = self.svd_recomm(userid, only_unknown=False)
        recom2 = self.svd_recomm(userid, only_unknown=True)
        recom3 = self.svd_similar(movieid)

        brief_info1 = self.get_brief_list(recom1)
        brief_info2 = self.get_brief_list(recom2)
        brief_info3 = self.get_brief_list(recom3)

        return [brief_info1, brief_info2, brief_info3]

    def svd_recomm(self, userid, only_unknown):
        user_found = False
        ratings = open(self.rating_file, 'r')
        for rating_row in ratings:
            rating_item = rating_row.split(',')
            if int(rating_item[0]) == userid:
                user_found = True
                break

        ratings.close()
        if not user_found:
            return None

        #output format: (movieid, similarity value)
        if only_unknown:
            similar_list = self.svd.recommend(userid, n=10, only_unknowns=True, is_row=True)
        else:
            similar_list = self.svd.recommend(userid, n=10, only_unknowns=False, is_row=False)

        movieid_list = self.get_id_list(similar_list)
        return movieid_list

    def svd_similar(self, movieid):
        movie_found = False
        movies = open(self.movie_file, 'r')
        for movie_row in movies:
            row_item = movie_row.split(',')
            if (int(row_item[0]) == movieid):
                movie_found = True
                break

        movies.close()
        if not movie_found:
            return None

        similar_list = self.svd.similar(movieid)
        movieid_list = self.get_id_list(similar_list)
        return movieid_list

    def get_id_list(self, l):
        movieid_list = []
        for s in l:
            movieid_list.append(s[0])
        return movieid_list

    def get_detail(self, imdb_id):
        #print type(imdb_id)
        m = self.ia.get_movie(str(imdb_id))

        cover = m.get('cover url')
        if cover:
            path = "Images/" + str(imdb_id) + ".jpg"
            urllib.urlretrieve(cover, path)

        return m

    def get_brief_list(self, movieList):
        info_list = []
        for m in movieList:
            info = self.get_brief(m)
            info_list.append(info)
        return info_list

    def get_brief(self, movieid):
        info = {}
        info['title'] = 'unknown'
        info['genre'] = 'unknown'
        info['rating'] = 0
        info['imdb_id'] = 1
        info['director'] = 'unknown'
        info['cast'] = 'unknown'

        movies = open(self.movie_file, 'r')
        for m in movies:
            row_item = m.split(',')
            if int(row_item[0]) == movieid:
                info['title'] = str(row_item[1].strip())
#.........这里部分代码省略.........
开发者ID:tbarnes94,项目名称:Movie-Recommendation-Project,代码行数:103,代码来源:engine.py


注:本文中的recsys.algorithm.factorize.SVD.create_matrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。