Skip to content

Commit

Permalink
small fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sirrice committed Jan 9, 2012
1 parent 8322ce6 commit 6726d2f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
all: labs

labs: day0/README.md day1/README.md day2/README.md day3/regression.py day3/hypothesis_testing.py
python resources/markdown/markdown_headers.py day0/README.md day0/index.html
python resources/markdown/markdown_headers.py day1/README.md day1/index.html
python resources/markdown/markdown_headers.py day2/README.md day2/index.html
python resources/markdown/markdown_headers.py day4/README.md day4/index.html
python resources/hacco/hacco.py day3/regression.py -d day3/ #/tmp/dataiap_html
python resources/hacco/hacco.py day3/hypothesis_testing.py -d day3/ #/tmp/dataiap_html
cp -r ./day0 ./day1 ./day2 ./day3 ./day4 /tmp/dataiap_html/
python resources/markdown/markdown_headers.py day0/README.md /tmp/dataiap_html/day0/index.html
python resources/markdown/markdown_headers.py day1/README.md /tmp/dataiap_html/day1/index.html
python resources/markdown/markdown_headers.py day2/README.md /tmp/dataiap_html/day2/index.html
python resources/markdown/markdown_headers.py day4/README.md /tmp/dataiap_html/day4/index.html
python resources/hacco/hacco.py day3/regression.py -d /tmp/dataiap_html/day3/ #/tmp/dataiap_html
python resources/hacco/hacco.py day3/hypothesis_testing.py -d /tmp/dataiap_html/day3/ #/tmp/dataiap_html
echo "\n\nnow do: \n\tgit checkout gh-pages\n\tcp -r /tmp/dataiap_html/* .\n"
51 changes: 26 additions & 25 deletions day1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ The dataset is quite large, and processing the full dataset can be pretty slow.
with file(sys.argv[1], 'r') as f:
i = 0
for line in f:
if i % 1000 == 0:
print line[:-1]
i += 1
if i % 1000 == 0:
print line[:-1]
i += 1

The line `print line[:-1]` prints the entire line except its last
character to the screen. Why skip the last character? Because each
Expand All @@ -142,29 +142,30 @@ We will be analyzing Obama vs McCain data, so you can modify this code to create

We learned how to iterate and extract data from the dataset, and how to plot lines, so we will now combine the two to plot Obama's campaign contributions by date. We will compute the total amount of donations for each day, and use `matplotlib` to create the charts.

from collections import defaultdict
import matplotlib.pyplot as plt
from collections import defaultdict
import matplotlib.pyplot as plt
import csv

reader = csv.DictReader(open(sys.argv[1], 'r'))
obamadonations = defaultdict(lambda:0)
for row in reader:
name = row['cand_nm']
datestr = row['contb_receipt_dt']
amount = float(row['contb_receipt_amt'])
date = datetime.datetime.strptime(datestr, '%d-%b-%y')
if 'Obama' in name:
obamadonations[date] += amount
# dictionaries
sorted_by_date = sorted(obamadonations.items(), key=lambda (key,val): key)
xs,ys = zip(*sorted_by_date)
plt.plot(xs, ys, label='line 1')
plt.legend(loc='upper center', ncol = 4)
plt.savefig('/tmp/test.png', format='png')
reader = csv.DictReader(open(sys.argv[1], 'r'))
obamadonations = defaultdict(lambda:0)
for row in reader:
name = row['cand_nm']
datestr = row['contb_receipt_dt']
amount = float(row['contb_receipt_amt'])
date = datetime.datetime.strptime(datestr, '%d-%b-%y')
if 'Obama' in name:
obamadonations[date] += amount
# dictionaries
sorted_by_date = sorted(obamadonations.items(), key=lambda (key,val): key)
xs,ys = zip(*sorted_by_date)
plt.plot(xs, ys, label='line 1')
plt.legend(loc='upper center', ncol = 4)
plt.savefig('/tmp/test.png', format='png')

A few notes about the code

Expand Down

0 comments on commit 6726d2f

Please sign in to comment.