本文整理汇总了Python中pylab.median函数的典型用法代码示例。如果您正苦于以下问题:Python median函数的具体用法?Python median怎么用?Python median使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了median函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_to_results
def add_to_results(model, name):
df = getattr(model, name)
model.results['param'].append(name)
model.results['bias'].append(df['abs_err'].mean())
model.results['mae'].append((pl.median(pl.absolute(df['abs_err'].dropna()))))
model.results['mare'].append(pl.median(pl.absolute(df['rel_err'].dropna())))
model.results['pc'].append(df['covered?'].mean())
示例2: trace_fibers
def trace_fibers(flatim, params):
trace_im1 = pf.getdata(flatim)*0
imdat, imhead = pf.getdata(flatim), pf.getheader(flatim)
### separating fibers in first column and assigning fibers ids
print '\n\tSEARCHING FOR FIBERS BETWEEN x=0 & y=[' +str(params.LO_BUFFER) +':'+str(len(imdat[:,0])-params.HI_BUFFER)+']'
fiber_peaks_pix = pl.find( imdat[:,0] > pl.median(imdat[:,0]) )
fiber_peaks_pix,fiber_peaks_flx = sep_peaks( fiber_peaks_pix, imdat[:,0] )
if pl.median(fiber_peaks_pix[0])<=params.LO_BUFFER:
fiber_peaks_pix = fiber_peaks_pix[1:]
fiber_peaks_flx = fiber_peaks_flx[1:]
if (len(imdat[:,0])-pl.median(fiber_peaks_pix[-1]))<=params.HI_BUFFER:
fiber_peaks_pix = fiber_peaks_pix[:-1]
fiber_peaks_flx = fiber_peaks_flx[:-1]
print '\t --> FOUND ', len(fiber_peaks_pix), ' FIBER PEAKS'
### creating array for fibers
fibers0 = []
id_cnt = 1
for f in range(len(fiber_peaks_pix)):
while params.FIBERS_EXCLUDE.tolist().count(id_cnt)==1: id_cnt+=1
fibx,fiby = fiber_peaks_pix[f],fiber_peaks_flx[f]
peakx = fibx[ fiby.tolist().index(max(fiby)) ]
yrange = pl.arange( peakx-params.FIBER_WIDTH/2 , peakx+params.FIBER_WIDTH/2+1 )
fibers0.append( fiber(id_cnt, 0, yrange ))
id_cnt+=1
## TRACING FIBERS ALONG X-AXIS INDIVIDUALLY
for fib in fibers0:
for x in range(1,len(imdat)):
## FIRST, TAKE THE FLUXES IN THE PIXELS AT x
## THAT ENCOMPASSED THE PEAK AT x-1
fluxes = imdat[ fib.xy[-1][1] , x ]
## NEXT, FIND THE VERTICAL SHIFT TO CENTER ON
## THE PEAK FLUX AT x. MAXIMUM SHIFT IS DETERMINED
## FROM THE FIBER_WIDTH PARAMETER.
deltay = range( -len(fluxes)/2+1 , len(fluxes)/2+1 )[ fluxes.tolist().index(max(fluxes)) ]
## RECORD THE NEW Y-PIXELS THAT ARE CENTERD ON
## THE FIBER AT x.
fib.xy.append( [ x, fib.xy[-1][1]+deltay ] )
## FLAG PIXELS FOR FIBER IN FIRST-PASS TRACE IMAGE
trace_im1[fib.xy[-1][1],x] = fib.id
trc0 = 'trace_pass1.fits'
print '\n\tWRITING INITIAL TRACING TO ', trc0
try: pf.writeto(trc0, trace_im1, header=imhead)
except:
os.remove(trc0)
pf.writeto(trc0, trace_im1, header=imhead)
return fibers0
示例3: combine_output
def combine_output(J, T, model, dir, reps, save=False):
"""
Combine output on absolute error, relative error, csmf_accuracy, and coverage from from
multiple runs of validate_once. Either saves the output to the disk, or returns arays
for each.
"""
cause = pl.zeros(J*T, dtype='f').view(pl.recarray)
time = pl.zeros(J*T, dtype='f').view(pl.recarray)
abs_err = pl.zeros(J*T, dtype='f').view(pl.recarray)
rel_err = pl.zeros(J*T, dtype='f').view(pl.recarray)
coverage = pl.zeros(J*T, dtype='f').view(pl.recarray)
csmf_accuracy = pl.zeros(J*T, dtype='f').view(pl.recarray)
for i in range(reps):
metrics = pl.csv2rec('%s/metrics_%s_%i.csv' % (dir, model, i))
cause = pl.vstack((cause, metrics.cause))
time = pl.vstack((time, metrics.time))
abs_err = pl.vstack((abs_err, metrics.abs_err))
rel_err = pl.vstack((rel_err, metrics.rel_err))
coverage = pl.vstack((coverage, metrics.coverage))
csmf_accuracy = pl.vstack((csmf_accuracy, metrics.csmf_accuracy))
cause = cause[1:,]
time = time[1:,]
abs_err = abs_err[1:,]
rel_err = rel_err[1:,]
coverage = coverage[1:,]
csmf_accuracy = csmf_accuracy[1:,]
mean_abs_err = abs_err.mean(0)
median_abs_err = pl.median(abs_err, 0)
mean_rel_err = rel_err.mean(0)
median_rel_err = pl.median(rel_err, 0)
mean_csmf_accuracy = csmf_accuracy.mean(0)
median_csmf_accuracy = pl.median(csmf_accuracy, 0)
mean_coverage_bycause = coverage.mean(0)
mean_coverage = coverage.reshape(reps, T, J).mean(0).mean(1)
percent_total_coverage = (coverage.reshape(reps, T, J).sum(2)==3).mean(0)
mean_coverage = pl.array([[i for j in range(J)] for i in mean_coverage]).ravel()
percent_total_coverage = pl.array([[i for j in range(J)] for i in percent_total_coverage]).ravel()
models = pl.array([[model for j in range(J)] for i in range(T)]).ravel()
true_cf = metrics.true_cf
true_std = metrics.true_std
std_bias = metrics.std_bias
all = pl.np.core.records.fromarrays([models, cause[0], time[0], true_cf, true_std, std_bias, mean_abs_err, median_abs_err, mean_rel_err, median_rel_err,
mean_csmf_accuracy, median_csmf_accuracy, mean_coverage_bycause, mean_coverage, percent_total_coverage],
names=['model', 'cause', 'time', 'true_cf', 'true_std', 'std_bias', 'mean_abs_err', 'median_abs_err',
'mean_rel_err', 'median_rel_err', 'mean_csmf_accuracy', 'median_csmf_accuracy',
'mean_covearge_bycause', 'mean_coverage', 'percent_total_coverage'])
if save:
pl.rec2csv(all, '%s/%s_summary.csv' % (dir, model))
else:
return all
示例4: loadFile
def loadFile(objectFileName):
oimg = pyfits.open(objectFileName)
# Load the IFU data -- Row-stacked spectra
odata = oimg[1].data
oError = oimg[2].data
odata_dim = odata.shape
wcs = astWCS.WCS(objectFileName, extensionName=1)
owavelengthStartEnd = wcs.getImageMinMaxWCSCoords()[0:2]
fiberNumber = wcs.getImageMinMaxWCSCoords()[2:4]
owavelengthStep = oimg[1].header['CDELT1']
owavelengthRange = [owavelengthStartEnd[0] + i * owavelengthStep
for i in range(odata_dim[1])]
# Check to make sure we got it right
if not owavelengthRange[-1] == owavelengthStartEnd[-1]:
print 'The ending wavelenghts do not match... Exiting'
sys.exit(1)
else:
# make median sky
specs = pyl.array([flux for flux in odata])
skySpec = pyl.median(specs, axis=0)
RSS = []
for i in range(int(fiberNumber[1])):
#oflux = odata[i] - oskyflux
oflux = odata[i] - skySpec
oflux[pyl.isnan(oflux)] = 0.0
oErrorFlux = oError[i]
#oflux = odata[i]
# Mask out extreme values in spectrum
# Just because edges dodgy in efosc
med = pyl.median(oflux)
oflux[pyl.greater(abs(oflux), 10.0 * med)] = 0.0001
objSED = astSED.SED(wavelength=owavelengthRange, flux=oflux)
#skySED = astSED.SED(wavelength=owavelengthRange, flux=oskyflux)
skySED = astSED.SED(wavelength=owavelengthRange, flux=skySpec)
errSED = astSED.SED(wavelength=owavelengthRange, flux=oErrorFlux)
# make it > 0 everywhere
objSED.flux = objSED.flux - objSED.flux.min()
objSED.flux = objSED.flux / objSED.flux.max()
errSED.flux = errSED.flux - errSED.flux.min()
errSED.flux = errSED.flux / errSED.flux.max()
skySED.flux = skySED.flux - skySED.flux.min()
skySED.flux = skySED.flux / skySED.flux.max()
RSS.append({'object': objSED, 'sky': skySED, 'error': errSED})
return RSS
示例5: one_ci
def one_ci(v, ci, bootstraps):
v = pylab.array(v)
v = pylab.ma.masked_array(v,pylab.isnan(v)).compressed()
if v.size == 0:
return pylab.nan, 0, 0 #Nothing to compute
r = pylab.randint(v.size, size=(v.size, bootstraps))
booted_samp = pylab.array([pylab.median(v[r[:,n]]) for n in xrange(bootstraps)])
booted_samp.sort()
med = pylab.median(booted_samp)
idx_lo = int(bootstraps * ci/2.0)
idx_hi = int(bootstraps * (1.0-ci/2))
return med, med-booted_samp[idx_lo], booted_samp[idx_hi]-med
示例6: remove_discontinuity
def remove_discontinuity(value, xgap=10, ygap=200):
"""
Remove discontinuity (sudden jump) in a series of values.
Written by Denis, developed for LLC Fringe Counts data.
value : list or numpy.array
xgap : "width" of index of the list/array to adjust steps
ygap : threshold value to detect discontinuity
"""
difflist = pl.diff(value)
discont_index = pl.find(abs(difflist) > ygap)
if len(discont_index) == 0:
return value
else:
discont_index = pl.append(discont_index, len(difflist))
# find indice at discontinuities
discont = {"start": [], "end": []}
qstart = discont_index[0]
for i in range(len(discont_index) - 1):
if discont_index[i + 1] - discont_index[i] > xgap:
qend = discont_index[i]
discont["start"].append(qstart - xgap)
discont["end"].append(qend + xgap)
qstart = discont_index[i + 1]
# add offsets at discontinuities
result = pl.array(value)
for i in range(len(discont["end"])):
result[0 : discont["start"][i]] += result[discont["end"][i]] - result[discont["start"][i]]
# remove the median
result = result - pl.median(result)
return result
示例7: makePlots
def makePlots(self, ax, x, fNum, fColor, fMarker, feedstock):
x.getQuery()
if x.queryString.startswith('No'):
pass
elif x.queryString.startswith('FR'):
data = [1,1]
ax.plot([fNum]*2,[1,1],fColor,marker=fMarker,markersize=2)
else:
cur = self.conn.cursor()
print x.queryString
cur.execute(x.queryString)
#[all data]
data = cur.fetchall()
cur.close()
medVal = median(data)
maxVal = max(data)
minVal = min(data)
ax.plot([fNum],medVal,fColor,marker='_', markersize=7)
#Plot the max/min values
ax.plot([fNum]*2,[maxVal, minVal],fColor,marker=fMarker, markersize=2)
self.writeResults(feedstock, str(maxVal[0]), str(medVal), str(minVal[0]))
示例8: flow_rate_hist
def flow_rate_hist(sheets):
ant_rates = []
weights = []
for sheet in sheets:
ants, seconds, weight = flow_rate(sheet)
ant_rate = seconds / ants
#ant_rate = ants / seconds
ant_rates.append(ant_rate)
weights.append(float(weight))
#weights.append(seconds)
weights = pylab.array(weights)
weights /= sum(weights)
#print "ants per second"
print "seconds per ant"
mu = pylab.mean(ant_rates)
print "mean", pylab.mean(ant_rates)
wmean = pylab.average(ant_rates, weights=weights)
print "weighted mean", wmean
print "median", pylab.median(ant_rates)
print "std", pylab.std(ant_rates, ddof=1)
ant_rates = pylab.array(ant_rates)
werror = (ant_rates - mu) * weights
print "weighted std", ((sum(werror ** 2))) ** 0.5
print "weighted std 2", (pylab.average((ant_rates - mu)**2, weights=weights)) ** 0.5
pylab.figure()
pylab.hist(ant_rates)
pylab.savefig('ant_flow_rates.pdf', format='pdf')
pylab.close()
示例9: scatter_times
def scatter_times(name, sheets):
means = []
medians = []
delays = []
mean_points = []
med_points = []
for sheet, delay in sheets:
delays.append(delay)
times = get_times(sheet)
mean = pylab.mean(times)
median = pylab.median(times)
means.append(mean)
medians.append(median)
mean_points.append((mean, sheet))
med_points.append((median, sheet))
print "----------mean points-----------"
for mean, sheet in sorted(mean_points):
print mean, sheet
print "----------median points-----------"
for median, sheet in sorted(med_points):
print median, sheet
pylab.scatter(delays, means, color='r')
pylab.scatter(delays, medians, color='b')
print "show"
pylab.show()
示例10: pitch_estimate
def pitch_estimate(dw):
step = 8
wsize = 2048
wfun = pl.ones
wa = 3
lo, hi = 50, 700
hist_params = dict(bins=800, lw=0, range=[lo,hi], rwidth=1.0,
normed=True, log=True)
subplots = wplot.Subplots(6, 1,
yticks=[0,.1,.25,.5,1],
xlim=(120,240),
autoscalex_on=False)
for wfun in [pl.hanning, pl.hamming, pl.blackman, pl.bartlett, pl.ones]:
cc = chunker.Chunker(dw, window=wfun(wsize), step=step)
acs = [cc.ac for c in cc.chunks()]
pp = [chunker.find_peak(a, lo, hi, wa=wa) for a in acs]
mm = pl.median(pp)
subplots(
title='window: %s(%d) step=%s range=%s wa=%d' % (wfun.func_name, wsize, step, [lo,hi], wa),
xticks=[mm]+range(lo,hi+50,50))
subplots.next()
freq, bins, patches = pl.hist(pp, **hist_params)
print 'Ok!'
示例11: mare
def mare(model, data_type):
try:
pred = model.vars[data_type]['p_pred'].trace().mean(0)
except:
pred = 0
obs = model.get_data(data_type)['value']
mare = pl.median((abs(pred - obs)/obs)*100)
return mare
示例12: plot_histogram
def plot_histogram(histogram, html_writer, title='', max_pathway_length=8, xmin=None, xlim=20, error_bars=True, min_to_show=20, legend_loc='upper left'):
fig = pylab.figure()
pylab.hold(True)
reps = 1000
y_offset = 0
offset_step = 0.007
colors = {1:'r', 2:'orange', 3:'green', 4:'cyan', 5:'blue', 'Rest':'violet', 'Not first':'k--', 'No known regulation':'grey', 'Activated':'green', 'Inhibited':'r', 'Mixed regulation':'blue'}
for key, value in histogram.iteritems():
if len(value) >= min_to_show:
m = stats.cmedian(value)
sample_std = None
if error_bars:
sample_vals = []
i = 0
while i < reps:
samples = []
while len(samples) < len(value):
samples.append(random.choice(value))
sample_vals.append(pylab.median(samples))
i += 1
sample_std = pylab.std(sample_vals)
plotting.cdf(value, label='%s (med=%.1f, N=%d)' % \
(key, m, len(value)),
style=colors.get(key, 'grey'), std=sample_std, y_offset=y_offset)
y_offset += offset_step
xmin = -1 * xlim if xmin == None else xmin
pylab.xlim(xmin, xlim)
pylab.xlabel('Irreversability')
#pylab.xlabel('deltaG')
pylab.ylabel('Cumulative distribution')
legendfont = matplotlib.font_manager.FontProperties(size=11)
pylab.legend(loc=legend_loc, prop=legendfont)
pylab.title(title)
pylab.hold(False)
if 'Not first' in histogram:
print '%s, first vs. non-first ranksum test: ' % title + '(%f, %f)' % stats.ranksums(histogram[1], histogram['Not first'])
if 'Inhibited' in histogram:
print '%s, inhibited vs. non-regulated ranksum test: ' % title + '(%f, %f)' % stats.ranksums(histogram['Inhibited'], histogram['No known regulation'])
#for k1, h1 in histogram.iteritems():
# for k2, h2 in histogram.iteritems():
# print k1, k2, stats.ranksums(h1, h2)
return fig
示例13: get_stats
def get_stats(typeid,date):
global db
cur = db.cursor()
cur.execute("SELECT AVG(price),SUM(volremain),SUM(volenter) - SUM(volremain),bid FROM archive_market WHERE typeid = %s AND (reportedtime) :: date = %s GROUP BY orderid,bid", [typeid, date])
a = cur.fetchall()
avg_b = array(zeros(len(a)),dtype=float)
vol_b = array(zeros(len(a)),dtype=float)
move_b = array(zeros(len(a)),dtype=float)
avg_s = array(zeros(len(a)),dtype=float)
vol_s = array(zeros(len(a)),dtype=float)
move_s = array(zeros(len(a)),dtype=float)
x_s = 0
x_b = 0
for r in a:
if r[3]:
avg_b[x_b] = r[0]
vol_b[x_b] = r[1]
move_b[x_b] = r[2]
x_b += 1
else:
avg_s[x_s] = r[0]
vol_s[x_s] = r[1]
move_s[x_s] = r[2]
x_s += 1
avg_b.resize(x_b)
avg_s.resize(x_s)
vol_b.resize(x_b)
vol_s.resize(x_s)
move_b.resize(x_b)
move_s.resize(x_s)
b = (None,None,None)
s = (None,None,None)
try:
b = (pylab.median(avg_b), pylab.mean(vol_b), pylab.mean(move_b))
s = (pylab.median(avg_s), pylab.mean(vol_s), pylab.mean(move_s))
except:
return (b,b,b)
ret = ( ((b[0]+s[0])/2, (b[1]+s[1])/2, (b[2]+s[2])/2), b, s)
print ret
return ret
示例14: simulateGame
def simulateGame(numGames, tilePairs, gameType):
runTimeList = []
for i in range(numGames):
gameTime = gameType(tilePairs)
runTimeList.append(gameTime)
medTime = pylab.median(runTimeList)
meanTime = pylab.mean(runTimeList)
pylab.hist(runTimeList,[x*2 for x in range(400)])
print 'meanTime: ' + str(meanTime)
print 'medianTime: ' + str(medTime)
return meanTime, medTime
示例15: DFA
def DFA(data, npoints=None, degree=1, use_median=False):
"""
computes the detrended fluctuation analysis
returns the fluctuation F and the corresponding window length L
:args:
data (n-by-1 array): the data from which to compute the DFA
npoints (int): the number of points to evaluate; if omitted the log(n)
will be used
degree (int): degree of the polynomial to use for detrending
use_median (bool): use median instead of mean fluctuation
:returns:
F, L: the fluctuation F as function of the window length L
"""
# max window length: n/4
#0th: compute integral
integral = cumsum(data - mean(data))
#1st: compute different window lengths
n_samples = npoints if npoints is not None else int(log(len(data)))
lengths = sort(array(list(set(
logspace(2,log(len(data)/4.),n_samples,base=exp(1)).astype(int)
))))
#print lengths
all_flucs = []
used_lengths = []
for wlen in lengths:
# compute the fluctuation of residuals from a linear fit
# according to Kantz&Schreiber, ddof must be the degree of polynomial,
# i.e. 1 (or 2, if mean also counts? -> see in book)
curr_fluc = []
# rrt = 0
for startIdx in arange(0,len(integral),wlen):
pt = integral[startIdx:startIdx+wlen]
if len(pt) > 3*(degree+1):
resids = pt - polyval(polyfit(arange(len(pt)),pt,degree),
arange(len(pt)))
# if abs(wlen - lengths[0]) < -1:
# print resids[:20]
# elif rrt == 0:
# print "wlen", wlen, "l0", lengths[0]
# rrt += 1
curr_fluc.append(std(resids, ddof=degree+1))
if len(curr_fluc) > 0:
if use_median:
all_flucs.append(median(curr_fluc))
else:
all_flucs.append(mean(curr_fluc))
used_lengths.append(wlen)
return array(all_flucs), array(used_lengths)