登录
首页 >  文章 >  python教程

如何在Python中构建一个简单的推荐系统

时间:2023-10-20 19:17:41 490浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《如何在Python中构建一个简单的推荐系统》,这篇文章主要讲到等等知识,如果你对文章相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

如何在Python中构建一个简单的推荐系统

推荐系统是为了帮助人们发现和选择他们可能感兴趣的物品而设计的。Python提供了丰富的库和工具,可以帮助我们构建一个简单但有效的推荐系统。本文将介绍如何使用Python构建一个基于用户的协同过滤推荐系统,并提供具体的代码示例。

协同过滤是一种推荐系统的常见算法,它基于用户的行为历史数据来推断用户之间的相似性,然后利用这些相似性来预测和推荐物品。我们将使用MovieLens数据集,它包含了一组用户对电影的评分数据。首先,我们需要安装所需的库:

pip install pandas scikit-learn

接下来,我们将导入所需的库并加载MovieLens数据集:

import pandas as pd
from sklearn.model_selection import train_test_split

# 加载数据集
data = pd.read_csv('ratings.csv')

该数据集包含userIdmovieIdrating三列,分别表示用户ID、电影ID和评分。接下来,我们将数据集拆分为训练集和测试集:

train_data, test_data = train_test_split(data, test_size=0.2, random_state=42)

现在,我们可以构建推荐系统了。这里我们将使用用户间的余弦相似度作为相似度度量指标。我们将创建两个字典来存储用户和电影的相似度得分:

# 计算用户之间的相似度
def calculate_similarity(train_data):
    similarity = dict()
    for user in train_data['userId'].unique():
        similarity[user] = dict()
        user_ratings = train_data[train_data['userId'] == user]
        for movie in user_ratings['movieId'].unique():
            similarity[user][movie] = 1.0

    return similarity

# 计算用户之间的相似度得分
def calculate_similarity_score(train_data, similarity):
    for user1 in similarity.keys():
        for user2 in similarity.keys():
            if user1 != user2:
                user1_ratings = train_data[train_data['userId'] == user1]
                user2_ratings = train_data[train_data['userId'] == user2]
                num_ratings = 0
                sum_of_squares = 0
                for movie in user1_ratings['movieId'].unique():
                    if movie in user2_ratings['movieId'].unique():
                        num_ratings += 1
                        rating1 = user1_ratings[user1_ratings['movieId'] == movie]['rating'].values[0]
                        rating2 = user2_ratings[user2_ratings['movieId'] == movie]['rating'].values[0]
                        sum_of_squares += (rating1 - rating2) ** 2
                similarity[user1][user2] = 1 / (1 + (sum_of_squares / num_ratings) ** 0.5)

    return similarity

# 计算电影之间的相似度得分
def calculate_movie_similarity_score(train_data, similarity):
    movie_similarity = dict()
    for user in similarity.keys():
        for movie in train_data[train_data['userId'] == user]['movieId'].unique():
            if movie not in movie_similarity.keys():
                movie_similarity[movie] = dict()

            for other_movie in train_data[train_data['userId'] == user]['movieId'].unique():
                if movie != other_movie:
                    movie_similarity[movie][other_movie] = similarity[user][other_user]

    return movie_similarity

# 构建推荐系统
def build_recommendation_system(train_data, similarity, movie_similarity):
    recommendations = dict()
    for user in train_data['userId'].unique():
        user_ratings = train_data[train_data['userId'] == user]
        recommendations[user] = dict()
        for movie in train_data['movieId'].unique():
            if movie not in user_ratings['movieId'].unique():
                rating = 0
                num_movies = 0
                for other_user in similarity[user].keys():
                    if movie in train_data[train_data['userId'] == other_user]['movieId'].unique():
                        rating += similarity[user][other_user] * train_data[(train_data['userId'] == other_user) & (train_data['movieId'] == movie)]['rating'].values[0]
                        num_movies += 1
                if num_movies > 0:
                    recommendations[user][movie] = rating / num_movies

    return recommendations

# 计算评价指标
def calculate_metrics(recommendations, test_data):
    num_users = 0
    sum_of_squared_error = 0
    for user in recommendations.keys():
        if user in test_data['userId'].unique():
            num_users += 1
            for movie in recommendations[user].keys():
                if movie in test_data[test_data['userId'] == user]['movieId'].unique():
                    predicted_rating = recommendations[user][movie]
                    actual_rating = test_data[(test_data['userId'] == user) & (test_data['movieId'] == movie)]['rating'].values[0]
                    sum_of_squared_error += (predicted_rating - actual_rating) ** 2
    rmse = (sum_of_squared_error / num_users) ** 0.5

    return rmse

# 计算用户之间的相似度
similarity = calculate_similarity(train_data)

# 计算用户之间的相似度得分
similarity = calculate_similarity_score(train_data, similarity)

# 计算电影之间的相似度得分
movie_similarity = calculate_movie_similarity_score(train_data, similarity)

# 构建推荐系统
recommendations = build_recommendation_system(train_data, similarity, movie_similarity)

# 计算评价指标
rmse = calculate_metrics(recommendations, test_data)

最后,我们可以输出推荐系统的结果和评价指标:

print(recommendations)
print('RMSE:', rmse)

通过上述代码示例,我们在Python中成功构建了一个基于用户的协同过滤推荐系统,并计算了其评价指标。当然,这只是一个简单的示例,实际的推荐系统需要更复杂的算法和更大规模的数据集来获得更准确的推荐结果。

总结一下,Python提供了强大的库和工具来构建推荐系统,我们可以使用协同过滤算法来推断用户之间的相似性,并根据这些相似性来进行推荐。希望本文能够帮助读者理解如何在Python中构建一个简单但有效的推荐系统,并为进一步探索推荐系统的领域提供了一些思路。

今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>