-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
113 lines (77 loc) · 3.14 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import BRS
import argparse
import sys
import pandas as pd
def parse_arguments():
parser = argparse.ArgumentParser(description='SVD or KNN collaborative filtering')
parser.add_argument(
"--SVD",
action="store_true",
help="User based collaborative filtering using SVD"
)
parser.add_argument(
"--KNN",
action="store_true",
help="Item collaborative filtering using KNN"
)
return parser.parse_args()
def YN():
reply = str(input('\n\nContinue (y/n):\t')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
else:
return False
def main():
args = parse_arguments()
cont = True
if not args.SVD and not args.KNN:
print("\n\nChoose SVD or KNN for testing user/item based collaborative filtering\n")
Top_B = BRS.Books()
BRS.Books().diagram()
High_Mean_Rating, High_Rating_Count = Top_B.Top_Books()
pd.set_option('display.max_colwidth', -1)
print("\n\nBooks with high ratings :\n")
print(High_Mean_Rating[['bookTitle', 'MeanRating', 'ratingCount', 'bookAuthor']])
print("\n\nBooks with high rating count :\n")
print(High_Rating_Count[['bookTitle', 'MeanRating', 'ratingCount', 'bookAuthor']])
sys.exit()
if args.SVD:
UCF = BRS.SVD()
UCF.scipy_SVD()
BRS.Books().diagram()
while cont:
try:
User_ID = int(input('Enter User ID in the range {0}-{1}: '.format(1, len(UCF.explicit_users))))
except:
print('Enter a number')
sys.exit()
if User_ID in range(1, len(UCF.explicit_users)):
pass
else:
print("Choose between {0}-{1}".format(1, len(UCF.explicit_users)))
sys.exit()
Rated_Books, SVD_Recommended_Books = UCF.Recommend_Books(userID=User_ID)
pd.set_option('display.max_colwidth', -1)
#
# print("\nThe Books already rated by the user\n")
# print(Rated_Books[['bookTitle', 'bookRating']])
print("\nRecommended Books for the user\n")
SVD_Recommended_Books = SVD_Recommended_Books.merge(UCF.average_rating, how='left', on='ISBN')
SVD_Recommended_Books = SVD_Recommended_Books.rename(columns={'bookRating': 'MeanRating'})
print(SVD_Recommended_Books[['bookTitle', 'MeanRating', 'bookAuthor']])
cont = YN()
if args.KNN:
ICF = BRS.KNN()
BRS.Books().diagram()
while cont:
book_name = input('\n\nEnter the Book Title:\t')
_, KNN_Recommended_Books, _ = ICF.Recommend_Books(book_name)
print('Recommendations for the book --> {0}:\n'.format(book_name))
KNN_Recommended_Books = KNN_Recommended_Books.merge(ICF.average_rating, how='left', on='ISBN')
KNN_Recommended_Books = KNN_Recommended_Books.rename(columns={'bookRating': 'MeanRating'})
print(KNN_Recommended_Books[['bookTitle', 'MeanRating', 'bookAuthor']])
cont = YN()
if __name__ == '__main__':
main()