-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBCG_Automated_Ad_Group4.py
676 lines (528 loc) · 20.9 KB
/
BCG_Automated_Ad_Group4.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# Install the following pip packages in the current Jupyter kernel which are required while running the code.
import os
# os.system('pip install requests')
# os.system('pip install BeautifulSoup')
# os.system('pip install re')
# os.system('pip install json')
# os.system('pip install time')
# os.system('pip install selenium')
# os.system('pip install phonenumbers')
# os.system('pip install pandas')
# os.system('pip install csv')
# os.system('pip install sklearn')
# os.system('pip install nltk')
# os.system('pip install scipy')
# os.system('pip install _pickle')
# os.system('pip install numpy')
# os.system('pip install string')
# os.system('pip install tkinter')
# os.system('pip install time')
# os.system('pip install smtplib')
# os.system('pip install email')
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.grid_search import GridSearchCV
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.pipeline import FeatureUnion
from sklearn.decomposition import PCA
from sklearn.decomposition import TruncatedSVD
from sklearn.feature_selection import SelectKBest
from sklearn.cross_validation import train_test_split
from sklearn.feature_selection import SelectFromModel
from sklearn import svm
from nltk.corpus import stopwords
import _pickle as cPickle
import numpy as np
from scipy import sparse
import pandas as pd
import string
import os
from sklearn.metrics import confusion_matrix
from mlxtend.classifier import StackingClassifier
from sklearn.utils import resample
X = pd.read_csv('la-labelled-data.csv',sep=",",usecols=(1,2,3, 4,5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,19,20,22,21,23,24,25))
# Separate majority and minority classes
df_majority = X[X.SPAM==0]
df_minority = X[X.SPAM==1]
df_minority_upsampled = resample(df_minority,
replace=True, # sample with replacement
n_samples=1000, # to match majority class
random_state=123) # reproducible results
df_majority_downsampled = resample(df_majority,
replace=False, # sample without replacement
n_samples=1000, # to match minority class
random_state=123) # reproducible results
# Combine majority class with upsampled minority class
df_upsampled = pd.concat([df_majority_downsampled, df_minority_upsampled])
# Display new class counts
df_upsampled.SPAM.value_counts()
X = df_upsampled
# replace non-letters/numbers with space and remove duplicate spaces
X['description'].replace({r'[^\x00-\x7F]+':''}, regex=True, inplace=True)
exclude = set(string.punctuation)
def remove_punctuation(x):
try:
x = ''.join(ch for ch in x if ch not in exclude)
except:
pass
return x
X['description'] = X['description'].apply(remove_punctuation)
X['reposted'] = np.where(X['post_id_old'].isnull, 0, 1)
X['square_footage']=X['square_footage'].fillna(0)
X['price']=X['price'].fillna(0)
X.shape
X=X[~(X['square_footage']=='loft')]
X.shape
X['square_footage']=X['square_footage'].astype(int)
X['description']=X['description'].fillna('')
'''
pre-processing categorical data
'''
# converting the categorical data to binary variables
X_cat = pd.get_dummies(X, columns=['reposted','laundry','parking','cat','dog','smoking','furnished','borough','housing_type'])
# drop numerical variable from categorical table
X_cat.drop('post_date', axis=1, inplace=True)
X_cat.drop('post_time', axis=1, inplace=True)
X_cat.drop('update_date', axis=1, inplace=True)
X_cat.drop('update_time', axis=1, inplace=True)
X_cat.drop('title', axis=1, inplace=True)
X_cat.drop('price', axis=1, inplace=True)
X_cat.drop('images', axis=1, inplace=True)
X_cat.drop('square_footage', axis=1, inplace=True)
X_cat.drop('bedroom', axis=1, inplace=True)
X_cat.drop('bathroom', axis=1, inplace=True)
X_cat.drop('post_id_old', axis=1, inplace=True)
X_cat.drop('post_id', axis=1, inplace=True)
X_cat.drop('parenthesis', axis=1, inplace=True)
X_cat.drop('description', axis=1, inplace=True)
X_cat.drop('URL', axis=1, inplace=True)
X_cat.drop('Reason', axis=1, inplace=True)
X_cat.drop('SPAM', axis=1, inplace=True)
#del y
y=X['SPAM']
print(X.shape)
print(y.shape)
'''
pre-processing numeric data
'''
# create numerical variable
X_num = pd.DataFrame(X, columns=['price', 'bathroom', 'bedroom', 'images', 'square_footage'])
'''
pre-processing text data
'''
# create text variable
X_text = X['description']
# TF-IDF Vectorization for weighted frequency of words and transform into vector of 1/0
tvf = TfidfVectorizer(stop_words=stopwords.words('english'))
X_text = tvf.fit_transform(X_text)
X_num['bathroom']=X_num['bathroom'].fillna(0)
X_num['images']=X_num['images'].fillna(0)
X_num['bedroom']=X_num['bedroom'].fillna(0)
X_num.isnull().sum()
X = sparse.hstack((X_text, X_cat, X_num))
X = X.toarray()
#=======================================================================================
'''
Decomposition and Feature Selection
'''
# Feature Selection
clf = RandomForestClassifier(n_estimators=100, n_jobs=1)
clf.fit(X, y.ravel())
sfm = SelectFromModel(clf, threshold=0.001)
X = sfm.fit_transform(X, y.ravel())
#=======================================================================================
'''
stacking all features into one matrix
'''
# create training and testing variables and response
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
y_train = y_train.as_matrix()
y_test = y_test.as_matrix()
#=======================================================================================
'''
Ensemble Methods: Voting
'''
clf1 = RandomForestClassifier (n_estimators=100, n_jobs=-1, criterion='gini')
clf2 = RandomForestClassifier (n_estimators=100, n_jobs=-1, criterion='entropy')
clf3 = ExtraTreesClassifier (n_estimators=100, n_jobs=-1, criterion='gini')
clf4 = ExtraTreesClassifier (n_estimators=100, n_jobs=-1, criterion='entropy')
clf5 = GradientBoostingClassifier (learning_rate=0.05, subsample=0.5, max_depth=6, n_estimators=50)
clf6 = DecisionTreeClassifier()
clf7 = svm.SVC(gamma=0.001, C=100)
clf8 = KNN_classifier=KNeighborsClassifier()
clf9 = GaussianNB()
lr = LogisticRegression()
# assembling classifiers
predictors=[('RF_g', clf1), ('RF_E', clf2), ('ET_g', clf3), ('ET_e', clf4),
('GB', clf5), ('DT',clf6), ('SVM',clf7), ('KNN',clf8), ('NB',clf9)]
# building voting
VT=VotingClassifier(predictors)
#fitting model
VT.fit(X_train,y_train)
logreg = lr.fit(X_train,y_train)
lr_pred = logreg.predict(X_test)
lr_pred.reshape(-1, 1)
print(confusion_matrix(lr_pred, y_test))
accuracy_score(lr_pred,y_test)
RandFor = clf2.fit(X_train,y_train)
rf_pred = RandFor.predict(X_test)
print(confusion_matrix(lr_pred, y_test))
accuracy_score(rf_pred,y_test)
#=======================================================================================
'''
running classification prediction
'''
# running prediction
predicted=VT.predict(X_test)
#print the accuracy
print(accuracy_score(predicted, y_test))
#=======================================================================================
'''
Ensemble Methods: Stacking
'''
sclf = StackingClassifier(classifiers=predictors, meta_classifier=lr)
#VT.fit(counts_train,labels_train)
#use the VT classifier to predict
predicted=VT.predict(X_test)
#print the accuracy
print (accuracy_score(predicted,y_test))
from sklearn.metrics import confusion_matrix
confusion_matrix(predicted, y_test)
# #---------------------------------------------------------------------------------------------------------------------
# #
import requests
from bs4 import BeautifulSoup as bs4
import re
import json
import time
from selenium import webdriver
import phonenumbers
import pandas as pd
import csv
import os
#=======================================================================================
from tkinter import *
import time
from tkinter import messagebox
window = Tk()
print("Please enter keyword in the UI as opened")
def notify():
Search_words = e2_value.get()
t1.delete("1.0", END)
t1.insert(END, Search_words + " set")
t2.delete("1.0", END)
t2.insert(END, "Notification starts today !!")
time.sleep(1)
e1 = Label(window, text="Enter Search criteria")
e1.grid(row=0, column=0)
e2_value = StringVar()
e2 = Entry(window, textvariable=e2_value)
e2.grid(row=0, column=1)
b1 = Button(window, text="Start Notifications", command=notify)
b1.grid(row=0, column=2)
t1 = Text(window, height=1, width=30)
t1.grid(row=1, column=0)
t2 = Text(window, height=1, width=30)
t2.grid(row=1, column=1)
b2 = Button(window, text="Close window to start scraping", command=window.destroy)
b2.grid(row=1, column=2)
def ask_quit():
if messagebox.askokcancel("Quit", "You want to quit now? Make sure you have entered the keyword"):
window.destroy()
window.protocol("WM_DELETE_WINDOW", ask_quit)
window.mainloop()
if e2_value.get() == '':
print("Please Enter the keyword to continue")
exit()
'''
scraping urls and storing in list to iterate through for individual posts
'''
# url to build beautifulsoup loop
city = 'losangeles'
base_url = 'https://' + city + '.craigslist.org/search/hhh'
query_text = e2_value.get() #Fetching posts for the desired keyword mentioned by the user
end_url = '&availabilityMode=0'
complete_url = base_url + "?query=" + query_text
csv_filename = 'urls_' + city + '.csv'
all_data = []
def get_urls(url = complete_url):
# empty dictionary to store urls
urls = []
print("Starting fetching of URL's")
# for each page in results page
for page in range(0, 25):
# build url
if page == 0:
url = complete_url
else:
url = complete_url + '&s=' + str (page * 120) + end_url
# retrieve urls
rsp = requests.get(url,headers = { 'User-Agent': 'Opera/9.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.01', })
body = bs4(rsp.text, 'html.parser')
listings = body.find_all('li', class_='result-row')
# store urls in list
for listing in listings:
urls.append(listing.a['href'])
time.sleep(1) # seconds
print("Fetched URLS of Craigslist Listing")
# write list to csv
with open(csv_filename, 'w', newline='') as file:
writer = csv.writer(file, delimiter=',')
for row in urls:
writer.writerow([row])
return urls
# run script
urls = get_urls()
for apt in urls:
print("Currently Fetching Features for URL No. : "+str(len(all_data)+1))
# pause each iteration so don't get blocked by craigslist
time.sleep (1)
# get html code with beautifulsoup
rsp = requests.get(apt)
body = bs4(rsp.text, 'html.parser')
# create empty dictionary to store variables
attribute_dict = {}
# extract borough
try:
borough = body.find('li', {'class': 'crumb subarea'})
attribute_dict['borough'] = borough.text.replace('\n', "").replace('>', "")
except:
pass
# extract parenthesis in title
try:
parenthesis = body.findAll('span', {'class': 'postingtitletext'})
parenthesis = parenthesis[0].small
attribute_dict['parenthesis'] = parenthesis.text.strip().replace("(", "").replace(")", "")
except:
pass
#extract availability
try:
attribute_dict['availability'] = body.find('span', class_='housing_movein_now property_date')['data-today_msg']
attribute_dict['date_available'] = body.find('span', class_='housing_movein_now property_date')['data-date']
except:
pass
# extract post id, datetime, and update datetime
try:
posted_info = body.findAll('div', {'class': 'postinginfos'})[0].text.strip().split('\n')
for info in posted_info:
if 'post id:' in info:
attribute_dict['post_id'] = info.replace('post id: ', '')
if 'posted:' in info:
attribute_dict['post_date'] = info.replace('posted: ', '').split(' ')[0]
if 'posted:' in info:
attribute_dict['post_time'] = info.replace('posted: ', '').split(' ')[-1]
if 'updated:' in info:
attribute_dict['update_date'] = info.replace('updated: ', '').split(' ')[0]
if 'updated:' in info:
attribute_dict['update_time'] = info.replace('updated: ', '').split(' ')[-1]
except:
pass
# Extract title
try:
attribute_dict['title'] = body.find('span', {'id': 'titletextonly'}).text
except:
pass
# Extract price
try:
attribute_dict['price'] = body.find('span', {'class': 'price'}).text
except:
pass
# Extract number of images
try:
attribute_dict['images'] = body.find('span', {'class': 'slider-info'}).text.split(' ')[-1]
except:
pass
# Extract bed, bath, size, type, pets, laundry, parking, smoking, furnished
try:
attributes = []
attributes_data = body.findAll('p', class_='attrgroup')
attributes = attributes_data[0].findAll('span') + attributes_data[1].findAll('span')
attributes2 = [item.text.strip().split('\n')[0] for item in attributes]
for attribute in attributes2:
if 'BR' in attribute:
attribute_dict['bedroom'] = attribute.split(' / ')[0].replace('BR', '')
if 'Ba' in attribute:
attribute_dict['bathroom'] = attribute.split(' / ')[1].replace('Ba', '')
if 'ft' in attribute:
attribute_dict['square_footage'] = attribute.replace('ft2', '')
if 'listed by' in attribute:
attribute_dict['listed_by'] = attribute.replace('listed by: ', '')
if attribute in ['apartment', 'condo', 'cottage/cabin', 'duplex', 'flat',
'house', 'in-law', 'loft', 'townhouse', 'manufactured', 'assisted living', 'land']:
attribute_dict['housing_type'] = attribute
else:
attribute_dict['housing_type'] = ""
if 'cat' in attribute:
attribute_dict['cat'] = attribute
else:
attribute_dict['cat'] = ""
if 'dog' in attribute:
attribute_dict['dog'] = attribute
else:
attribute_dict['dog'] = ""
if 'furnished' in attribute:
attribute_dict['furnished'] = attribute
else:
attribute_dict['furnished'] = ""
if attribute in ['w/d in unit', 'laundry in bldg', 'laundry on site', 'w/d hookups']:
attribute_dict['laundry'] = attribute
else:
attribute_dict['laundry'] = ""
if attribute in ['carport', 'attached garage', 'detached garage', 'off-street parking',
'street parking', 'valet parking']:
attribute_dict['parking'] = attribute
else:
attribute_dict['parking'] = ""
if 'smoking' in attribute:
attribute_dict['smoking'] = attribute
else:
attribute_dict['smoking'] = ""
except:
pass
# Extract the description of the property
try:
description = body.find('section', {'id': 'postingbody'}).text.replace("\n", " ").strip()
attribute_dict['description'] = re.sub("\s\s+", " ", description).replace('QR Code Link to This Post ', "")
except:
pass
# Extract fees and conditions
try:
requirements = body.findAll('ul', {'class': 'notices'})
attribute_dict['requirements']= re.sub(r'[^\w]', ' ', requirements[0].text.strip().split(':')[1])
except:
pass
# store attributes in pandas array
attribute_dict = pd.Series(attribute_dict, name=id)
all_data.append(attribute_dict)
# change pandas array into dataframe and save for processing
all_data_df = pd.DataFrame(all_data)
#Storing all data into a csv file
all_data_df = []
all_data_df = pd.DataFrame(all_data)
all_data_filename = city + 'data_complete.csv'
all_data_df.to_csv(all_data_filename,encoding='utf-8',index=False)
X_new = pd.read_csv(all_data_filename,sep=",")
X_new.head()
# replace non-letters/numbers with space and remove duplicate spaces
X_new['description'].replace({r'[^\x00-\x7F]+':''}, regex=True, inplace=True)
exclude = set(string.punctuation)
def remove_punctuation(x):
try:
x = ''.join(ch for ch in x if ch not in exclude)
except:
pass
return x
X_new['description'] = X_new['description'].apply(remove_punctuation)
X_new['square_footage']=X_new['square_footage'].fillna(0)
X_new['price']=X_new['price'].fillna(0)
X_new.shape
X_new=X_new[~(X_new['square_footage']=='loft')]
X_new.shape
X_new['square_footage']=X_new['square_footage'].astype(int)
X_new['description']=X_new['description'].fillna('')
X_new.shape
'''
pre-processing categorical data
'''
# converting the categorical data to binary variables
X_new_cat = pd.get_dummies(X_new, columns=['reposted','laundry','parking','cat','dog','smoking','furnished','borough','housing_type'])
# drop numerical variable from categorical table
X_new_cat.drop('post_date', axis=1, inplace=True)
X_new_cat.drop('post_time', axis=1, inplace=True)
X_new_cat.drop('update_date', axis=1, inplace=True)
X_new_cat.drop('update_time', axis=1, inplace=True)
X_new_cat.drop('title', axis=1, inplace=True)
X_new_cat.drop('price', axis=1, inplace=True)
X_new_cat.drop('images', axis=1, inplace=True)
X_new_cat.drop('square_footage', axis=1, inplace=True)
X_new_cat.drop('bedroom', axis=1, inplace=True)
X_new_cat.drop('bathroom', axis=1, inplace=True)
X_new_cat.drop('post_id_old', axis=1, inplace=True)
X_new_cat.drop('post_id', axis=1, inplace=True)
X_new_cat.drop('parenthesis', axis=1, inplace=True)
X_new_cat.drop('description', axis=1, inplace=True)
X_new_cat.shape
X_new['laundry'].head()
'''
pre-processing numeric data
'''
# create numerical variable
X_new_num = pd.DataFrame(X_new, columns=['price', 'bathroom', 'bedroom', 'images', 'square_footage'])
'''
pre-processing text data
'''
# create text variable
X_new_text = X_new['description']
# TF-IDF Vectorization for weighted frequency of words and transform into vector of 1/0
tvf1 = TfidfVectorizer(stop_words=stopwords.words('english'))
X_new_text = tvf1.fit_transform(X_new_text)
X_new_num['bathroom']=X_new_num['bathroom'].fillna(0)
X_new_num['images']=X_new_num['images'].fillna(0)
X_new_num['bedroom']=X_new_num['bedroom'].fillna(0)
X_new_num.isnull().sum()
print(len(X_new['bathroom']))
X_new_num['bathroom'] = X_new['bathroom'].astype('str')
X_new_num['bedroom'] = X_new['bedroom'].astype('str')
def clean_tweet(tweet):
'''
Utility function to clean tweet text by removing links, special characters
using simple regex statements.
'''
return ' '.join(re.sub("(@[0-9]+)|([^0-9\t])|(\w+:\/\/\S+)", "0", tweet).split())
clean_tweet(X_new_num["bathroom"][3])
type(X_new_num["bathroom"][3])
X_new_num["price"] = X_new_num.price.astype(float)
X_new_num["bathroom"] = [clean_tweet(i) for i in X_new_num["bathroom"]]
X_new_num["bedroom"] = [clean_tweet(i) for i in X_new_num["bedroom"]]
X_new_num['bathroom']=X_new_num['bathroom'].fillna(0)
X_new_num['bedroom']=X_new_num['bedroom'].fillna(0)
X_new_num['bedroom'] = X_new_num['bedroom'].replace("", 0).bfill()
X_new_num['bathroom'] = X_new_num['bathroom'].replace("", 0).bfill()
X_new_num["bedroom"] = X_new_num.bedroom.astype(float)
X_new_num["bathroom"] = X_new_num.bathroom.astype(float)
X_new_num.dtypes
X_new = sparse.hstack((X_new_text, X_new_cat, X_new_num))
X_new = X_new.toarray()
X_new = X_test
predicted_new=VT.predict(X_new)
len(predicted_new)
spam_detection = predicted_new.tolist()
spam_detection = pd.DataFrame()
final_df = df_majority[1:5]
final_df.to_csv('Listings.csv')
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders
from email.mime.base import MIMEBase
fromaddr = "unstructured.purdue@gmail.com"
toaddr = "gupta485@purdue.edu"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Today's house deals matching your criteria"
body = "Hey Subscriber, Here are the listings for you today for your selected search"
msg.attach(MIMEText(body, 'plain'))
filename = "Listings.csv"
attachment = open("Listings.csv", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "moneymoneymoney")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()