-
Notifications
You must be signed in to change notification settings - Fork 17
/
Day_17_Midterm_with_Key.py
780 lines (344 loc) · 12.1 KB
/
Day_17_Midterm_with_Key.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
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
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <markdowncell>
# Working with Open Data Midterm (March 19, 2013)
#
# *There are **84** points in this exam, but the test will be scored out of a base total of **60 points**.*
#
# Name: ______________________________________
#
# Date: ______________________________________
#
#
# <headingcell level=1>
# 1. Open data and the census (Total: 7)
# <markdowncell>
# **1a.** <span style="font-weight:bold; color:red">[7]</span> What is **open data**? Use the US Census data set, specifically the Census Quickfacts (<http://quickfacts.census.gov/qfd/download_data.html>) that we've been studying in this course, to illustrate your definition of open data.
# <markdowncell>
# * A piece of content or data is open if anyone is free to use, reuse, and redistribute it — subject only, at most, to the requirement to attribute and/or share-alike. (3)
#
# * US Census data is free of copyright as a work of the US federal government and is free of charge. (2)
#
# * some illustration of how census data can be used (2)
# <codecell>
# <headingcell level=1>
# 2. CourtListener (Total: 7)
# <markdowncell>
# **2a.** <span style="font-weight:bold; color:red">[7]</span> What problems is http://www.courtlistener.com/ trying to solve? Why does CourtListener involve web scraping?
# <markdowncell>
# * 4 for description of CourtListener, what it aggregates, that it's an alert service, offers bulk downloads, etc
# * 3 for what scraping is, lack of standards around how court data can be presented in structured form, APIs for how to access court cases, or how alerts done.
# <codecell>
# <markdowncell>
#
#
#
#
#
#
#
#
#
#
#
# <headingcell level=1>
# 3. Project Questions (Total: 14)
# <markdowncell>
# **3a.** <span style="font-weight:bold; color:red">[2]</span> In a sentence or two, describe what are you aiming to accomplish in your project.
# <codecell>
# <markdowncell>
# **3b.** <span style="font-weight:bold; color:red">[8]</span> What open data set are you using in your project? How is that data open?
# <codecell>
# <markdowncell>
# **3c.** <span style="font-weight:bold; color:red">[4]</span> What is an immediate next step in your project?
# <codecell>
# <headingcell level=1>
# 4. Verifying population totals in Census (Total: 20)
# <markdowncell>
# Consider the following code to calculate the population of the US, state-like entities, and county-like entities.
#
# <codecell>
import pandas as pd
from pandas import Series, DataFrame
from itertools import islice
import datetime
from itertools import islice
import codecs
import re
import csv
import os
if os.getcwd() == '/home/picloud/notebook':
ON_PICLOUD = True
DATA_DIR = '/home/picloud/working-open-data/data'
PYDATA_DIR = '/home/picloud/pydata-book/'
else:
ON_PICLOUD = False
DATA_DIR = os.path.join(os.pardir, "data")
PYDATA_DIR = os.path.join(os.pardir, "pydata-book")
dataset_fname = os.path.join (DATA_DIR, "census/DataSet.txt")
datadict_fname = os.path.join (DATA_DIR, "census/DataDict.txt")
fips_fname = os.path.join (DATA_DIR, "census/FIPS_CountyName.txt")
assert os.path.exists(DATA_DIR)
assert os.path.exists(PYDATA_DIR)
assert os.path.exists(dataset_fname)
assert os.path.exists(datadict_fname)
assert os.path.exists(fips_fname)
# read in fips code
fips_file = codecs.open(fips_fname, encoding='iso-8859-1')
fips = dict()
for row in islice(fips_file, None):
fips[row[:5]] = row[6:-1]
# read in data set
ds_file = codecs.open(dataset_fname, encoding='iso-8859-1')
reader = csv.DictReader(ds_file)
dataset = dict([(row["fips"], row) for row in islice(reader, None)])
states_fips = sorted([k for k in fips.keys() if k[-3:] == '000' and k != '00000'])
# <markdowncell>
# And look at the following outputs to remind you of the content of the data set:
# <codecell>
!head $fips_fname
# <codecell>
dataset["00000"]["POP010210"]
# <codecell>
for f in states_fips[:5]:
print f, fips[f]
# <codecell>
list(islice(sorted(dataset.keys()),5))
# <codecell>
fips['06000']
# <headingcell level=2>
# Questions for Section 4
# <codecell>
# number of counties in CA
from collections import Counter
print Counter([k[0:2] for k in dataset.keys() if k[2:5] != '000'])['06']
# <markdowncell>
# **4a.** <span style="font-weight:bold; color:red">[6]</span> Explain how the following code (which produces `58`) shows that the number of counties in California is 58:
#
# from collections import Counter
# print Counter([k[0:2] for k in dataset.keys() if k[2:5] != '000'])['06']
#
# Include in your explaination:
#
# * what `Counter` does
# * the significance of `k[0:2]`
# * why do we have `k[2:5] != '000'`
# * why `['06']` is part of the code
# <markdowncell>
# * list comprehension of state prefixes by filtering on counties
# * Counter then takes tallies those state prefixes
# * state prefix from 1st 2 characters of fips code
# * counties don't end in '000'
# * California prefix is 06
# * what Counter does: Counter takes iterable and creats a count value for all values in iterable -- these values become keys
# <codecell>
# <markdowncell>
# **4b.** <span style="font-weight:bold; color:red">[4]</span> Explain what the following piece of code calculates (and how), and what the answer is:
#
# len([k for k in dataset.keys() if k[2:5] == '000' and k != '00000'])
# <markdowncell>
# * 2 for answer
# * 1 for explaining "grammar" of the statement -- e.g., `len` gives number of elements
# * 1 for semantics -- e.g., 50 states + DC = 51; exclude USA ('00000'); include state like entites (ends in '000'))
# <codecell>
# <markdowncell>
# **4c.** <span style="font-weight:bold; color:red">[5]</span> What is the answer to following?
#
# sum([int(dataset[k]["POP010210"]) for k in dataset.keys() if k[2:5] == '000' and k != '00000'])
#
#
# Explain how you came up with the answer.
# <markdowncell>
# * quote the exact pop of USA
# * get states by looking for '000' ending but exclude US ('00000')
# * dataset[k]['POP010210'] holds census pop for fips code k
# * need int() coercion
# * sum -- to add up all in list
# <codecell>
# <markdowncell>
# **4d.** <span style="font-weight:bold; color:red">[5]</span> What is the answer to following?
#
# sum([int(dataset[k]["POP010210"]) for k in dataset.keys() if k[2:5] != '000'])
#
# Explain how you came up with the answer.
# <markdowncell>
# * quote the exact pop of USA
# * get counties by looking for '000' ending but exclude US ('00000')
# * dataset[k]['POP010210'] holds census pop for fips code k
# * need int() coercion
# * sum -- to add up all in list
# * ok to say that logic of 4d is same as 4c except county -- no need to write it all out again
# <codecell>
# <headingcell level=1>
# 5. Slice notation (Total: 7)
# <markdowncell>
# Consider the following code using slice notation:
# <codecell>
import string
alphabet = string.lowercase
alphabet
print "alphabet:", alphabet
print "alphabet[0]:", alphabet[0]
print "alphabet[-1], alphabet[0:5], alphabet[-2:]:", alphabet[-1], alphabet[0:5], alphabet[-2:]
# <headingcell level=2>
# Calculate the following:
# <markdowncell>
# **5a.** <span style="font-weight:bold; color:red">[1]</span> `alphabet[5]`
# <codecell>
alphabet[5]
# <codecell>
# <markdowncell>
# **5b.** <span style="font-weight:bold; color:red">[1]</span> `alphabet[0:3]`
# <codecell>
alphabet[0:3]
# <codecell>
# <markdowncell>
# **5c.** <span style="font-weight:bold; color:red">[2]</span> `alphabet[1:4:2]`
# <codecell>
alphabet[1:4:2]
# <codecell>
# <markdowncell>
# **5d.** <span style="font-weight:bold; color:red">[1]</span> `alphabet[-6:]`
# <codecell>
alphabet[-6:]
# <codecell>
# <markdowncell>
# **5e.** <span style="font-weight:bold; color:red">[2]</span> `alphabet[-1:-3:-1]`
# <codecell>
alphabet[-1:-3:-1]
# <codecell>
# <headingcell level=1>
# 6. ndarray (Total: 3)
# <codecell>
a = array([0,1,2,3])
# <codecell>
a + 5
# <markdowncell>
# **6a.** <span style="font-weight:bold; color:red">[3]</span> Given that
#
# a = array([0,1,2,3])
#
# and that
#
# a + 5
#
# is:
#
# array([5, 6, 7, 8])
#
# what is:
#
# sum(2*a)
#
#
# <codecell>
sum(2*a)
# <codecell>
# <headingcell level=1>
# 7. Chemical elements DataFrame (Total: 18)
# <markdowncell>
# Consider the following `DataFrame` holding information about the lightest chemical elements
# <codecell>
# round off atomic weight
elements = DataFrame([{'number': 1, 'name': 'hydrogen', 'weight':1},
{'number': 2, 'name': 'helium', 'weight':4},
{'number': 3, 'name': 'lithium', 'weight':7},
{'number': 4, 'name': 'beryllium', 'weight':9},
{'number': 5, 'name': 'boron', 'weight':11},
{'number': 6, 'name': 'carbon', 'weight':12},
], index= ['H', 'He', 'Li', 'Be', 'B', 'C'])
# add group information
elements['group'] = Series([1, 18, 1, 2, 13, 14], index = ['H', 'He', 'Li', 'Be', 'B', 'C'])
elements
# <headingcell level=2>
# Calculate the following (showing how you arrive at your answer)
# <markdowncell>
# **7a.** <span style="font-weight:bold; color:red">[1]</span>
# <codecell>
len(elements.index)
# <codecell>
# <markdowncell>
# **7b.** <span style="font-weight:bold; color:red">[4]</span>
# <codecell>
elements[elements.number > 4]["weight"].sum()
# <codecell>
# <markdowncell>
# **7c.** <span style="font-weight:bold; color:red">[4]</span>
# <codecell>
set(elements[elements['group'] == 1].name)
# <codecell>
# <markdowncell>
# **7d.** <span style="font-weight:bold; color:red">[4]</span>
# <codecell>
elements.sort_index(by='weight')['number'][::-1][:2].sum()
# <codecell>
# <headingcell level=2>
# Now we add comments to DataFrame
# <codecell>
comments = Series(['first and most common element', 'the C in organic'], index=['H', 'C'])
# <codecell>
elements['comments'] = comments
elements
# <headingcell level=2>
# Calculate the following, again showing how you arrive at your answer
# <markdowncell>
# **7e.** <span style="font-weight:bold; color:red">[1]</span>
# <codecell>
elements.comments.dropna().count()
# <codecell>
# <markdowncell>
# **7f.** <span style="font-weight:bold; color:red">[4]</span>
# <codecell>
"".join(elements.comments.dropna().apply(lambda x: x[0]).values)
# <codecell>
# <headingcell level=3>
# Hints for Question 7f
# <codecell>
"".join(['a','b'])
# <codecell>
elements.number.apply(lambda x: 2*x)
# <codecell>
"".join(elements.number.apply(lambda x: str(2*x)).values)
# <headingcell level=1>
# 8. Matching the capitals to latitude, longitude pairs (Total: 4)
# <markdowncell>
# <img src="https://www.evernote.com/shard/s1/sh/b5dd80cb-740d-4aa2-a2a7-151fd0e3119b/77a2a008cbee7050a323c5302075ab1b/res/b777ee6b-b293-4e2c-b4e8-2313e6db7af0/Google_Maps-20130317-171357.jpg.jpg?resizeSmall&width=500">
# <markdowncell>
# **8a.** <span style="font-weight:bold; color:red">[4]</span> Match the following capital cities to their respective latitude, longitudes -- to each number, match a letter:
#
# 1. Ottawa, Canada: ____________
# 2. Moscow, Russia: ____________
# 3. Manila, Philippines: ____________
# 4. Buenos Aires, Argentina: ____________
#
# Lat/long:
#
# A (14.583333, 120.966667)
# B (45.420833, -75.69)
# C (-34.603333, -58.381667)
# D (55.75, 37.616667)
# <markdowncell>
# B, D, A, C
# <headingcell level=1>
# 9. datetime (Total: 4)
# <codecell>
import datetime
# <markdowncell>
# **9a.** <span style="font-weight:bold; color:red">[2]</span> What is
#
#
# <codecell>
(datetime.datetime.now() + datetime.timedelta(days=20)).month
# <markdowncell>
# 4
# <codecell>
# <markdowncell>
# **9b.** <span style="font-weight:bold; color:red">[2]</span>
# <markdowncell>
# What does the following code print:
#
# dt = datetime.datetime.fromtimestamp(24*60*60*5) - datetime.datetime.fromtimestamp(0)
# print dt.days
# <markdowncell>
# 5
# <codecell>