-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (57 loc) · 2.41 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
68
69
70
71
#!/usr/bin/python
# -*- coding: utf-8 -*-
import streamlit as st
import pandas as pd
from sklearn import neighbors
from sklearn.preprocessing import MinMaxScaler
def main():
df = pd.read_csv('final.csv')
dforigin = pd.read_csv('dfbeforeprocess.csv')
df.set_index('title', inplace=True)
model = neighbors.NearestNeighbors(n_neighbors=5,
algorithm='ball_tree', metric='euclidean')
model.fit(df)
st.title('Book Recommendation App')
sidebar = st.sidebar.selectbox('Select Recommender Type',
['Publisher', 'Author', 'Language'])
def rec_book_publisher(publisher_name):
publisher_books = dforigin[dforigin['publisher']
== publisher_name][['title',
'average_rating']]
publisher_books = \
publisher_books.sort_values(by='average_rating',
ascending=False)
return publisher_books.head(10)
# Recommendation function based on Author
def rec_book_author(author_name):
author_books = dforigin[dforigin['authors']
== author_name][['title',
'average_rating']]
author_books = author_books.sort_values(by='average_rating',
ascending=False)
return author_books.head(10)
# Recommendation function based on Language
def rec_book_lang(lang):
lang_books = dforigin[dforigin['language_code']
== lang][['title', 'average_rating']]
lang_books = lang_books.sort_values(by='average_rating',
ascending=False)
return lang_books.head(10)
if sidebar == 'Publisher':
publisher_name = st.selectbox('Select Publisher',
list(dforigin['publisher'].value_counts().index))
rec_books = rec_book_publisher(publisher_name)
elif sidebar == 'Author':
author_name = st.selectbox('Select Author',
list(dforigin['authors'
].value_counts().index))
rec_books = rec_book_author(author_name)
elif sidebar == 'Language':
lang = st.selectbox('Select Language',
list(dforigin['language_code'
].value_counts().index))
rec_books = rec_book_lang(lang)
st.subheader('Recommended Books')
st.write(rec_books)
if __name__ == '__main__':
main()