Advertisement
gronke

Bicycle Algorithm

Oct 7th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. import requests
  2. import lxml.html
  3. import time
  4. import numpy
  5. import unicodedata
  6. import pylab
  7. import collections
  8.  
  9. timestore = numpy.array([[0]])
  10. locastore = numpy.array([['dummy location']])
  11.  
  12. for year in range(12,14):
  13.     if year == 12:
  14.         mstart = 2
  15.         mlimit = 13
  16.     if year == 13:
  17.         mlimit = 10
  18.     ystring = str(year)
  19.     for month in range(mstart,mlimit,1):
  20.         if month == 2:
  21.             dlimit = 28
  22.         if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
  23.             dlimit = 32
  24.         if month == 4 or month == 6 or month == 9 or month == 7:
  25.             dlimit = 31  
  26.         for day in range(1,dlimit):
  27.             if month < 10:
  28.                 mstring = '0'+ str(month)
  29.             if month >= 10:
  30.                 mstring = str(month)
  31.             if day < 10:
  32.                 dstring = '0'+str(day)
  33.             if day >= 10:
  34.                 dstring = str(day)
  35.             req = requests.get('http://www.safety.ncsu.edu/newblotter.asp?NOTDTE='+mstring+'%2F'+dstring+'%2F'+ystring+'')
  36.             root = lxml.html.fromstring(req.text)
  37.             no_records = 'I am sorry'
  38.             if no_records in req.text:
  39.                 print ('NO RECORDS')
  40.             else:
  41.                 interest_words = ['bicycle']
  42.                 seen_with = 'stolen'
  43.                
  44.                 # Find last table
  45.                 tables = root.cssselect('table')
  46.                 table = tables[-1]
  47.                
  48.                 rows = table.cssselect('tr')
  49.                 # Exclude table header
  50.                 rows = rows[1:]
  51.                
  52.                 for row in rows:
  53.                     cells = row.cssselect('td')
  54.                     date_time = cells[1].text_content().strip()
  55.                     narrative = cells[5].text_content().strip()
  56.                     location = cells[4].text_content().strip()
  57.                     for word in interest_words:
  58.                         if word in narrative and seen_with in narrative:
  59.                             #Storing date and time
  60.                             date_time = unicodedata.normalize('NFKD', date_time).encode('ascii','ignore')
  61.                             if date_time.find(':') == 1:
  62.                                 times = time.strptime(date_time,"%I:%M  %p")
  63.                             hour, minute = times[3:5]
  64.                             minute = minute/60.
  65.                             hour += minute
  66.                             storearray = numpy.array([[hour]])
  67.                             timestore = numpy.concatenate((timestore,storearray))
  68.                            
  69.                             #Storing location
  70.                             location = unicodedata.normalize('NFKD', location).encode('ascii','ignore')
  71.                             locaarray = numpy.array([[location]])
  72.                             locastore = numpy.concatenate((locastore,locaarray))
  73.                            
  74.  
  75. pylab.figure(1)
  76. pylab.hist(timestore,bins=24, range=(0,24), normed=True, histtype='stepfilled')
  77. pylab.xlim(0,24)
  78. pylab.xticks(numpy.arange(0,24,4))
  79. pylab.title('Incidinces of Bike Theft from 02/01/12 to 09/31/13')
  80.  
  81. pylab.figure(2)
  82. counter = collections.Counter(locastore.ravel())
  83. ks = numpy.array([[]])
  84. vs = numpy.array([[]])
  85. for k, v in sorted(counter.iteritems(), key=lambda x:x[::-1]):
  86.     ks = numpy.concatenate([[k]])
  87.     vs = numpy.concatenate([[v]])
  88.  
  89. pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement