-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_notebook
9968 lines (9968 loc) · 790 KB
/
final_notebook
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Coursera - IBM Data Science Capstone Project ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Brian Lee ###\n",
"[email protected]<br />\n",
"LinkedIn: https://www.linkedin.com/in/brian-lee-64a2022b/\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Introduction ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If I were to open a Starbucks, where should I locate it? Clearly, the location is one of the most important business decisions for this venture. Is there a science to locating a StarBucks? Can we create a machine learning model to predict where a StarBucks is likely to be located? I'd like to find out.\n",
"\n",
"If a reliable model can be made,it could be used in the process of opening a store. It could be used as a final sanity check, or at the beginning stage, to select a promising location. If a trustworthy model predicts with a high degree of confidence that a Starbucks should be located in an area, but there is not one there yet, perhaps that is an opportunity.\n",
"\n",
"Any person interested in opening a Starbucks should be interested in these results. This includes myself, other potential franchisees, and the Starbucks corporation itself, which operates many of it's own stores. The Starbucks competition might also be interested as they could possibly gain competitive insigths. I also believe others might be interested in this procedure, as it might be applied to predicting the location of other entities, based on the same sort of data.\n",
"\n",
"The main purpose of this project is to prove the concept of predicting Starbuck's locations in general. I suspect it may work better or worse depending on the locations chosen for training and prediction, the radius size, and the specific features used in the model. I may vary those factors in order to prove the concept, which could then be applied carefully to a particular geographic area of interest at a later date."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Data ###"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Foursquare (https://foursquare.com) is a company that crowdsources location data, tying latitude-longitude coordinates (and other things) with public venues, including many businesses, such as Starbucks locations. The features for the Starbucks location classifier will come from venue data from the Fourquare API. I will use venue category names (such as 'bar', 'Chinese restaurant', 'coffee shop') as features to classify an area as either an area with a Starbucks, or an area without one. I will use the venue name (e.g. 'Starbucks') to determine whether or not the venue is a Starbucks. An area containing a Starbucks contains at least one venue named 'Starbucks' within the radius supplied to the Foursquare API's explore' endpoint.\n",
"\n",
"An area will be a circular area with a given radius. I have not determined that radius yet, and I might use more than one radius in this project. Within that radius, the Foursquare API will return venues. The larger the radius, the more venues that could be anticipated, and the higher likelihood of a Starbucks, but a larger radius is also less useful for business use. I have done some exploratory work with a radius of 300m.\n",
"\n",
"The areas to be chosen should contain a mix of areas with Starbuck's and those without. I have done some exploratory work in a particular area of San Francisco. I located Starbucks with Google Maps and got latitude and longitude points making a box around that area. I could choose the train and test points at random within an area, or I could generate a grid covering an entire area. Given the constraints my Foursquare Developer account places on my searches, and knowing that binary classifiers work better with a good number of examples of both classes, I may want to generate areas that I know will be heavy with Starbucks locations. To that end, I discovered a Kaggle dataset (https://www.kaggle.com/starbucks/store-locations) containing the latitude and longitude coordinates of 25,600 worldwide Starbucks locations. I may or may not use that resource."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Methodology ###\n",
"\n",
"I will generate a corpus of geographic points, and using a reasonable radius (perhaps 300m) a corpus of geographic areas. Each geographic area will have a feature set based on json data returned by the Foursquare API's 'explore' endpoint. I plan to use the category names of the returned venues as features for that area. I will use the venue names to determine whether oir not a Starbucks is in that area, and generate the labels from that. \n",
"\n",
"Once I have labelled data, I will use sklearn.model_selection.train_test_split to generate tarining and testing sets. Then I can use any of a host of sklearn classifiers to generate predictive models. I have done some exploratory work with a random forest."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Libraries imported!\n"
]
}
],
"source": [
"# import libraries\n",
"\n",
"import urllib.request # open and read URLs\n",
"\n",
"import json # handle JSON files\n",
"from pandas.io.json import json_normalize # tranform JSON file into a pandas dataframe\n",
"\n",
"import requests # handle requests\n",
"import pandas as pd # process data as dataframes with Pandas\n",
"import numpy as np # handle data in a vectorized manner with NumPy\n",
"\n",
"# !conda install -c conda-forge geopy --yes # uncomment this line if you haven't installed the GeoPy geocoding library yet\n",
"from geopy.geocoders import Nominatim # convert an address into latitude and longitude values\n",
"\n",
"##!conda install -c conda-forge folium=0.5.0 --yes # uncomment this line if you haven't installed the Folium library yet\n",
"import folium # map rendering library\n",
"\n",
"# Matplolib plotting library and associated modules\n",
"import matplotlib.pyplot as plt \n",
"import matplotlib.cm as cm\n",
"import matplotlib.colors as colors\n",
"\n",
"from sklearn.cluster import KMeans # for K-Means clustering with Scikit-Learn\n",
"\n",
"print(\"Libraries imported!\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# The code was removed by Watson Studio for sharing."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Define a function to gather recommended venues, with specifically the name and category, using the explore API\n",
"\n",
"def getNearbyVenues(latitudes, longitudes, radius=300, limit=100):\n",
" \n",
" count = 0\n",
" venues_list = []\n",
" for lat, lng in zip(latitudes, longitudes):\n",
" #create a unique namefor this pointthat is easier todeal with than a combination of lat and lon\n",
" point_name = \"point\"+str(count)\n",
" print(point_name,':',lat,'-',lng)\n",
" \n",
"\n",
" \n",
" # create the API request URL\n",
" url = \"https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}\".format(\n",
" CLIENT_ID, \n",
" CLIENT_SECRET, \n",
" VERSION, \n",
" lat, \n",
" lng, \n",
" radius, \n",
" limit)\n",
" \n",
" # make the GET request\n",
" results = requests.get(url).json()[\"response\"][\"groups\"][0][\"items\"]\n",
" #print(\"resultscols=\",results.columns)\n",
" \n",
"\n",
" \n",
" # return only relevant information for each nearby venue\n",
" venues_list.append([(\n",
" point_name,\n",
" lat, \n",
" lng, \n",
" v[\"venue\"][\"name\"], \n",
" v[\"venue\"][\"location\"][\"lat\"], \n",
" v[\"venue\"][\"location\"][\"lng\"], \n",
" v[\"venue\"][\"location\"][\"distance\"], \n",
" v[\"venue\"][\"categories\"][0][\"name\"]) for v in results])\n",
" count = count + 1\n",
" nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])\n",
" nearby_venues.columns = [\"Point Name\",\n",
" \"Neighborhood Latitude\", \n",
" \"Neighborhood Longitude\", \n",
" \"Venue\", \n",
" \"Venue Latitude\", \n",
" \"Venue Longitude\", \n",
" \"Venue Distance\", \n",
" \"Venue Category\"]\n",
" \n",
" return(nearby_venues)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"sf_lat=37.7749\n",
"sf_lon=-122.4194\n",
"\n",
"import random\n",
"\n",
"#37.780253, -122.509117\n",
"#37.804203, -122.459264 - upper left of starbucks intensive space\n",
"#37.767547, -122.409342 - lower right ofstarbucksintensive space\n",
"llat=37.767547\n",
"ulat=37.804203\n",
"llon=-122.459264\n",
"ulon=-122.409342"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"37.80795680296507 -122.45245054492938\n",
"37.807956605930144 -122.44563708987684\n",
"37.807956408895215 -122.43882363484242\n",
"37.80795621186029 -122.4320101798261\n",
"37.807956014825365 -122.42519672482788\n",
"37.80795581779044 -122.41838326984777\n",
"37.80795562075552 -122.41156981488575\n",
"37.80795542372061 -122.40475635994184\n",
"new row of coordinates\n",
"37.80255104525841 -122.45245104155096\n",
"37.80255084826162 -122.44563808312\n",
"37.80255065126483 -122.43882512470717\n",
"37.80255045426805 -122.4320121663124\n",
"37.802550257271264 -122.42519920793575\n",
"37.802550060274484 -122.41838624957718\n",
"37.802549863277704 -122.4115732912367\n",
"37.80254966628091 -122.40476033291432\n",
"new row of coordinates\n",
"37.79714528257788 -122.4524515380398\n",
"37.79714508561922 -122.44563907609765\n",
"37.79714488866056 -122.43882661417359\n",
"37.7971446917019 -122.43201415226763\n",
"37.79714449474326 -122.42520169037975\n",
"37.79714429778461 -122.41838922850997\n",
"37.79714410082596 -122.41157676665827\n",
"37.797143903867315 -122.40476430482464\n",
"new row of coordinates\n",
"37.79173951492371 -122.45245203439586\n",
"37.79173931800319 -122.44564006880981\n",
"37.791739121082664 -122.43882810324182\n",
"37.79173892416214 -122.43201613769192\n",
"37.79173872724161 -122.42520417216008\n",
"37.79173853032108 -122.41839220664633\n",
"37.791738333400566 -122.41158024115066\n",
"37.79173813648005 -122.40476827567306\n",
"new row of coordinates\n",
"37.78633374229618 -122.45245253061923\n",
"37.786333545413775 -122.44564106125655\n",
"37.78633334853137 -122.43882959191193\n",
"37.78633315164897 -122.43201812258538\n",
"37.786332954766564 -122.42520665327689\n",
"37.78633275788416 -122.41839518398648\n",
"37.78633256100176 -122.41158371471414\n",
"37.78633236411936 -122.40477224545988\n",
"new row of coordinates\n",
"37.780927964695465 -122.45245302670996\n",
"37.780927767851175 -122.445642053438\n",
"37.78092757100689 -122.43883108018407\n",
"37.78092737416261 -122.43202010694823\n",
"37.78092717731832 -122.42520913373045\n",
"37.780926980474035 -122.4183981605307\n",
"37.78092678362975 -122.41158718734903\n",
"37.780926586785476 -122.40477621418542\n",
"new row of coordinates\n",
"37.77552218212185 -122.45245352266805\n",
"37.77552198531567 -122.44564304535416\n",
"37.775521788509494 -122.43883256805834\n",
"37.77552159170332 -122.43202209078056\n",
"37.77552139489714 -122.42521161352084\n",
"37.775521198090956 -122.41840113627917\n",
"37.775521001284794 -122.41159065905556\n",
"37.775520804478624 -122.40478018184999\n",
"new row of coordinates\n",
"37.77011639457553 -122.45245401849358\n",
"37.77011619780746 -122.44564403700518\n",
"37.77011600103939 -122.43883405553484\n",
"37.77011580427131 -122.43202407408255\n",
"37.77011560750325 -122.4252140926483\n",
"37.77011541073519 -122.4184041112321\n",
"37.77011521396712 -122.41159412983394\n",
"37.770115017199046 -122.40478414845384\n",
"new row of coordinates\n",
"37.76471060205676 -122.45245451418654\n",
"37.76471040532678 -122.4456450283911\n",
"37.76471020859681 -122.43883554261372\n",
"37.76471001186684 -122.43202605685437\n",
"37.76470981513687 -122.42521657111305\n",
"37.764709618406904 -122.41840708538979\n",
"37.76470942167694 -122.41159759968455\n",
"37.764709224946984 -122.40478811399736\n",
"new row of coordinates\n",
"37.75930480456578 -122.452455009747\n",
"37.759304607873894 -122.44564601951201\n",
"37.75930441118203 -122.43883702929507\n",
"37.75930421449015 -122.43202803909617\n",
"37.759304017798286 -122.42521904891527\n",
"37.759303821106414 -122.41841005875241\n",
"37.759303624414535 -122.41160106860758\n",
"37.75930342772267 -122.40479207848078\n",
"new row of coordinates\n",
"37.75389900210284 -122.45245550517498\n",
"37.75389880544905 -122.44564701036798\n",
"37.75389860879526 -122.43883851557901\n",
"37.75389841214148 -122.43203002080806\n",
"37.75389821548771 -122.42522152605513\n",
"37.75389801883393 -122.4184130313202\n",
"37.75389782218016 -122.41160453660329\n",
"37.75389762552637 -122.40479604190442\n",
"new row of coordinates\n",
"37.74849319466815 -122.45245600047053\n",
"37.74849299805245 -122.44564800095907\n",
"37.748492801436754 -122.4388400014656\n",
"37.74849260482105 -122.43203200199018\n",
"37.74849240820536 -122.42522400253276\n",
"37.74849221158966 -122.41841600309334\n",
"37.74849201497396 -122.41160800367196\n",
"37.74849181835827 -122.40480000426858\n",
"new row of coordinates\n",
"37.74308738226196 -122.45245649563368\n",
"37.74308718568433 -122.44564899128537\n",
"37.7430869891067 -122.43884148695507\n",
"37.74308679252907 -122.43203398264276\n",
"37.743086595951446 -122.42522647834848\n",
"37.743086399373816 -122.41841897407217\n",
"37.74308620279619 -122.41161146981388\n",
"37.74308600621857 -122.40480396557358\n",
"new row of coordinates\n",
"37.737681564884504 -122.45245699066447\n",
"37.737681368344944 -122.44564998134696\n",
"37.73768117180539 -122.43884297204742\n",
"37.73768097526584 -122.43203596276588\n",
"37.737680778726286 -122.42522895350234\n",
"37.737680582186734 -122.41842194425679\n",
"37.73768038564719 -122.41161493502925\n",
"37.73768018910764 -122.4048079258197\n",
"new row of coordinates\n",
"37.73227574253603 -122.45245748556296\n",
"37.73227554603454 -122.44565097114389\n",
"37.732275349533055 -122.43884445674281\n",
"37.732275153031566 -122.43203794235973\n",
"37.73227495653009 -122.42523142799463\n",
"37.73227476002861 -122.41842491364753\n",
"37.732274563527135 -122.41161839931841\n",
"37.73227436702565 -122.40481188500729\n",
"new row of coordinates\n",
"37.72686991521677 -122.45245798032916\n",
"37.726869718753335 -122.44565196067626\n",
"37.72686952228992 -122.43884594104135\n",
"37.7268693258265 -122.43203992142443\n",
"37.726869129363074 -122.4252339018255\n",
"37.726868932899656 -122.41842788224453\n",
"37.726868736436245 -122.41162186268153\n",
"37.72686853997283 -122.40481584313652\n",
"new row of coordinates\n",
"37.721464082926936 -122.45245847496308\n",
"37.72146388650157 -122.44565294994413\n",
"37.72146369007621 -122.43884742494316\n",
"37.72146349365085 -122.43204189996015\n",
"37.72146329722549 -122.42523637499512\n",
"37.721463100800136 -122.41843085004804\n",
"37.721462904374775 -122.41162532511895\n",
"37.72146270794941 -122.40481980020783\n",
"new row of coordinates\n",
"37.716058245666794 -122.45245896946481\n",
"37.716058049279475 -122.44565393894759\n",
"37.716057852892156 -122.43884890844832\n",
"37.716057656504844 -122.432043877967\n",
"37.716057460117526 -122.42523884750368\n",
"37.716057263730214 -122.4184338170583\n",
"37.71605706734291 -122.41162878663089\n",
"37.716056870955605 -122.40482375622143\n",
"new row of coordinates\n",
"37.71065240343657 -122.45245946383437\n",
"37.7106522070873 -122.44565492768672\n",
"37.71065201073803 -122.438850391557\n",
"37.71065181438877 -122.43204585544522\n",
"37.71065161803949 -122.42524131935141\n",
"37.71065142169023 -122.41843678327555\n",
"37.71065122534096 -122.41163224721765\n",
"37.71065102899169 -122.4048277111777\n",
"new row of coordinates\n",
"37.705246556236524 -122.45245995807181\n",
"37.7052463599253 -122.44565591616156\n",
"37.70524616361406 -122.43885187426925\n",
"37.70524596730283 -122.4320478323949\n",
"37.705245770991596 -122.42524379053849\n",
"37.705245574680376 -122.41843974870001\n",
"37.705245378369156 -122.41163570687947\n",
"37.70524518205793 -122.4048316650769\n",
"new row of coordinates\n",
"37.699840704066865 -122.45246045217714\n",
"37.699840507793674 -122.44565690437221\n",
"37.69984031152048 -122.43885335658523\n",
"37.69984011524729 -122.43204980881617\n",
"37.6998399189741 -122.42524626106507\n",
"37.69983972270091 -122.4184427133319\n",
"37.699839526427716 -122.41163916561666\n",
"37.699839330154525 -122.40483561791933\n",
"new row of coordinates\n",
"37.69443484692786 -122.45246094615041\n",
"37.69443465069269 -122.44565789231874\n",
"37.69443445445753 -122.438854838505\n",
"37.694434258222365 -122.43205178470919\n",
"37.69443406198721 -122.42524873093129\n",
"37.694433865752046 -122.41844567717133\n",
"37.69443366951689 -122.41164262342933\n",
"37.69443347328174 -122.40483956970522\n",
"new row of coordinates\n",
"37.68902898481972 -122.45246143999165\n",
"37.68902878862257 -122.4456588800012\n",
"37.68902859242544 -122.4388563200287\n",
"37.689028396228295 -122.43205376007411\n",
"37.689028200031146 -122.42525120013744\n",
"37.68902800383401 -122.4184486402187\n",
"37.68902780763688 -122.41164608031788\n",
"37.689027611439755 -122.40484352043497\n",
"new row of coordinates\n",
"37.683623117742705 -122.45246193370092\n",
"37.68362292158358 -122.44565986741974\n",
"37.683622725424456 -122.43885780115646\n",
"37.68362252926533 -122.4320557349111\n",
"37.68362233310621 -122.42525366868365\n",
"37.68362213694708 -122.41845160247412\n",
"37.68362194078797 -122.41164953628251\n",
"37.683621744628866 -122.4048474701088\n",
"new row of coordinates\n",
"37.678217245697034 -122.45246242727823\n",
"37.67821704957592 -122.44566085457438\n",
"37.67821685345481 -122.43885928188843\n",
"37.6782166573337 -122.43205770922037\n",
"37.678216461212585 -122.42525613657023\n",
"37.678216265091486 -122.41845456393798\n",
"37.678216068970386 -122.41165299132363\n",
"37.678215872849286 -122.40485141872719\n",
"new row of coordinates\n",
"37.67281136868298 -122.45246292072365\n",
"37.67281117259987 -122.4456618414652\n",
"37.67281097651677 -122.43886076222462\n",
"37.67281078043367 -122.43205968300195\n",
"37.67281058435057 -122.42525860379719\n",
"37.672810388267465 -122.4184575246103\n",
"37.67281019218437 -122.41165644544131\n",
"37.67280999610127 -122.40485536629023\n",
"new row of coordinates\n",
"37.66740548670076 -122.45246341403718\n",
"37.66740529065566 -122.44566282809225\n",
"37.66740509461056 -122.43886224216521\n",
"37.66740489856546 -122.43206165625607\n",
"37.66740470252036 -122.4252610703648\n",
"37.66740450647526 -122.4184604844914\n",
"37.66740431043016 -122.41165989863592\n",
"37.667404114385064 -122.40485931279831\n",
"new row of coordinates\n",
"37.661999599750594 -122.45246390721888\n",
"37.661999403743486 -122.44566381445564\n",
"37.66199920773638 -122.43886372171028\n",
"37.66199901172928 -122.43206362898279\n",
"37.66199881572218 -122.42526353627319\n",
"37.66199861971508 -122.41846344358144\n",
"37.661998423707985 -122.4116633509076\n",
"37.66199822770089 -122.40486325825164\n",
"new row of coordinates\n",
"37.65659370783275 -122.4524644002688\n",
"37.65659351186364 -122.44566480055546\n",
"37.65659331589453 -122.43886520085998\n",
"37.656593119925425 -122.43206560118237\n",
"37.65659292395632 -122.42526600152264\n",
"37.6565927279872 -122.41846640188078\n",
"37.656592532018095 -122.41166680225679\n",
"37.65659233604899 -122.40486720265068\n",
"new row of coordinates\n",
"37.651187810947455 -122.45246489318694\n",
"37.65118761501632 -122.44566578639174\n",
"37.65118741908519 -122.43886667961439\n",
"37.65118722315406 -122.43206757285492\n",
"37.65118702722294 -122.4252684661133\n",
"37.65118683129182 -122.41846935938955\n",
"37.6511866353607 -122.41167025268365\n",
"37.65118643942958 -122.40487114599561\n",
"new row of coordinates\n",
"37.64578190909498 -122.45246538597335\n",
"37.64578171320182 -122.44566677196457\n",
"37.64578151730867 -122.43886815797362\n",
"37.645781321415534 -122.43206954400053\n",
"37.64578112552239 -122.4252709300453\n",
"37.64578092962925 -122.41847231610791\n",
"37.64578073373611 -122.41167370218838\n",
"37.645780537842974 -122.40487508828672\n",
"new row of coordinates\n",
"37.640376002275524 -122.45246587862809\n",
"37.64037580642035 -122.44566775727402\n",
"37.64037561056517 -122.4388696359378\n",
"37.64037541471001 -122.43207151461944\n",
"37.64037521885483 -122.4252733933189\n",
"37.640375022999656 -122.41847527203622\n",
"37.64037482714449 -122.41167715077138\n",
"37.64037463128932 -122.40487902952438\n",
"new row of coordinates\n",
"37.63497009048935 -122.45246637115119\n",
"37.634969894672146 -122.44566874232021\n",
"37.63496969885495 -122.43887111350706\n",
"37.63496950303775 -122.43207348471175\n",
"37.63496930722056 -122.42527585593427\n",
"37.634969111403365 -122.41847822717463\n",
"37.63496891558617 -122.41168059843284\n",
"37.63496871976898 -122.40488296970886\n",
"new row of coordinates\n",
"37.62956417373667 -122.45246686354265\n",
"37.62956397795742 -122.44566972710314\n",
"37.62956378217819 -122.43887259068146\n",
"37.62956358639895 -122.43207545427758\n",
"37.62956339061971 -122.42527831789155\n",
"37.629563194840486 -122.41848118152336\n",
"37.62956299906126 -122.41168404517298\n",
"37.62956280328203 -122.40488690884042\n",
"new row of coordinates\n",
"37.62415825201776 -122.45246735580254\n",
"37.62415805627648 -122.44567071162292\n",
"37.6241578605352 -122.43887406746111\n",
"37.62415766479392 -122.43207742331714\n",
"37.624157469052655 -122.42528077919097\n",
"37.62415727331139 -122.41848413508264\n",
"37.624157077570125 -122.41168749099212\n",
"37.62415688182885 -122.40489084691941\n",
"new row of coordinates\n",
"37.61875232533285 -122.45246784793092\n",
"37.61875212962952 -122.44567169587964\n",
"37.618751933926184 -122.43887554384617\n",
"37.61875173822286 -122.43207939183054\n",
"37.61875154251955 -122.42528323983272\n",
"37.618751346816225 -122.41848708785271\n",
"37.61875115111291 -122.41169093589052\n",
"37.61875095540959 -122.40489478394613\n",
"new row of coordinates\n",
"37.61334639368217 -122.45246833992779\n",
"37.61334619801679 -122.44567267987338\n",
"37.61334600235141 -122.4388770198368\n",
"37.613345806686034 -122.43208135981799\n",
"37.61334561102066 -122.42528569981698\n",
"37.61334541535528 -122.4184900398338\n",
"37.61334521968992 -122.41169437986842\n",
"37.613345024024554 -122.40489871992084\n",
"new row of coordinates\n",
"37.60794045706597 -122.4524688317932\n",
"37.607940261438536 -122.4456736636042\n",
"37.6079400658111 -122.43887849543297\n",
"37.60793987018368 -122.43208332727954\n",
"37.60793967455625 -122.42528815914393\n",
"37.607939478928806 -122.41849299102608\n",
"37.607939283301384 -122.41169782292607\n",
"37.607939087673955 -122.40490265484384\n",
"new row of coordinates\n",
"37.60253451548451 -122.45246932352718\n",
"37.602534319895014 -122.44567464707212\n",
"37.60253412430552 -122.43887997063487\n",
"37.60253392871603 -122.4320852942154\n",
"37.602533733126535 -122.42529061781373\n",
"37.60253353753705 -122.41849594142985\n",
"37.60253334194755 -122.41170126506377\n",
"37.602533146358056 -122.40490658871546\n",
"new row of coordinates\n"
]
}
],
"source": [
"#37.599350, -122.496830\n",
"#37.807957, -122.390854\n",
"#reset parameters to bigger area\n",
"llat = 37.599350\n",
"ulat = 37.807957\n",
"##llon = -122.496830\n",
"##ulon = -122.390854\n",
"\n",
"\n",
"points = []\n",
"radius = 300 #in meters\n",
"current_lat = ulat\n",
"current_lon = llon\n",
"import geopy\n",
"from geopy.distance import VincentyDistance\n",
"d = radius/1000 #in kilometers\n",
"d=d*2\n",
"b=90 #in degrees 90 = east,180=south,270=west\n",
"# given: lat1, lon1, b = bearing in degrees, d = distance in kilometers\n",
"\n",
"def lon_pass(current_lat, current_lon, ulat, ulon):\n",
" while (current_lat <= ulat) and (current_lon <= ulon) and (current_lat >= llat) and (current_lon >= llon):\n",
" origin = geopy.Point(current_lat, current_lon)\n",
" destination = VincentyDistance(kilometers=d).destination(origin, b)\n",
"\n",
" current_lat, current_lon = destination.latitude, destination.longitude\n",
" print(current_lat, current_lon)\n",
" points.append((current_lat, current_lon))\n",
"\n",
"while (current_lat <= ulat) and (current_lon <= ulon) and (current_lat >= llat) and (current_lon >= llon):\n",
" lon_pass(current_lat, current_lon, ulat, ulon)\n",
" print('new row of coordinates') \n",
" current_lon=llon#move back to beginning of row \n",
" origin = geopy.Point(current_lat, current_lon)\n",
" destination = VincentyDistance(kilometers=d).destination(origin, 180)#move south one step for next row\n",
" current_lat, current_lon = destination.latitude, destination.longitude\n",
"\n",
" \n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### We many need more examples of the near-Starbucks class for a balanced set - so get a bunch of points we know are near to Starbucks\n",
"I pulled coordinates from the Kaggle Starbucks locations dataset<br />\n",
"The precision of the Kaggle set wasn't good enough to ensure the Starbucks location would be within the radius chosen, so I found a different dataset with higher precision"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"#starbucks_heavy_coordinates = pd.read_csv('data/directory-us-ca.csv') #this file gave lat-lon tothe hundreths, but that isn't good enough to ensure a Starbucks is within my radius\n",
"starbucks_heavy_coordinates = pd.read_csv('data/starbucks_us_ca_locations.csv',header=None)\n",
"starbucks_heavy_coordinates.columns = [\"Longitude\", \"Latitude\", \"Location\", \"Address\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Longitude</th>\n",
" <th>Latitude</th>\n",
" <th>Location</th>\n",
" <th>Address</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-118.114954</td>\n",
" <td>34.079000</td>\n",
" <td>Starbucks - CA - ALHAMBRA [W] 00373</td>\n",
" <td>Valley & Almansor, Alhambra_810 E VALLEY BLVD_...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-117.428300</td>\n",
" <td>34.626700</td>\n",
" <td>Starbucks - CA - Adelanto [WD] 00374</td>\n",
" <td>Palmdale & Hwy 395, Adelanto_14136 US Hwy 395_...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-118.757319</td>\n",
" <td>34.153435</td>\n",
" <td>Starbucks - CA - Agoura Hills 00375</td>\n",
" <td>Vons-Agoura Hills #2001_5671 Kanan Rd._Agoura ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>-118.757134</td>\n",
" <td>34.160600</td>\n",
" <td>Starbucks - CA - Agoura [W] 00376</td>\n",
" <td>Agoura_5827 Canan Road_Agoura, California 9130...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-122.277604</td>\n",
" <td>37.788222</td>\n",
" <td>Starbucks - CA - Alameda 00377</td>\n",
" <td>Albertsons-Alameda #7031_815 Marina Village_Al...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>-122.253388</td>\n",
" <td>37.757099</td>\n",
" <td>Starbucks - CA - Alameda 00378</td>\n",
" <td>Safeway - Alameda #2708_2227 South Shore Cente...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>-122.233245</td>\n",
" <td>37.768070</td>\n",
" <td>Starbucks - CA - Alameda 00379</td>\n",
" <td>Tilden & Blanding - Alameda_2671 Blanding Aven...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>-122.243242</td>\n",
" <td>37.763696</td>\n",
" <td>Starbucks - CA - Alameda [W] 00380</td>\n",
" <td>Park Street & Central - Alameda_1364 Park Stre...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>-122.252229</td>\n",
" <td>37.756650</td>\n",
" <td>Starbucks - CA - Alameda [W] 00381</td>\n",
" <td>South Shore Shopping Center_2210-J South Shore...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>-122.276112</td>\n",
" <td>37.779659</td>\n",
" <td>Starbucks - CA - Alameda [W] 00382</td>\n",
" <td>Webster & Atlantic - Alameda_720 Atlantic Aven...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>-122.033770</td>\n",
" <td>37.850140</td>\n",
" <td>Starbucks - CA - Alamo 00383</td>\n",
" <td>Safeway-Alamo #962_200 Alamo Plaza_Alamo, Cali...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>-122.031998</td>\n",
" <td>37.849275</td>\n",
" <td>Starbucks - CA - Alamo [W] 00384</td>\n",
" <td>Las Trampas & Danville- Alamo_3225 Danville Bo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>-122.307930</td>\n",
" <td>37.885310</td>\n",
" <td>Starbucks - CA - Albany 00385</td>\n",
" <td>Target Albany T-1926_1057 Eastshore Hwy_Albany...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>-118.151899</td>\n",
" <td>34.079716</td>\n",
" <td>Starbucks - CA - Alhambra [W] 00386</td>\n",
" <td>Fremont Ave & Mission Rd_1131 S Fremont Ave_Al...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>-118.122876</td>\n",
" <td>34.078553</td>\n",
" <td>Starbucks - CA - Alhambra [W] 00387</td>\n",
" <td>Garfield & Valley, Alhambra_1 East Valley Blvd...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>-118.127970</td>\n",
" <td>34.094562</td>\n",
" <td>Starbucks - CA - Alhambra [W] 00388</td>\n",
" <td>Main & 1st - Alhambra_101 W. Main Street_Alham...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>-117.703145</td>\n",
" <td>33.575604</td>\n",
" <td>Starbucks - CA - Aliso Viejo [W] 00389</td>\n",
" <td>Aliso Viejo_27072 La Paz Road_Aliso Viejo, Cal...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>-122.263695</td>\n",
" <td>38.165844</td>\n",
" <td>Starbucks - CA - American Canyon 00390</td>\n",
" <td>Safeway-American Canyon #1883_103 American Can...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>-122.245290</td>\n",
" <td>38.171000</td>\n",
" <td>Starbucks - CA - American Canyon [WD] 00391</td>\n",
" <td>Napa Junction & Hwy 29 - American C_6050 Main ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>-122.263920</td>\n",
" <td>38.165852</td>\n",
" <td>Starbucks - CA - American Canyon [W] 00392</td>\n",
" <td>American Canyon & Highway 29_101 W American Ca...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>-117.917858</td>\n",
" <td>33.799640</td>\n",
" <td>Starbucks - CA - Anaheim 00393</td>\n",
" <td>Anaheim Marriott Hotel_700 W. Convention Way_A...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>-117.919050</td>\n",
" <td>33.799591</td>\n",
" <td>Starbucks - CA - Anaheim 00394</td>\n",
" <td>Hilton - Anaheim Hotel Lobby_777 Convention Wa...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>-117.914482</td>\n",
" <td>33.836449</td>\n",
" <td>Starbucks - CA - Anaheim 00395</td>\n",
" <td>Lincoln & Anaheim, Anaheim_110 West Lincoln A...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>-117.751970</td>\n",
" <td>33.868545</td>\n",
" <td>Starbucks - CA - Anaheim 00396</td>\n",
" <td>Vons-Anaheim Hills #2216_8010 E Santa Ana Cany...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>-117.792281</td>\n",
" <td>33.851333</td>\n",
" <td>Starbucks - CA - Anaheim Hills 00397</td>\n",
" <td>Vons-Anaheim #2103_5600 E Santa Ana Canyon Rd_...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>-117.788732</td>\n",
" <td>33.851543</td>\n",
" <td>Starbucks - CA - Anaheim Hills [W] 00398</td>\n",
" <td>Santa Ana Canyon & Imperial Hwy, An_5741 E. Sa...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>-117.791645</td>\n",
" <td>33.859876</td>\n",
" <td>Starbucks - CA - Anaheim Hills [W] 00399</td>\n",
" <td>Anaheim Hills_5655 E. La Palma_Anaheim Hills, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>-117.739566</td>\n",
" <td>33.863460</td>\n",
" <td>Starbucks - CA - Anaheim Hills [W] 00400</td>\n",
" <td>Sycamore Canyon Plaza - Anaheim Hil_721 South ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>-117.744356</td>\n",
" <td>33.863973</td>\n",
" <td>Starbucks - CA - Anaheim Hills [W] 00401</td>\n",
" <td>Weir Canyon & Monte Vista_8295 E. Monte Vista ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>-117.837392</td>\n",
" <td>33.855011</td>\n",
" <td>Starbucks - CA - Anaheim [WD] 00402</td>\n",
" <td>Tustin & 91 Fwy, Anaheim_1070 N. Tustin Avenue...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2101</th>\n",
" <td>-118.048673</td>\n",
" <td>33.967258</td>\n",
" <td>Starbucks - CA - Whittier [W] 02474</td>\n",
" <td>Washington & Lambert, Whittier_12376 Washingto...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2102</th>\n",
" <td>-118.027445</td>\n",
" <td>33.960010</td>\n",
" <td>Starbucks - CA - Whittier [W] 02475</td>\n",
" <td>Whittier Blvd & La Serna_15175 Whittier Blvd._...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2103</th>\n",
" <td>-117.232402</td>\n",
" <td>33.597763</td>\n",
" <td>Starbucks - CA - Wildomar 02476</td>\n",
" <td>Albertsons - Wildomar #6735_23805 Clinton Keit...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2104</th>\n",
" <td>-117.243930</td>\n",
" <td>33.596524</td>\n",
" <td>Starbucks - CA - Wildomar [WD] 02477</td>\n",
" <td>Clinton Keith Road & Hwy 15, Wildom_23823 Clin...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2105</th>\n",
" <td>-123.351699</td>\n",
" <td>39.404156</td>\n",
" <td>Starbucks - CA - Willits 02478</td>\n",
" <td>Safeway - Willits #965_845 Main Street_Willits...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2106</th>\n",
" <td>-122.212069</td>\n",
" <td>39.525796</td>\n",
" <td>Starbucks - CA - Willows [WD] 02479</td>\n",
" <td>I-5 & Wood - Willows_505 Humboldt Ave_Willows,...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2107</th>\n",
" <td>-117.092000</td>\n",
" <td>33.630200</td>\n",
" <td>Starbucks - CA - Winchester [WD] 02480</td>\n",
" <td>Winchester & Benton, Winchester_30628 Benton R...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2108</th>\n",
" <td>-122.807220</td>\n",
" <td>38.554863</td>\n",
" <td>Starbucks - CA - Windsor 02481</td>\n",
" <td>Safeway - Windsor #1434_9080 Brooks Rd_Windsor...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2109</th>\n",
" <td>-122.805973</td>\n",
" <td>38.548277</td>\n",
" <td>Starbucks - CA - Windsor [W] 02482</td>\n",
" <td>Windsor_8786 Lakewood Drive_Windsor, Californi...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2110</th>\n",
" <td>-118.603776</td>\n",
" <td>34.179226</td>\n",
" <td>Starbucks - CA - Woodland Hills 02483</td>\n",
" <td>Marriott Warner Center Lobby_21850 Oxnard St_W...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2111</th>\n",
" <td>-118.585977</td>\n",
" <td>34.166413</td>\n",
" <td>Starbucks - CA - Woodland Hills 02484</td>\n",
" <td>Target Woodland Hills T-288_20801 Ventura Blvd...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2112</th>\n",
" <td>-118.635710</td>\n",
" <td>34.158230</td>\n",
" <td>Starbucks - CA - Woodland Hills 02485</td>\n",
" <td>Vons-Woodland Hills #2031_23381 Mulholland Dr_...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2113</th>\n",
" <td>-118.597399</td>\n",
" <td>34.178463</td>\n",
" <td>Starbucks - CA - Woodland Hills [W] 02486</td>\n",
" <td>Canoga & Oxnard, Woodland Hills_5960 N. Canoga...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2114</th>\n",
" <td>-118.569982</td>\n",
" <td>34.171224</td>\n",
" <td>Starbucks - CA - Woodland Hills [W] 02487</td>\n",
" <td>Taft Plaza - Woodland Hills_20054 Ventura Blvd...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2115</th>\n",
" <td>-118.615569</td>\n",
" <td>34.168510</td>\n",
" <td>Starbucks - CA - Woodland Hills [W] 02488</td>\n",
" <td>Ventura & Shoup, Woodland Hills_22435 Ventura ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2116</th>\n",
" <td>-118.598845</td>\n",
" <td>34.188181</td>\n",
" <td>Starbucks - CA - Woodland Hills [W] 02489</td>\n",
" <td>Victory & Canoga - Woodland Hills_21504 Victor...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2117</th>\n",
" <td>-118.605856</td>\n",
" <td>34.167757</td>\n",
" <td>Starbucks - CA - Woodland Hills [W] 02490</td>\n",
" <td>Woodland Hills_5422 Topanga Canyon Blvd_Woodla...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2118</th>\n",
" <td>-121.746897</td>\n",
" <td>38.675424</td>\n",
" <td>Starbucks - CA - Woodland [W] 02491</td>\n",
" <td>421 Pioneer Avenue_421 Pioneer Avenue_Woodland...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2119</th>\n",