-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
67 lines (60 loc) · 2.82 KB
/
app.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
import streamlit as st
import json
from Classifier import KNearestNeighbours
from operator import itemgetter
# Load data and movies list from corresponding JSON files
with open(r'data.json', 'r+', encoding='utf-8') as f:
data = json.load(f)
with open(r'titles.json', 'r+', encoding='utf-8') as f:
movie_titles = json.load(f)
def knn(test_point, k):
# Create dummy target variable for the KNN Classifier
target = [0 for item in movie_titles]
# Instantiate object for the Classifier
model = KNearestNeighbours(data, target, test_point, k=k)
# Run the algorithm
model.fit()
# Distances to most distant movie
max_dist = sorted(model.distances, key=itemgetter(0))[-1]
# Print list of 10 recommendations < Change value of k for a different number >
table = list()
for i in model.indices:
# Returns back movie title and imdb link
table.append([movie_titles[i][0], movie_titles[i][2]])
return table
if __name__ == '__main__':
genres = ['Action', 'Adventure', 'Animation', 'Biography', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Family',
'Fantasy', 'Film-Noir', 'Game-Show', 'History', 'Horror', 'Music', 'Musical', 'Mystery', 'News',
'Reality-TV', 'Romance', 'Sci-Fi', 'Short', 'Sport', 'Thriller', 'War', 'Western']
movies = [title[0] for title in movie_titles]
st.header('Movie Recommendation System')
apps = ['--Select--', 'Movie based', 'Genres based']
app_options = st.selectbox('Select application:', apps)
if app_options == 'Movie based':
movie_select = st.selectbox('Select movie:', ['--Select--'] + movies)
if movie_select == '--Select--':
st.write('Select a movie')
else:
n = st.number_input('Number of movies:', min_value=5, max_value=20, step=1)
genres = data[movies.index(movie_select)]
test_point = genres
table = knn(test_point, n)
for movie, link in table:
# Displays movie title with link to imdb
st.markdown(f"[{movie}]({link})")
elif app_options == apps[2]:
options = st.multiselect('Select genres:', genres)
if options:
imdb_score = st.slider('IMDb score:', 1, 10, 8)
n = st.number_input('Number of movies:', min_value=5, max_value=20, step=1)
test_point = [1 if genre in options else 0 for genre in genres]
test_point.append(imdb_score)
table = knn(test_point, n)
for movie, link in table:
# Displays movie title with link to imdb
st.markdown(f"[{movie}]({link})")
else:
st.write("This is a simple Movie Recommender application. "
"You can select the genres and change the IMDb score.")
else:
st.write('Select option')