-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_genre_analysis.py
334 lines (277 loc) · 10.8 KB
/
user_genre_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import random
from fuzzywuzzy import process
# Read CSV files into dataframes
ratings_df = pd.read_csv('ratings.csv')
movies_df = pd.read_csv('movies.csv')
all_titles = movies_df['title'].tolist()
def movie_finder(title):
return process.extractOne(title,all_titles)[0]
# Compute Bayesian average
movie_stats = ratings_df.groupby('movieId')['rating'].agg(['count', 'mean'])
C = movie_stats['count'].mean()
m = movie_stats['mean'].mean()
def bayesian_avg(ratings):
return round((C * m + ratings.sum()) / (C + ratings.count()), 3)
average_ratings_df = ratings_df.groupby('movieId')['rating'].agg(bayesian_avg).reset_index()
average_ratings_df.columns = ['movieId', 'average_rating']
# Genre mappings
genre_mappings = {
'Action': 'Action/Adventure',
'Adventure': 'Action/Adventure',
'Animation': 'Animation/Children',
'Children': 'Animation/Children',
'Fantasy': 'Fantasy/Western',
'Crime': 'Crime/Thriller',
'Thriller': 'Crime/Thriller',
'Mystery': 'Mystery/Film-Noir',
'Horror': 'Horror/Sci-Fi',
'Sci-Fi': 'Horror/Sci-Fi',
'War': 'War/Drama',
'Drama': 'War/Drama',
'Musical': 'Music/Romance',
'Romance': 'Music/Romance',
'Film-Noir': 'Mystery/Film-Noir',
'Documentary': 'Documentary/IMAX',
'IMAX': 'Documentary/IMAX',
'Western': 'Fantasy/Western'
}
# Function to combine genres based on mappings
def combine_genres(genres):
return '|'.join(genre_mappings.get(genre, '') for genre in genres.split('|'))
# Apply genre combination function to the dataset
movies_df['combined_genres'] = movies_df['genres'].apply(combine_genres)
# Merge ratings and movies data
combined_df = pd.merge(ratings_df, movies_df, on='movieId')
# Split the combined genres into individual genres
split_genres = combined_df['combined_genres'].str.split('|')
# Remove duplicates from each list of genres
unique_genres = split_genres.apply(set)
# Join the unique genres back together with "|"
cleaned_genres = unique_genres.apply('|'.join)
# Replace the combined_genres column with the cleaned genres
combined_df['combined_genres'] = cleaned_genres
# Get unique genres
all_genres = set('|'.join(movies_df['combined_genres']).split('|')) - {''}
unique_genres_list=all_genres
def plot_user_genre_preferences(user_id):
num_movies_genres = {}
# Step 1: Filter ratings by the specific user
user_ratings = combined_df[combined_df['userId'] == user_id]
for combined_genre in unique_genres_list:
num_movies_genres[combined_genre] = []
individual_genres = combined_genre.split('/')
if len(individual_genres) == 2:
num_movies_combined_genre = user_ratings[user_ratings['combined_genres'].str.contains(individual_genres[0])].shape[0]
num_movies_genres[combined_genre].append(num_movies_combined_genre)
num_movies_action_not_adventure = user_ratings[user_ratings['genres'].str.contains(individual_genres[0]) & ~user_ratings['genres'].str.contains(individual_genres[1])].shape[0]
num_movies_genres[combined_genre].append(num_movies_action_not_adventure)
num_movies_adventure_not_action = user_ratings[user_ratings['genres'].str.contains(individual_genres[1]) & ~user_ratings['genres'].str.contains(individual_genres[0])].shape[0]
num_movies_genres[combined_genre].append(num_movies_adventure_not_action)
num_movies_adventure_action = user_ratings[user_ratings['genres'].str.contains(individual_genres[0]) & user_ratings['genres'].str.contains(individual_genres[1])].shape[0]
num_movies_genres[combined_genre].append(num_movies_adventure_action)
# Define the data
data = {
'parent': [],
'character': [],
'value': []
}
# Iterate over the dictionary and format the data
for parent, values in num_movies_genres.items():
genres = parent.split('/')
genres.append("Both")
for i, genre in enumerate(genres):
j = i + 1
data['parent'].append(parent)
data['character'].append(genre)
data['value'].append(values[j])
# Create DataFrame
data_df = pd.DataFrame(data)
# Plotting
fig = px.sunburst(data_df, path=['parent', 'character'], values='value',
title=f'Sunburst Chart of Genres and Sub-Genres for User {user_id}')
return fig
# Initialize an empty dictionary to store users fond of each genre
users_fond_of_genre = {}
# Loop through each genre in unique_genres_list
for genre in all_genres:
# Filter the dataset to include only rows where the movie belongs to the current genre
genre_movies_df = combined_df[combined_df['combined_genres'].str.contains(genre)]
# Group the filtered dataset by user ID and count the number of unique movies each user has rated
genre_movie_counts = genre_movies_df.groupby('userId')['movieId'].nunique()
# Calculate the 95th percentile value for the current genre
percentile_95 = genre_movie_counts.quantile(0.98)
# Identify users who have rated a significant number of movies for the current genre
threshold = percentile_95 # Using the 95th percentile value as the threshold
users_fond_of_current_genre = genre_movie_counts[genre_movie_counts >= threshold].index.tolist()
# Store the list of users in the dictionary with the genre as the key
users_fond_of_genre[genre] = users_fond_of_current_genre
del genre_movies_df
def get_horizontal_bar_chart(movie_list):
movies_list = [movie_finder(movie) for movie in movie_list]
# print(movies_list)
filtered_df = movies_df[movies_df['title'].isin(movie_list)]
numbers_list = [int(movies_df[movies_df['title'] == title]['movieId']) for title in movies_list]
movie_list = numbers_list
# print(numbers_list)
# Calculate average ratings for each genre for each movie
genre_average_ratings = {}
for random_movie_id in numbers_list:
movie_row = combined_df[combined_df['movieId'] == random_movie_id]
genres = movie_row['combined_genres'].iloc[0].split('|')
if '' in genres:
genres.remove('')
genre_average_ratings[random_movie_id] = {}
for genre in genres:
filtered_ratings = combined_df[(combined_df['userId'].isin(users_fond_of_genre[genre])) & (combined_df['movieId'] == random_movie_id)]
average_rating = filtered_ratings['rating'].mean()
if average_rating == 0:
average_rating = average_ratings_df.loc[average_ratings_df['movieId'] == random_movie_id, 'average_rating'].iloc[0]
genre_average_ratings[random_movie_id][genre] = average_rating
for genre in all_genres:
if genre not in genres:
genre_average_ratings[random_movie_id][genre] = 0
# print(genre_average_ratings)
#import plotly.graph_objects as go
data=genre_average_ratings
# Initialize lists to store x and y values
x_values = []
x_values1 = []
x_values2 = []
x_values3 = []
x_values4 = []
x_values5 = []
x_values6 = []
x_values7 = []
x_values8 = []
x_values9 = []
y_values = movies_list
# Extract ratings for each genre for each movie
for movie_id, genre_ratings in data.items():
for genre, rating in genre_ratings.items():
if genre == 'Animation/Children':
x_values.append(rating)
elif genre == 'Action/Adventure':
x_values1.append(rating)
elif genre == 'War/Drama':
x_values2.append(rating)
elif genre == 'Documentary':
x_values3.append(rating)
elif genre == 'Crime/Thriller':
x_values4.append(rating)
elif genre == 'Documentary/IMAX':
x_values5.append(rating)
elif genre == 'Music/Romance':
x_values6.append(rating)
elif genre == 'Horror/Sci-Fi':
x_values7.append(rating)
elif genre == 'Fantasy/Western':
x_values8.append(rating)
elif genre == 'Mystery/Film-Noir':
x_values9.append(rating)
print(x_values)
fig = go.Figure()
fig.add_trace(go.Bar(
y=y_values,
x=x_values,
name='Animation/Children',
orientation='h',
marker=dict(
color='rgba(246, 78, 139, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values1,
name='Action/Adventure',
orientation='h',
marker=dict(
color='rgba(58, 71, 80, 0.6)',
line=dict(color='rgba(58, 71, 80, 1.0)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values2,
name='War/Drama',
orientation='h',
marker=dict(
color='rgba(0, 100, 0, 0.6)',
line=dict(color='rgba(0, 100, 0, 1)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values3,
name='Documentary',
orientation='h',
marker=dict(
color='rgba(0, 0, 100, 0.6)',
line=dict(color='rgba(0, 0, 100, 1)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values4,
name='Crime/Thriller',
orientation='h',
marker=dict(
color='rgba(100, 0, 0, 0.6)',
line=dict(color='rgba(100, 0, 0, 1)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values5,
name='Documentary/IMAX',
orientation='h',
marker=dict(
color='rgba(255, 0, 10, 0.6)',
line=dict(color='rgba(255, 0, 10, 1)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values6,
name='Music/Romance',
orientation='h',
marker=dict(
color='rgba(96, 89, 161, 0.6)',
line=dict(color='rgba(96, 89, 161, 1)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values7,
name='Horror/Sci-Fi',
orientation='h',
marker=dict(
color='rgba(122, 255, 153, 0.6)',
line=dict(color='rgba(122, 255, 153, 1)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values8,
name='Fantasy/Western',
orientation='h',
marker=dict(
color='rgba(122, 255, 255, 0.6)',
line=dict(color='rgba(122, 255, 255, 1)', width=3)
)
))
fig.add_trace(go.Bar(
y=y_values,
x=x_values9,
name='Mystery/Film-Noir',
orientation='h',
marker=dict(
color='rgba(252, 101, 0, 0.6)',
line=dict(color='rgba(252, 101, 0, 1)', width=3)
)
))
fig.update_layout(barmode='stack')
return fig