Skip to content

Instantly share code, notes, and snippets.

from matplotlib.colors import LinearSegmentedColormap
bins = [0,1]
# Maps values to a bin.
# The mapped values must start at 0 and end at 1.
def bin_mapping(x):
for idx, bound in enumerate(bins):
if x < bound:
return idx / (len(bins) - 1.0)
@gebelo
gebelo / gist:902815fd76c080a4a668e96a93cb430d
Last active March 26, 2021 00:42
Converting Z-Score to Percentile in Python
def zptile(z_score):
return .5 * (math.erf(z_score / 2 ** .5) + 1)
zptile(0.95)
# excel says: 0.8289438737
0.8289438736915181
via: http://stackoverflow.com/questions/2782284/function-to-convert-a-z-score-into-a-percentage
@gebelo
gebelo / gist:4aee93ba6d622b42ced6953881310b14
Created June 7, 2016 18:28
Ugly but effective MySQL conversion of m/d/y string into yyyy-mm-dd date
SELECT dob, SUBSTRING_INDEX( `dob` , '/', 1 ) AS mo,
SUBSTRING_INDEX(SUBSTRING_INDEX( `dob` , '/', 2 ),'/',-1) AS dy,
SUBSTRING_INDEX(SUBSTRING_INDEX( `dob` , '/', 3 ),'/',-1) AS yr,
CONCAT(SUBSTRING_INDEX(SUBSTRING_INDEX( `dob` , '/', 3 ),'/',-1),"-",SUBSTRING_INDEX( `dob` , '/', 1 ),"-",SUBSTRING_INDEX(SUBSTRING_INDEX( `dob` , '/', 2 ),'/',-1)) AS dt
FROM table
"dob" "mo" "dy" "yr" "dt"
"7/19/1970" "7" "19" "1970" "1970-7-19"
@gebelo
gebelo / gist:5cc47579a2259d7e0d9953ddd3b0b2ea
Last active May 10, 2016 11:33
US Native American Population
2010-2014, ACS 5-year sample
Source: IPUMS.org
Nativer American Alone, by Tribal Identification
302: Apache 70,940
303: Blackfoot 28,256
304: Cherokee 279,721
305: Cheyenne 14,386
306: Chickasaw 23,110
307: Chippewa 114,423
@gebelo
gebelo / zoomer!
Created March 14, 2016 17:57
leaflet zoomer problem
The correct 'link':
<div class='map-navigation' data-zoom='12' data-position='<%=d.tractlat%>,<%=d.tractlon%>'>Zoom</div>
The 'link' that zoomed but then reloaded page:
<div class='map-navigation'><a href='' data-zoom='12' data-position='<%=d.tractlat%>,<%=d.tractlon%>'>Zoom</a></div>
The zoomer function:
document.querySelector('.map-navigation').onclick = function(abc) {
var pos = abc.target.getAttribute('data-position');
var zoom = abc.target.getAttribute('data-zoom');
Bonus example:
The IPUMS SDA query interface allows you to collapse variables into broader categories. I do this often with occupations -- individual occupations are usually too small to carry a meaningful sample size, but if you look at the occupational hierarchy in the docs, you can see how you might tile up individual occupations into broader but still meaningful categories.
So try this.
1) Pick any sample to query
2) put sex as your column