Skip to content

Instantly share code, notes, and snippets.

@wazaahhh
Last active October 7, 2015 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wazaahhh/5226efceedcaf74629d2 to your computer and use it in GitHub Desktop.
Save wazaahhh/5226efceedcaf74629d2 to your computer and use it in GitHub Desktop.
py:binning
def binning(x,y,bins,log_10=False,confinter=5):
'''makes a simple binning'''
x = np.array(x);y = np.array(y)
if isinstance(bins,int) or isinstance(bins,float):
bins = np.linspace(np.min(x)*0.9,np.max(x)*1.1,bins)
else:
bins = np.array(bins)
if log_10:
bins = bins[bins>0]
c = x > 0
x = x[c]
y = y[c]
bins = np.log10(bins)
x = np.log10(x)
y = np.log10(y)
Tbins = []
Median = []
Mean = []
Sigma =[]
Perc_Up = []
Perc_Down = []
Points=[]
for i,ix in enumerate(bins):
if i+2>len(bins):
break
c1 = x >= ix
c2 = x < bins[i+1]
c=c1*c2
if len(y[c])>0:
Tbins = np.append(Tbins,np.median(x[c]))
Median = np.append(Median,np.median(y[c]))
Mean = np.append(Mean,np.mean(y[c]))
Sigma = np.append(Sigma,np.std(y[c]))
Perc_Down = np.append(Perc_Down,np.percentile(y[c],confinter))
Perc_Up = np.append(Perc_Up,np.percentile(y[c],100 - confinter))
Points = np.append(Points,len(y[c]))
return {'bins' : Tbins,
'median' : Median,
'mean' : Mean,
'stdDev' : Sigma,
'percDown' :Perc_Down,
'percUp' :Perc_Up,
'nPoints' : Points}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment