-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptive.py
255 lines (112 loc) · 4.21 KB
/
adaptive.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# coding: utf-8
# In[164]:
import pandas as pd
import sklearn
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tokenize import RegexpTokenizer
import string
from gensim import corpora
from gensim.models import LsiModel
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from gensim.models.coherencemodel import CoherenceModel
from sklearn.metrics.pairwise import linear_kernel
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# In[165]:
book_data = pd.read_csv("books.csv")
user_data = pd.read_csv("users.csv")
# In[168]:
book_data.keys()
# In[169]:
book_data = book_data.drop(['publication_day','isbn','isbn13','publication_month','publication_day','is_ebook','work','average_rating','num_pages','format','edition_information','ratings_count','book_links','series_works','similar_books'],axis = 1)
# In[171]:
book_data = book_data.drop(['asin','kindle_asin','marketplace_id','country_code','text_reviews_count','public_document'], axis = 1)
# In[172]:
book_data.keys()
# In[173]:
book_data = book_data.drop(['_id','id'], axis = 1)
# In[177]:
book_data.head()
# In[178]:
for i in range (book_data.shape[0]):
book_data.iloc[i,6] = re.sub(r"[^a-zA-Z]+", ' ', book_data.iloc[i,6])
# In[179]:
word = ['name','count','shelf']
for i in range(book_data.shape[0]):
word_list = book_data.iloc[i,6].split();
book_data.iloc[i,6] = ' '.join([i for i in word_list if i not in word])
# In[180]:
for i in range (book_data.shape[0]):
book_data.iloc[i,5] = re.sub(r"[^a-zA-Z]+", ' ', book_data.iloc[i,5])
# In[181]:
word = ['name','count','shelf']
for i in range(book_data.shape[0]):
word_list = book_data.iloc[i,6].split();
book_data.iloc[i,6] = ' '.join([i for i in word_list if i not in word])
# In[184]:
def pre_process(x):
if isinstance(x, list):
return [str.lower(i.replace(" ", " ")) for i in x]
else:
if isinstance(x, str):
return str.lower(x.replace(" ", " "))
else:
return ''
# In[185]:
columns = ['description','authors','publisher','popular_shelves']
for f in columns:
book_data[f] = book_data[f].apply(pre_process)
# In[186]:
def combine(x):
return (x['description']) + ' ' + (x['authors']) + ' ' + (x['publisher']) + ' ' + (x['popular_shelves'])
# In[188]:
book_data['train'] = book_data.apply(combine,axis = 1)
# In[190]:
word_stopped = TfidfVectorizer(stop_words='english')
book_data['train'] = book_data['train'].fillna('')
matrix = word_stopped.fit_transform(book_data['train'])
# In[192]:
co_sim = linear_kernel(matrix,matrix)
# In[194]:
book_data = book_data.reset_index()
# In[195]:
indexing = pd.Series(book_data.index, index=book_data['title']).drop_duplicates()
# In[ ]:
indexing = pd.Series(book_data.index, index=book_data['title','publisher']).drop_duplicates()
# In[223]:
resultss = []
# In[228]:
def get_recommendations(title,cosine_sim1 = co_sim):
index = indexing[title]
similarity = list(enumerate(cosine_sim1[index]))
similarity = sorted(similarity,key=lambda i: i[1], reverse = True)
similarity = similarity[1:20]
movie_index = [j[0] for j in similarity ]
resultss = book_data['title'].iloc[movie_index]
return resultss
# In[229]:
book_data['title'][323]
# In[230]:
get_recommendations('The Door Into Summer')
# In[126]:
get_recommendations("Darwin's Dangerous Idea: Evolution and the Meanings of Life")
# In[162]:
get_recommendations("Darwin's Dangerous Idea: Evolution and the Meanings of Life")
# In[200]:
get_recommendations("Darwin's Dangerous Idea: Evolution and the Meanings of Life")
# In[203]:
get_recommendations('Storage Area Network Essentials: A Complete Guide to Understanding and Implementing Sans')
# In[208]:
get_recommendations('Letterhead and Logo Design 9')
# In[213]:
get_recommendations('What to Expect the First Year (What to Expect)')
# In[217]:
get_recommendations('The Fellowship of the Ring (The Lord of the Rings, #1)')
# In[227]:
get_recommendations('eBay for Dummies')
# In[ ]: