Skip to content

Instantly share code, notes, and snippets.

View alexrutherford's full-sized avatar

Alex Rutherford alexrutherford

View GitHub Profile
bigSmallDict = (df['crime_category'].value_counts(normalize=True) > 0.05).to_dict()
# Make a dictionary saying if a category is > 5% of total or not
def assignOther(t):
if bigSmallDict[t]:
return t
else:
return 'other'
# Define a mini function that looks in the dictionary
# Grab data from https://drive.google.com/open?id=18IVEIp5qn4OnoWerC4f3e9MUwcBnM5U-
df = pd.read_excel('all_data_M_2018.xlsx')
df = df[df['o_group']=='detailed']
# Drop broad occupation groups
df = df[df['area']==99]
# Drop occupations for states, keep only those for the entire US
def rescale(p,rho = 0.001):
rhoAbs = abs(rho)
if abs(rho) > 10.001:
print('Error in rho ')
sys.exit(1)
out = np.zeros_like(p)
import sklearn.model_selection
import pandas as pd
df = pd.DataFrame(data={'A':[1,2,3],'B':[4,5,6],'C':[1,0,1]})
res = sklearn.model_selection.train_test_split(df[['A','B']],df['C'])
# Returns 4 things
res[0].shape
# (2,2)
# coding: utf-8
from __future__ import print_function
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import LSTM
from keras.optimizers import RMSprop
from keras.utils.data_utils import get_file
import numpy as np
import random
@alexrutherford
alexrutherford / plot_chloropleth.py
Last active August 13, 2016 16:46
Function to plot chloropleth map from shapefiles
import shapefile
import seaborn as sns
sf = shapefile.Reader("voronoi/voronoi.shp")
shapes = sf.shapes()
def getMid(bbox):
'''
Helper function to get midpoint of a bounding box
'''
Script to map points to administrative units defined by shapefiles
Requires shapefiles of administrative units from GADM (http://gadm.org/).
Alex Rutherford 2016
'''
import scipy.spatial
import shapefile,pickle,random,re,collections,csv
@alexrutherford
alexrutherford / grab_weather.py
Created February 22, 2016 16:32
Script to grab global weather data from Open Weather Map
import requests
import json,re,csv,re,sys
from secrets import *
import datetime,time
import logging
def makeFileName(n,log=False):
now=datetime.datetime.now()
timeStem='%d_%d_%d_%d_%d' % (now.year,now.month,now.day,now.hour,now.minute)
import pandas as pd
pandas.date_range("11:00", "21:30", freq="30min")
import requests
from secrets import key
def getCountry(s):
'''
Takes address string as input, queries Google geocoding API and returns country as ISO code
'''
tempUrl='http://maps.googleapis.com/maps/api/geocode/json?address=%s' % (s)
res=requests.get(tempUrl)