tobiasaurer commited on
Commit
4fdddbb
1 Parent(s): f44fadf

rename application file

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+
4
+
5
+ st.title("Movie Recommender")
6
+
7
+ st.write("""
8
+ ### Project description
9
+ Type in a movie title with the release year in brackets (e.g. "The Matrix (1999)"), choose the number of recommendations you wish, and the app will recommend movies based on your chosen movie.
10
+ The recommendation process will take ca. 15 seconds.
11
+ """)
12
+
13
+ chosen_movie = st.text_input("Movie title and release year")
14
+ number_of_recommendations = st.slider("Number of recommendations", 1, 10, 5)
15
+
16
+ movies = pd.read_csv('https://raw.githubusercontent.com/tobiasaurer/recommender-systems/main/movie_data/movies.csv')
17
+ ratings = pd.read_csv('https://raw.githubusercontent.com/tobiasaurer/recommender-systems/main/movie_data/ratings.csv')
18
+
19
+ all_ratings = ratings.merge(movies, on='movieId')[['title', 'rating', 'userId']]
20
+ all_ratings_pivoted = all_ratings.pivot_table(index='userId', columns='title', values='rating')
21
+
22
+ def get_recommendations_for_movie(movie_name, n):
23
+
24
+ eligible_movies = []
25
+
26
+ for movie in all_ratings_pivoted.columns:
27
+ nr_shared_ratings = all_ratings_pivoted.loc[all_ratings_pivoted[movie_name].notnull() & all_ratings_pivoted[movie].notnull(), [movie_name, movie]].count()[0]
28
+ if nr_shared_ratings >= 10:
29
+ eligible_movies.append(movie)
30
+
31
+ return (
32
+ all_ratings_pivoted
33
+ [eligible_movies]
34
+ .corrwith(all_ratings_pivoted[movie_name]).sort_values(ascending=False)[1:n+1]
35
+ .index
36
+ )
37
+
38
+ if st.button("Recommend"):
39
+
40
+ recommendations = get_recommendations_for_movie(chosen_movie, number_of_recommendations)
41
+
42
+ st.write("Recommendations for", chosen_movie)
43
+ st.write(recommendations)