-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader_dummy_middle.py
628 lines (538 loc) · 18.2 KB
/
loader_dummy_middle.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
# Loader Dummy Features Middle
# This loader will get every restaurant in the dataset
# For every feature that a restaurant does not have, a dummy variable will be provided
# The dummy feature will simply hold the middle value of the feature
# This loader will dummy every single restaurant feature that is not present
# Currently operates on 67 features
# Standard libraries
import random
import math
# Third-party libraries
from pymongo import MongoClient
import numpy as np
def load_data():
print("Initializing loader for middle-value dummy variables")
print("Connecting to database...")
# Connect to mongo
client = MongoClient('ds049181.mongolab.com', 49181)
db = client.new_yelp_data
db.authenticate("naho", "naho")
businesses = db.businesses
# Query for every single restaurant
restaurants = businesses.find({"categories": "Restaurants"});
data_matrix = []
training_data = []
test_data = []
i = 0
print("Found " + str(restaurants.count()) + " Restaurants")
for restaurant in restaurants:
#### Dietary Restriction Attributes #########
(dietary_restrictions_dairy_free,
dietary_restrictions_gluten_free,
dietary_restrictions_halal,
dietary_restrictions_kosher,
dietary_restrictions_soy_free,
dietary_restrictions_vegan,
dietary_restrictions_vegetarian,
) = get_dietary_restrictions(restaurant["attributes"])
#### Parking Attributes #########
(parking_valet,
parking_garage,
parking_street,
parking_lot,
parking_validated) = get_parking_fields(restaurant["attributes"])
#### MealTime, Good For *, Variables #####################
(good_for_dessert,
good_for_lunch,
good_for_latenight,
good_for_dinner,
good_for_breakfast,
good_for_brunch) = get_good_for_fields(restaurant["attributes"])
#### Ambience Variables #####################
(ambience_romantic,
ambience_intimate,
ambience_touristy,
ambience_hipster,
ambience_divey,
ambience_classy,
ambience_trendy,
ambience_upscale,
ambience_casual,
) = get_ambience_fields(restaurant["attributes"])
#### Music Variables ###############
(dj,
jukebox,
live,
video,
karaoke,
background_music,
playlist,
) = get_music_fields(restaurant["attributes"])
#### Payment Variables ##############
(payment_type_amex,
payment_type_cash_only,
payment_type_discover,
payment_type_mastercard,
payment_type_visa,
) = get_payment_type_fields(restaurant["attributes"])
##### Top-Level Boolean Variables #######################
caters = get_boolean_feature(restaurant["attributes"], "Caters")
open_24_hours = get_boolean_feature(restaurant["attributes"], "Open 24 Hours")
corkage = get_boolean_feature(restaurant["attributes"], "Corkage")
order_at_counter = get_boolean_feature(restaurant["attributes"], "Order at Counter")
byob = get_boolean_feature(restaurant["attributes"], "BYOB")
good_for_dancing = get_boolean_feature(restaurant["attributes"], "Good For Dancing")
happy_hour = get_boolean_feature(restaurant["attributes"], "Happy Hour")
coat_check = get_boolean_feature(restaurant["attributes"], "Coat Check")
good_for_kids = get_boolean_feature(restaurant["attributes"], "Good For Kids")
dogs_allowed = get_boolean_feature(restaurant["attributes"], "Dogs Allowed")
drive_thru = get_boolean_feature(restaurant["attributes"], "Drive-Thru")
wheelchair_accessible = get_boolean_feature(restaurant["attributes"], "Wheelchair Accessible")
take_out = get_boolean_feature(restaurant["attributes"], "Take-out")
has_TV = get_boolean_feature(restaurant["attributes"], 'Has TV')
waiter_service = get_boolean_feature(restaurant["attributes"], 'Waiter Service')
takes_reservations = get_boolean_feature(restaurant["attributes"], 'Takes Reservations')
delivery = get_boolean_feature(restaurant["attributes"], 'Delivery')
outdoor_seating = get_boolean_feature(restaurant["attributes"], "Outdoor Seating")
good_for_kids = get_boolean_feature(restaurant["attributes"], "Good for Kids")
good_for_groups = get_boolean_feature(restaurant["attributes"], "Good For Groups")
by_appointment_only = get_boolean_feature(restaurant["attributes"], 'By Appointment Only')
accepts_insurance = get_boolean_feature(restaurant["attributes"], 'Accepts Insurance')
#### Spectrum Variables ######################
smoking = get_smoking(restaurant["attributes"])
wi_fi = get_wifi(restaurant["attributes"])
ages_allowed = get_ages_allowed(restaurant["attributes"])
noise_level = get_noise_level(restaurant["attributes"])
alcohol = get_alcohol(restaurant["attributes"])
attire = get_attire(restaurant["attributes"])
price_range = get_price_range(restaurant["attributes"])
# Get the stars and number of reviews for the restaurant
stars = restaurant["stars"]
review_count = restaurant["review_count"]
attributes = np.array([[
dietary_restrictions_dairy_free,
dietary_restrictions_gluten_free,
dietary_restrictions_halal,
dietary_restrictions_kosher,
dietary_restrictions_soy_free,
dietary_restrictions_vegan,
dietary_restrictions_vegetarian,
parking_valet,
parking_garage,
parking_street,
parking_lot,
parking_validated,
good_for_dessert,
good_for_lunch,
good_for_latenight,
good_for_dinner,
good_for_breakfast,
good_for_brunch,
ambience_romantic,
ambience_intimate,
ambience_touristy,
ambience_hipster,
ambience_divey,
ambience_classy,
ambience_trendy,
ambience_upscale,
ambience_casual,
dj,
jukebox,
live,
video,
karaoke,
background_music,
playlist,
payment_type_amex,
payment_type_cash_only,
payment_type_discover,
payment_type_mastercard,
payment_type_visa,
wi_fi,
caters,
open_24_hours,
corkage,
order_at_counter,
byob,
good_for_dancing,
happy_hour,
smoking,
coat_check,
good_for_kids,
dogs_allowed,
drive_thru,
wheelchair_accessible,
take_out,
has_TV,
waiter_service,
takes_reservations,
delivery,
outdoor_seating,
good_for_kids,
good_for_groups,
by_appointment_only,
accepts_insurance,
ages_allowed,
noise_level,
alcohol,
attire,
price_range
]])
attributes = np.transpose(attributes)
data_entry = (attributes, restaurant_score(stars, review_count))
data_matrix.append(data_entry)
i += 1
random.shuffle(data_matrix)
test_size = int(math.floor(len(data_matrix)*0.2))
test_data = data_matrix[0:test_size]
training_data = data_matrix[test_size + 1: len(data_matrix)]
print("Class 1 count is:")
print(class1)
print("Class 0 count is:")
print(class0)
return (training_data, test_data)
# Returns given attribute
# 0.5 if not present, dummy variable
def get_boolean_feature(attributes, name):
if name not in attributes:
return 0.5
if attributes[name]:
return 1
return 0
# Returns smoking allowed
# 1.5 if not present, dummy variable
def get_smoking(attributes):
if 'Smoking' not in attributes:
return 1
smoking = attributes['Smoking']
if smoking == "yes":
return 0
elif smoking == "outdoor":
return 1
elif smoking == "no":
return 2
# Returns wifi allowed
# 1.5 if not present, dummy variable
def get_wifi(attributes):
if 'Wi-Fi' not in attributes:
return 1
wifi = attributes['Wi-Fi']
if wifi == "no":
return 0
elif wifi == "paid":
return 1
elif wifi == "free":
return 2
# Returns ages allowed
# 1.5 if not present, dummy variable
def get_ages_allowed(attributes):
if 'Ages Allowed' not in attributes:
return 1.5
ages_allowed = attributes['Ages Allowed']
if ages_allowed == "18plus":
return 0
elif ages_allowed == "19plus":
return 1
elif ages_allowed == "21plus":
return 2
elif ages_allowed == "allages":
return 3
# Returns price range
# 2.5 if not present, dummy variable
def get_price_range(attributes):
if 'Price' not in attributes:
return 2.5
return attributes["Price Range"]
# Returns noise level
# 1.5 if not present, dummy variable
def get_noise_level(attributes):
if 'Noise Level' not in attributes:
return 1.5
noise_level = attributes['Noise Level']
if noise_level == "average":
return 0
elif noise_level == "quiet":
return 1
elif noise_level == "loud":
return 2
elif noise_level == "very_loud":
return 3
# Returns music fields
# Values may be 0.5 if they do not exist for the given restaurant
def get_music_fields(attributes):
if 'Music' not in attributes:
return (0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5)
music = attributes["Music"]
if 'playlist' in attributes:
playlist = 1 if music["playlist"] else 0
else:
playlist = 0.5
if 'jukebox' in attributes:
jukebox = 1 if music["jukebox"] else 0
else:
jukebox = 0.5
if 'live' in attributes:
live = 1 if music["live"] else 0
else:
live = 0.5
if 'dj' in attributes:
dj = 1 if music["dj"] else 0
else:
dj = 0.5
if 'video' in attributes:
video = 1 if music["video"] else 0
else:
video = 0.5
if 'karaoke' in attributes:
karaoke = 1 if music["karaoke"] else 0
else:
karaoke = 0.5
if 'background_music' in attributes:
background_music = 1 if music["background_music"] else 0
else:
background_music = 0.5
return (dj,
live,
jukebox,
video,
karaoke,
background_music,
playlist)
# returns payment_type fields
# these may be 0.5 if they do not exist for the given restaurant
def get_payment_type_fields(attributes):
if 'Payment Type' not in attributes:
return (0.5, 0.5, 0.5, 0.5, 0.5)
payment_type = attributes["payment_type"]
if 'amex' in payment_type:
payment_type_amex = 1 if payment_type["amex"] else 0
else:
payment_type_amex = 0.5
if 'cash_only' in payment_type:
payment_type_cash_only = 1 if payment_type["cash_only"] else 0
else:
payment_type_cash_only = 0.5
if 'discover' in payment_type:
payment_type_discover = 1 if payment_type["discover"] else 0
else:
payment_type_discover = 0.5
if 'mastercard' in payment_type:
payment_type_mastercard = 1 if payment_type["mastercard"] else 0
else:
payment_type_mastercard = 0.5
if 'visa' in payment_type:
payment_type_visa = 1 if payment_type["visa"] else 0
else:
payment_type_visa = 0.5
return (
payment_type_amex,
payment_type_cash_only,
payment_type_discover,
payment_type_mastercard,
payment_type_visa,
)
# returns dietary_restrictions fields
# these may be 0.5 if they do not exist for the given restaurant
def get_dietary_restrictions(attributes):
if 'Dietary Restrictions' not in attributes:
return (0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5)
dietary_restrictions = attributes["Dietary Restrictions"]
if 'dairy_free' in dietary_restrictions:
dietary_restrictions_dairy_free = 1 if dietary_restrictions["dairy_free"] else 0
else:
dietary_restrictions_dairy_free = 0.5
if 'gluten_free' in dietary_restrictions:
dietary_restrictions_gluten_free = 1 if dietary_restrictions["gluten_free"] else 0
else:
dietary_restrictions_gluten_free = 0.5
if 'halal' in dietary_restrictions:
dietary_restrictions_halal = 1 if dietary_restrictions["halal"] else 0
else:
dietary_restrictions_halal = 0.5
if 'kosher' in dietary_restrictions:
dietary_restrictions_kosher = 1 if dietary_restrictions["kosher"] else 0
else:
dietary_restrictions_kosher = 0.5
if 'soy_free' in dietary_restrictions:
dietary_restrictions_soy_free = 1 if dietary_restrictions["soy-free"] else 0
else:
dietary_restrictions_soy_free = 0.5
if 'vegan' in dietary_restrictions:
dietary_restrictions_vegan = 1 if dietary_restrictions["vegan"] else 0
else:
dietary_restrictions_vegan = 0.5
if 'vegetarian' in dietary_restrictions:
dietary_restrictions_vegetarian = 1 if dietary_restrictions["vegetarian"] else 0
else:
dietary_restrictions_vegetarian = 0.5
return (
dietary_restrictions_dairy_free,
dietary_restrictions_gluten_free,
dietary_restrictions_halal,
dietary_restrictions_kosher,
dietary_restrictions_soy_free,
dietary_restrictions_vegan,
dietary_restrictions_vegetarian
)
# returns parking fields
# these may be 0.5 if they do not exist for the given restaurant
def get_parking_fields(attributes):
if 'Parking' not in attributes:
return (0.5, 0.5, 0.5, 0.5, 0.5)
parking = attributes["Parking"]
if 'valet' in parking:
parking_valet = 1 if parking["valet"] else 0
else:
parking_valet = 0.5
if 'garage' in parking:
parking_garage = 1 if parking["garage"] else 0
else:
parking_garage = 0.5
if 'street' in parking:
parking_street = 1 if parking["street"] else 0
else:
parking_street = 0.5
if 'lot' in parking:
parking_lot = 1 if parking["lot"] else 0
else:
parking_lot = 0.5
if 'validated' in parking:
parking_validated = 1 if parking["validated"] else 0
else:
parking_validated = 0.5
return (
parking_valet,
parking_garage,
parking_street,
parking_lot,
parking_validated,
)
# Returns good for fields
# These may be 0.5 if they do not exist for the given restaurant
def get_good_for_fields(attributes):
if 'Good For' not in attributes:
return (0.5, 0.5, 0.5, 0.5, 0.5, 0.5)
good_for = attributes["Good For"]
if 'dessert' in good_for:
good_for_dessert = 1 if good_for["dessert"] else 0
else:
good_for_dessert = 0.5
if 'breakfast' in good_for:
good_for_breakfast = 1 if good_for["breakfast"] else 0
else:
good_for_breakfast = 0.5
if 'brunch' in good_for:
good_for_brunch = 1 if good_for["brunch"] else 0
else:
good_for_brunch = 0.5
if 'lunch' in good_for:
good_for_lunch = 1 if good_for["lunch"] else 0
else:
good_for_lunch = 0.5
if 'dinner' in good_for:
good_for_dinner = 1 if good_for["dinner"] else 0
else:
good_for_dinner = 0.5
if 'latenight' in good_for:
good_for_latenight = 1 if good_for["latenight"] else 0
else:
good_for_latenight = 0.5
return (
good_for_dessert,
good_for_breakfast,
good_for_brunch,
good_for_lunch,
good_for_dinner,
good_for_latenight,
)
# Returns ambience fields
# These may be 0.5 if they do not exist for the given restaurant
def get_ambience_fields(attributes):
if 'Ambience' not in attributes:
return (0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5)
ambience = attributes["Ambience"]
if 'romantic' in ambience:
ambience_romantic = 1 if ambience["romantic"] else 0
else:
ambience_romantic = 0.5
if 'intimate' in ambience:
ambience_intimate = 1 if ambience["intimate"] else 0
else:
ambience_intimate = 0.5
if 'touristy' in ambience:
ambience_touristy = 1 if ambience["touristy"] else 0
else:
ambience_touristy = 0.5
if 'hipster' in ambience:
ambience_hipster = 1 if ambience["hipster"] else 0
else:
ambience_hipster = 0.5
if 'divey' in ambience:
ambience_divey = 1 if ambience["divey"] else 0
else:
ambience_divey = 0.5
if 'trendy' in ambience:
ambience_trendy = 1 if ambience["trendy"] else 0
else:
ambience_trendy = 0.5
if 'classy' in ambience:
ambience_classy = 1 if ambience["classy"] else 0
else:
ambience_classy = 0.5
if 'upscale' in ambience:
ambience_upscale = 1 if ambience["upscale"] else 0
else:
ambience_upscale = 0.5
if 'casual' in ambience:
ambience_casual = 1 if ambience["casual"] else 0
else:
ambience_casual = 0.5
return (
ambience_romantic,
ambience_intimate,
ambience_touristy,
ambience_hipster,
ambience_divey,
ambience_classy,
ambience_trendy,
ambience_upscale,
ambience_casual,
)
# Returns alcohol rating (none, full_bar, beer_and_wine)
# Returns 1 if alcohol is not a present feature
def get_alcohol(attributes):
if 'Alcohol' not in attributes:
return 1
alcohol = attributes['Alcohol']
if alcohol == "none":
return 0
elif alcohol == "full_bar":
return 1
elif alcohol == "beer_and_wine":
return 2
# Returns attire rating (casual, dressy, formal)
def get_attire(attributes):
if 'Attire' not in attributes:
return 1
attire = attributes['Attire']
if attire == "casual":
return 0
elif attire == "dressy":
return 1
elif attire == "formal":
return 2
# Returns the label for a restaurant based on stars and review count
def restaurant_score(stars, review_count):
global class1
global class0
val = np.zeros((2, 1))
if stars >= 3.5 and review_count > 37:
class1 += 1
val[1] = 1
else:
class0 += 1
val[0] = 1
return val
class1 = 0
class0 = 0