# ML之RS：基于用户的CF+LFM实现的推荐系统(基于相关度较高的用户实现电影推荐)

+关注继续查看

## 实现代码

#ML之RS：基于CF和LFM实现的推荐系统

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import time

import warnings

warnings.filterwarnings('ignore')

np.random.seed(1)

plt.style.use('ggplot')

#1、导入数据集

movies = movies.set_index('movieId')[['title', 'genres']]

#2、观察数据集

# How many users?

print (data.userId.nunique(), 'users')

# How many movies?

print (data.movieId.nunique(), 'movies')

# How possible ratings?

print (data.userId.nunique() * data.movieId.nunique(), 'possible ratings')

# How many do we have?

print (len(data), 'ratings')

print (100 * (float(len(data)) / (data.userId.nunique() * data.movieId.nunique())), '% of possible ratings')

# Number of ratings per users

fig = plt.figure(figsize=(10, 10))

ax = plt.hist(data.groupby('userId').apply(lambda x: len(x)).values, bins=50)

plt.xlabel("ratings")

plt.ylabel("users")

plt.title("Number of ratings per user")

plt.show()

# Number of ratings per movie

fig = plt.figure(figsize=(10, 10))

ax = plt.hist(data.groupby('movieId').apply(lambda x: len(x)).values, bins=50)

plt.xlabel("ratings")

plt.ylabel("movies")

plt.title('Number of ratings per movie')

plt.show()

# Ratings distribution评分分布

fig = plt.figure(figsize=(10, 10))

ax = plt.hist(data.rating.values, bins=5)

plt.xlabel("ratings")

plt.ylabel("numbers")

plt.title("Distribution of ratings")

plt.show()

# Average rating per user

fig = plt.figure(figsize=(10, 10))

ax = plt.hist(data.groupby('userId').rating.mean().values, bins=10)

plt.xlabel("Average rating")

plt.ylabel("numbers")

plt.title("Average rating per user")

plt.show()

# Average rating per movie

fig = plt.figure(figsize=(10, 10))

ax = plt.hist(data.groupby('movieId').rating.mean().values, bins=10)

plt.title('Average rating per movie')

plt.show()

# Top Movies，genres电影类型

average_movie_rating = data.groupby('movieId').mean()

pd.concat([movies.loc[top_movies.index.values],

average_movie_rating.loc[top_movies.index.values].rating], axis=1)

# Robust Top Movies - Lets weight the average rating by the square root of number of ratings让平均评分进行加权数的平方根

top_movies = data.groupby('movieId').apply(lambda x:len(x)**0.5 * x.mean()).sort_values('rating', ascending=False).head(10)

pd.concat([movies.loc[top_movies.index.values],

average_movie_rating.loc[top_movies.index.values].rating], axis=1)

controversial_movies = data.groupby('movieId').apply(lambda x:len(x)**0.25 * x.std()).sort_values('rating', ascending=False).head(10)

pd.concat([movies.loc[controversial_movies.index.values],

average_movie_rating.loc[controversial_movies.index.values].rating], axis=1)

6846 0

2796 0

4408 0
windows server 2008阿里云ECS服务器安全设置

5412 0

3824 0

1087 0
+关注

1701

0

《SaaS模式云原生数据仓库应用场景实践》

《看见新力量：二》电子书