1. 程式人生 > >ML之RS:基於使用者的CF+LFM實現的推薦系統(基於相關度較高的使用者實現電影推薦)

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')
# data = pd.read_csv('ml-20m/ratings_smaller.csv', index_col=0)
# movies = pd.read_csv('ml-20m/movies_smaller.csv')

#1、匯入資料集
data = pd.read_csv('ml-latest-small/ratings.csv')
movies = pd.read_csv('ml-latest-small/movies.csv')
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()
top_movies = average_movie_rating.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)

# 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)

相關文章推薦

GitHub