• 基于内容的推荐例子(电影推荐)


    推荐例子介绍

    根据典型关键数据

    导演

    演员

    关键字

    题材

    'keywords', 'cast', 'genres', 'director'

    构造自然语言的组合特征,利用CountVectorizer计算每个词出现的次数,作为特征向量,

    使用余弦相似性构造所有电影之间的相似性。

    代码

    https://github.com/fanqingsong/Content-based-Recommandation-Engine

    import pandas as pd
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.metrics.pairwise import cosine_similarity
    
    
    def get_title_from_index(index):
        return df[df.index == index]["title"].values[0]
    
    
    def get_index_from_title(title):
        return df[df.title == title]["index"].values[0]
    
    
    # Reading CSV File
    df = pd.read_csv("movie_dataset_content.csv", encoding='utf-8')
    
    
    # Selecting Features
    features = ['keywords', 'cast', 'genres', 'director']
    
    # Creating a column in DF which combines all selected features
    for feature in features:
        df[feature] = df[feature].fillna('')
    
    
    def combine_features(row):
        return row['keywords'] + " " + row['cast'] + " " + row["genres"] + " " + row["director"]
    
    
    df["combined_features"] = df.apply(combine_features, axis=1)
    
    # making an object of CountVectorizer class to create count matrix
    cv = CountVectorizer()
    
    # Creating count matrix from this new combined column
    count_matrix = cv.fit_transform(df["combined_features"])
    
    # Computing the Cosine Similarity based on the count_matrix
    cosine_sim = cosine_similarity(count_matrix)
    
    movie_liked_by_user = "Thor"
    
    # Getting index of this movie from its title
    liked_movie_index = get_index_from_title(movie_liked_by_user)
    
    similar_movies = list(enumerate(cosine_sim[liked_movie_index]))
    
    # Get a list of similar movies in descending order of similarity score
    predictions = sorted(similar_movies, key=lambda x: x[1], reverse=True)
    
    # Print titles of 10 predicted movies
    i = 0
    for movie in predictions:
        print(get_title_from_index(movie[0]))
        i = i+1
        if i > 10:
            break

    运行

    root@DESKTOP-OGSLB14:~/mine/Content-based-Recommandation-Engine# python3 content_based_recommender.py
    Thor
    Thor: The Dark World
    The Avengers
    Captain America: The Winter Soldier
    Avengers: Age of Ultron
    Captain America: Civil War
    Pirates of the Caribbean: Dead Man's Chest
    Cinderella
    Jack Ryan: Shadow Recruit
    The Amazing Spider-Man 2
    Captain America: The First Avenger
    root@DESKTOP-OGSLB14:~/mine/Content-based-Recommandation-Engine#

  • 相关阅读:
    译:DOM2中的高级事件处理(转)
    Cookbook of QUnit
    URI编码解码和base64
    css截断长文本显示
    内置对象,原生对象和宿主对象
    HTML中的meta(转载)
    iframe编程的一些问题
    自动补全搜索实现
    new的探究
    深入instanceof
  • 原文地址:https://www.cnblogs.com/lightsong/p/11235333.html
Copyright © 2020-2023  润新知