ayoni02 commited on
Commit
eb88a98
·
1 Parent(s): 4b0ef19

Added docstrigs

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -6,19 +6,17 @@ from surprise import Dataset, Reader
6
  from surprise import KNNBasic
7
 
8
  def opendata(a, nrows):
 
 
 
9
  df = pd.read_csv(a, nrows=nrows, sep=';', encoding='ISO-8859-1')
10
  return df
11
 
12
- def split(df):
13
- n=len(df)
14
- N=list(range(n))
15
- random.seed(2023)
16
- random.shuffle(N)
17
- train=df.iloc[N[0:(n*4)//5]]
18
- test=df.iloc[N[(n*4)//5:]]
19
- return train, test
20
-
21
  def red(df):
 
 
 
 
22
  reader = Reader(rating_scale=(1,10)) # rating scale range
23
  trainset = Dataset.load_from_df(df[['User-ID','ISBN','Book-Rating']],reader).build_full_trainset()
24
  items = trainset.build_anti_testset()
@@ -26,6 +24,12 @@ def red(df):
26
 
27
 
28
  def mod(df, user, items):
 
 
 
 
 
 
29
  algo = KNNBasic()
30
  algo.fit(df)
31
  user_items = list(filter(lambda x: x[0] == user, items))
@@ -34,12 +38,18 @@ def mod(df, user, items):
34
  return recommendations
35
 
36
  def gl(num):
 
 
 
 
 
 
37
  data = opendata('BX-Book-Ratings.csv', nrows=20_000)
38
  books = opendata('BX_Books.csv', nrows=None)
39
  mapping_dict = books.set_index("ISBN")["Book-Title"].to_dict()
40
- train, test = split(data)
41
- users=test['User-ID'].tolist()
42
- trainset, items = red(train)
43
  user = users[int(num)]
44
  recommendations = mod(trainset, user, items)
45
  op = []
 
6
  from surprise import KNNBasic
7
 
8
  def opendata(a, nrows):
9
+ """
10
+ opens the data and returns a dataframe
11
+ """
12
  df = pd.read_csv(a, nrows=nrows, sep=';', encoding='ISO-8859-1')
13
  return df
14
 
 
 
 
 
 
 
 
 
 
15
  def red(df):
16
+ """
17
+ Builds the trainset and the anti testset,
18
+ Returns the trainset and the anti testset as items
19
+ """
20
  reader = Reader(rating_scale=(1,10)) # rating scale range
21
  trainset = Dataset.load_from_df(df[['User-ID','ISBN','Book-Rating']],reader).build_full_trainset()
22
  items = trainset.build_anti_testset()
 
24
 
25
 
26
  def mod(df, user, items):
27
+ """
28
+ Builds the model using KNNBasic,
29
+ Fits the model to the trainset,
30
+ Takes the given user and creates a recommendation for them based on the model
31
+ returns the recommendations
32
+ """
33
  algo = KNNBasic()
34
  algo.fit(df)
35
  user_items = list(filter(lambda x: x[0] == user, items))
 
38
  return recommendations
39
 
40
  def gl(num):
41
+ """
42
+ Opens the 2 datasets
43
+ Creates a mapping dictionary for the ISBN and Book-Title,
44
+ Creates a list of users and accepts a user input,
45
+ Builds the model and returns the top 5 recommendations for the user
46
+ """
47
  data = opendata('BX-Book-Ratings.csv', nrows=20_000)
48
  books = opendata('BX_Books.csv', nrows=None)
49
  mapping_dict = books.set_index("ISBN")["Book-Title"].to_dict()
50
+
51
+ users=data['User-ID'].tolist()
52
+ trainset, items = red(data)
53
  user = users[int(num)]
54
  recommendations = mod(trainset, user, items)
55
  op = []