Skip to content

Instantly share code, notes, and snippets.

readme_text = np.array(open(f'{BASE_FOLDER}/MovieLens-1M/README').read().splitlines())
start_index = np.flatnonzero(np.core.defchararray.find(readme_text,'Occupation is chosen')!=-1)[0]
end_index = np.flatnonzero(np.core.defchararray.find(readme_text,'MOVIES FILE DESCRIPTION')!=-1)[0]
occupation_list = [x.split('"')[1] for x in readme_text[start_index:end_index][2:-1].tolist()]
occupation_dict = dict(zip(range(len(occupation_list)), occupation_list))
users_df = pd.read_csv(f'{BASE_FOLDER}/MovieLens-1M/users.dat',
delimiter='::', engine='python', header=None,
names=['user_id', 'gender', 'age', 'occupation', 'zip_code'])
users_df['occupation'] = users_df['occupation'].replace(occupation_dict)
user_id gender age occupation zip_code
0 1 F 1 K-12 student 48067
1 2 M 56 self-employed 70072
2 3 M 25 scientist 55117
3 4 M 45 executive/managerial 02460
4 5 M 25 writer 55455
users_df_occupation_by_gender = users_df.groupby(['occupation', 'gender']).size().reset_index(name='occupation_by_gender_count')
gender_count = users_df['gender'].value_counts()
users_df_occupation_by_gender['occupation_by_gender_count'] = \
users_df_occupation_by_gender['occupation_by_gender_count'].div(users_df_occupation_by_gender['gender'].map(gender_count))
users_df_occupation_by_gender
occupation_by_gender_plot = px.bar(users_df_occupation_by_gender, x='occupation', y='occupation_by_gender_count', color='gender', barmode='group')
occupation_by_gender_plot
ratings_df = pd.read_csv(f'{BASE_FOLDER}/MovieLens-1M/ratings.dat',
delimiter='::', engine='python', header=None,
names=['user_id', 'movie_id', 'rating', 'time'])
ratings_df.head()
user_id movie_id rating time
0 1 1193 5 978300760
1 1 661 3 978302109
2 1 914 3 978301968
3 1 3408 4 978300275
4 1 2355 5 978824291
combined_ratings_df = pd.merge(pd.merge(movies_df_exploded.rename_axis('movie_id'), ratings_df, on='movie_id'), users_df, on='user_id')
combined_ratings_data = combined_ratings_df.groupby(['genre', 'gender']).agg({'rating': ['mean', 'count']}).reset_index()
combined_ratings_data.columns = [' '.join(col).strip() for col in combined_ratings_data.columns.values]
combined_ratings_data.loc[combined_ratings_data['gender'] == 'F', 'rating count'] /= len(combined_ratings_df[combined_ratings_df['gender'] == 'F'])
combined_ratings_data.loc[combined_ratings_data['gender'] == 'M', 'rating count'] /= len(combined_ratings_df[combined_ratings_df['gender'] == 'M'])
px.bar(combined_ratings_data, x='genre', y='rating count', color='gender', barmode='group')
rating_by_genre_df = ratings_df.join(movies_df_exploded, on='movie_id').\
groupby('genre').agg({'rating': ['mean', 'count']}).sort_values(('rating', 'mean')).reset_index()
rating_by_genre_df.columns = ['_'.join(col).strip() for col in rating_by_genre_df.columns.values]
px.bar(rating_by_genre_df, x='genre_', y='rating_mean', height=300)
posters_df = pd.read_csv(f'{BASE_FOLDER}/MovieLens-1M/movie_poster.csv',
header=None, names=['URL'])
print(posters_df.head())
movies_df = movies_df.join(posters_df, how='inner')
sampled_movie_df = movies_df.sample(n=8)
images = sampled_movie_df['URL'].tolist()
names = sampled_movie_df['movie_name'].tolist()
ipyplot.plot_images(images, names)
def get_wikipedia_page_name(raw_name):
names = wikipedia.search(raw_name)
if len(names) == 0:
return ''
else:
return names[0]
def get_movie_plot(page_name):
try:
try:
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 2.
,movie_name,movie_plot
0,Toy Story (1995),"A group of living toys, who assume lifelessness around humans, are preparing to move into a new house with their owner Andy Davis, his sister Molly and their single mother. The toys become uneasy when Andy has his birthday party a week early; to calm them, Sheriff Woody, Andy's favorite toy and their leader, sends Sarge and his green army men to spy on the gift opening with a baby monitor. The other toys (which include Mr. Potato Head, Slinky Dog, Rex the tyrannosaur, Hamm the piggy bank, and Bo Peep the porcelain doll) are relieved when Andy receives nothing that could replace them. Andy then receives a last-minute surprise gift – a Buzz Lightyear action figure who believes he is a real space ranger. Buzz impresses the other toys with his various features and becomes Andy's new favorite, making Woody jealous.The day before the move, Andy's family plans for a dinner at Pizza Planet, where Andy is allowed to bring along only one toy. To ensure Andy chooses him and not