Spaces:
Runtime error
Runtime error
tobiasaurer
commited on
Commit
•
b4c7df7
1
Parent(s):
bd1ac10
reupload of current files
Browse files- app.py +1 -10
- pages/1 - Popularity based recommender.py +40 -4
- pages/2 - User based recommender.py +0 -0
- pages/3 - Old recommender.py +0 -43
app.py
CHANGED
@@ -1,18 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
|
4 |
-
st.markdown("# Introduction")
|
5 |
-
st.sidebar.markdown("# Introduction")
|
6 |
-
|
7 |
st.title("Movie Recommenders")
|
8 |
|
9 |
st.write("""
|
10 |
### Instructions
|
11 |
Check the Sidebar and choose a recommender that suits your purpose.
|
12 |
-
""")
|
13 |
-
|
14 |
-
movies = pd.read_csv('https://raw.githubusercontent.com/tobiasaurer/recommender-systems/main/movie_data/movies.csv')
|
15 |
-
ratings = pd.read_csv('https://raw.githubusercontent.com/tobiasaurer/recommender-systems/main/movie_data/ratings.csv')
|
16 |
-
|
17 |
-
all_ratings = ratings.merge(movies, on='movieId')[['title', 'rating', 'userId']]
|
18 |
-
all_ratings_pivoted = all_ratings.pivot_table(index='userId', columns='title', values='rating')
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
|
|
|
|
|
|
|
4 |
st.title("Movie Recommenders")
|
5 |
|
6 |
st.write("""
|
7 |
### Instructions
|
8 |
Check the Sidebar and choose a recommender that suits your purpose.
|
9 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
pages/1 - Popularity based recommender.py
CHANGED
@@ -1,12 +1,48 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
4 |
|
5 |
st.write("""
|
6 |
### Instructions
|
7 |
-
|
|
|
|
|
8 |
""")
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
st.write(
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
|
4 |
+
# Import Data
|
5 |
+
movies = pd.read_csv('https://raw.githubusercontent.com/tobiasaurer/recommender-systems/main/movie_data/movies.csv')
|
6 |
+
ratings = pd.read_csv('https://raw.githubusercontent.com/tobiasaurer/recommender-systems/main/movie_data/ratings.csv')
|
7 |
+
|
8 |
+
st.title("Popularity-Based Recommender")
|
9 |
|
10 |
st.write("""
|
11 |
### Instructions
|
12 |
+
Move the slider to the desired number of recommendations you wish to receive.
|
13 |
+
Afterwards, simply click the "Recommend!" button to receive recommendations of the most popular movies in our database.
|
14 |
+
If you want, you can narrow it down by picking one or several genre(s).
|
15 |
""")
|
16 |
|
17 |
+
# FUNCTIONS:
|
18 |
+
|
19 |
+
def get_popular_recommendations(n, genres):
|
20 |
+
return (
|
21 |
+
ratings
|
22 |
+
.groupby('movieId')
|
23 |
+
.agg(avg_rating = ('rating', 'mean'), num_ratings = ('rating', 'count'))
|
24 |
+
.merge(movies, on='movieId')
|
25 |
+
.assign(combined_rating = lambda x: x['avg_rating'] * x['num_ratings']**0.5)
|
26 |
+
[lambda df: df["genres"].str.contains(genres, regex=True)]
|
27 |
+
.sort_values('combined_rating', ascending=False)
|
28 |
+
.head(n)
|
29 |
+
[['title', 'avg_rating', 'genres']]
|
30 |
+
)
|
31 |
+
|
32 |
+
def transform_genre_to_regex(genres):
|
33 |
+
regex = ""
|
34 |
+
for genre in genres:
|
35 |
+
regex += f"(?=.*{genre})"
|
36 |
+
return regex
|
37 |
+
|
38 |
+
# USER INPUT:
|
39 |
+
number_of_recommendations = st.slider("Number of recommendations", 1, 10, 5)
|
40 |
+
|
41 |
+
genre_list = set([inner for outer in movies.genres.str.split('|') for inner in outer])
|
42 |
+
genres = st.multiselect('Optional: Select one or more genres', genre_list, default=None, key=None, help=None, on_change=None, args=None, kwargs=None, disabled=False)
|
43 |
+
genres_regex = transform_genre_to_regex(genres)
|
44 |
+
|
45 |
+
|
46 |
+
if st.button("Recommend!"):
|
47 |
|
48 |
+
st.write(get_popular_recommendations(number_of_recommendations, genres_regex))
|
pages/2 - User based recommender.py
DELETED
File without changes
|
pages/3 - Old recommender.py
DELETED
@@ -1,43 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
|
4 |
-
|
5 |
-
st.title("Movie Recommender")
|
6 |
-
|
7 |
-
st.write("""
|
8 |
-
### Instructions
|
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.\n\n
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|