def compute_precision_score_mapping(thresh, prec, score):
ind = np.argsort(thresh); # thresh, ind = torch.sort(thresh)
thresh = thresh[ind];
indexes = np.unique(thresh, return_index=True)[1]
indexes = np.sort(indexes);
thresh = thresh[indexes]
thresh = np.vstack((min(-1000, min(thresh) - 1), thresh[:, np.newaxis], max(1000, max(thresh) + 1)));
prec = prec[ind];
for i in xrange(1, len(prec)):
prec[i] = max(prec[i], prec[i - 1]);
prec = prec[indexes]
prec = np.vstack((prec[0], prec[:, np.newaxis], prec[-1]));
f = interp1d(thresh[:, 0], prec[:, 0])
val = f(score)
return val
python类sort()的实例源码
def argsort(a, axis=-1):
"""Returns the indices that would sort an array with a stable sorting.
Args:
a (cupy.ndarray): Array to sort.
axis (int or None): Axis along which to sort. Default is -1, which
means sort along the last axis. If None is supplied, the array is
flattened before sorting.
Returns:
cupy.ndarray: Array of indices that sort ``a``.
.. note::
For its implementation reason, ``cupy.argsort`` does not support
``kind`` and ``order`` parameters.
.. seealso:: :func:`numpy.argsort`
"""
return a.argsort(axis=axis)
def msort(a):
"""Returns a copy of an array sorted along the first axis.
Args:
a (cupy.ndarray): Array to be sorted.
Returns:
cupy.ndarray: Array of the same type and shape as ``a``.
.. note:
``cupy.msort(a)``, the CuPy counterpart of ``numpy.msort(a)``, is
equivalent to ``cupy.sort(a, axis=0)``.
.. seealso:: :func:`numpy.msort`
"""
# TODO(takagi): Support float16 and bool.
return sort(a, axis=0)
# TODO(okuta): Implement sort_complex
def create_component_sframe(g, baseid_name='page_id', layer_name='layer'):
"""Get component SFrame enriched with structural properties for each component"""
columns = g.vertices.column_names()
columns.remove('__id')
columns.remove('component_id')
# Append s to have unique column names (required by graphlab)
gb_dict = {c + 's': gl.aggregate.CONCAT(c) for c in columns}
gb_dict['nids'] = gl.aggregate.CONCAT('__id')
gb_dict['node_count'] = gl.aggregate.COUNT('__id')
comps = g.vertices.groupby('component_id', gb_dict)
comps['width'] = comps.apply(lambda x: len(np.unique(x[layer_name + 's'])))
comps['height'] = comps.apply(lambda x: len(np.unique(x[baseid_name + 's'])))
edges = g.edges.groupby('component_id', {'src': gl.aggregate.CONCAT('__src_id'),
'tgt': gl.aggregate.CONCAT('__dst_id')})
comps = comps.join(edges, 'component_id')
return comps.sort('node_count', False)
def test_multicollinearity(df, target_name, r2_threshold = 0.89):
'''Tests if any of the features could be predicted from others with R2 >= 0.89
input: dataframe, name of target (to exclude)
'''
r2s = pd.DataFrame()
for feature in df.columns.difference([target_name]):
model = sk.linear_model.Ridge()
model.fit(df[df.columns.difference([target_name,feature])], df[feature])
pos = np.in1d(model.coef_, np.sort(model.coef_)[-5:])
r2s = r2s.append(pd.DataFrame({'r2':sk.metrics.r2_score(df[feature],\
model.predict(df[df.columns.difference([target_name, feature])])),\
'predictors' : str(df.columns.difference([target_name, feature])[np.ravel(np.argwhere(pos == True))].tolist())}, index = [feature]))
print('Testing', feature)
print('-----------------')
if len(r2s[r2s['r2'] >= r2_threshold]) > 0:
print('Multicollinearity detected')
print(r2s[r2s['r2'] >= r2_threshold])
else:
print('No multicollinearity')
def wsparsify(w_gpu, percentage):
"""
Keeps only as many entries nonzero as specified by percentage.
"""
w = w_gpu.get()
vals = sort(w)[::-1]
idx = floor(prod(w.shape()) * percentage/100)
zw_gpu = cua.zeros_like(w_gpu) # gpu array filled with zeros
tw_gpu = cua.empty_like(w_gpu) # gpu array containing threshold
tw_gpu.fill(vals[idx])
w_gpu = cua.if_positive(w_gpu > tw_gpu, w_gpu, zw_gpu)
del zw_gpu
del tw_gpu
return w_gpu
def sparsify(x, percentage):
"""
Keeps only as many entries nonzero as specified by percentage.
Note that only the larges values are kept.
--------------------------------------------------------------------------
Usage:
Call: y = sparsify(x, percentage)
Input: x input ndarray x
percentage percentage of nonzero entries in y
Output: sparsified version of x
--------------------------------------------------------------------------
Copyright (C) 2011 Michael Hirsch
"""
vals = np.sort(x.flatten())[::-1]
idx = np.floor(np.prod(x.shape) * percentage/100)
x[x < vals[idx]] = 0
return x
def buckets(x, y, size=50):
assert len(x[0]) == len(y[0])
num_inputs = len(x)
samples = x + y
num_items = len(samples)
xy = zip(*samples)
xy.sort(key=lambda i: len(i[0]))
t_len = size
idx = 0
bucks = [[[]] for _ in range(num_items)]
for item in xy:
if len(item[0]) > t_len:
if len(bucks[0][idx]) > 0:
for buck in bucks:
buck.append([])
idx += 1
while len(item[0]) > t_len:
t_len += size
for i in range(num_items):
#print item[i]
bucks[i][idx].append(item[i])
return bucks[:num_inputs], bucks[num_inputs:]
def biased_out(prediction, bias):
out = []
b_pres = []
for pre in prediction:
b_pres.append(pre[:,0] - pre[:,1])
props = np.concatenate(b_pres)
props = np.sort(props)[::-1]
idx = int(bias*len(props))
if idx == len(props):
idx -= 1
th = props[idx]
print 'threshold: ', th, 1 / (1 + np.exp(-th))
for pre in b_pres:
pre[pre >= th] = 0
pre[pre != 0] = 1
out.append(pre)
return out
def ecdf(x):
"""Empirical cumulative distribution function
Given a 1D array of values, returns a function f(q) that outputs the
fraction of values less than or equal to q.
Parameters
----------
x : 1D array
values for which to compute CDF
Returns
----------
ecdf_fun: Callable[[float], float]
function that returns the value of the CDF at a given point
"""
xp = np.sort(x)
yp = np.arange(len(xp) + 1) / len(xp)
def ecdf_fun(q):
return yp[np.searchsorted(xp, q, side="right")]
return ecdf_fun
def calc_volume(roi):
# oar and ptv are lists using str(z) as keys
# each item is an ordered list of points representing a polygon
# polygon n is inside polygon n-1, then the current accumulated polygon is
# polygon n subtracted from the accumulated polygon up to and including polygon n-1
# Same method DICOM uses to handle rings and islands
volume = 0.
all_z_values = [round(float(z), 2) for z in list(roi)]
all_z_values = np.sort(all_z_values)
thicknesses = np.abs(np.diff(all_z_values))
thicknesses = np.append(thicknesses, np.min(thicknesses))
all_z_values = all_z_values.tolist()
for z in list(roi):
# z in coord will not necessarily go in order of z, convert z to float to lookup thickness
# also used to check for top and bottom slices, to add area of those contours
thickness = thicknesses[all_z_values.index(round(float(z), 2))]
shapely_roi = points_to_shapely_polygon(roi[z])
if shapely_roi:
volume += shapely_roi.area * thickness
return round(volume / 1000., 2)
def __init__(self,p=[-0.9594,4.294],pprior=None,
N=50,x=None,**kwargs):
f=lambda t,s: np.array([t-s*abs(t),t+s*abs(t)])
if pprior is None:
self.pprior={'p'+str(i) : f(t,10) for i,t in enumerate(p) }
self.label=self.pprior.keys()
self.ndim=len(p)
self.p=p
if x is None:
self.N=N
self.x = np.sort(10*np.random.rand(N))
else:
self.N=len(x)
self.x=x
self.y,self.yerr=self.data(**kwargs)
# As prior, we assume an 'uniform' prior (i.e. constant prob. density)
def test_encode_data_roundtrip():
minrand, maxrand = np.sort(np.random.randint(-427, 8848, 2))
testdata = np.round((np.sum(
np.dstack(
np.indices((512, 512),
dtype=np.float64)),
axis=2) / (511. + 511.)) * maxrand, 2) + minrand
baseval = -1000
interval = 0.1
rtripped = _decode(data_to_rgb(testdata.copy(), baseval, interval), baseval, interval)
assert testdata.min() == rtripped.min()
assert testdata.max() == rtripped.max()
def projsplx_multi(Y):
n, m = Y.shape
if n==1:
X = projsplx(Y)
else:
Y1 = -np.sort(-Y,axis=1)
tmpsum = np.zeros(n)
tmax = np.zeros(n)
bget = np.zeros(n, dtype=bool)
for ii in xrange(0,m-1):
active = (bget==False)
tmpsum[active] = tmpsum[active] + Y1[active][:,ii]
tmax[active] = (tmpsum[active] - 1)/(ii+1)
deactivate = (tmax>=Y1[:,ii+1]) & active
bget[deactivate] = True
active = (bget==False)
tmax[active] = (tmpsum[active] + Y1[active][:,m-1] - 1)/m
X = (Y.transpose() - tmax).transpose()
X[X<0.0] = 0.0
return X
def projsplx(y):
y1 = np.array(y, copy=True)
m = y1.shape[1]
bget = False
y1[0][::-1].sort()
tmpsum = 0
for ii in xrange(0,m-1):
tmpsum = tmpsum + y1[0][ii]
tmax = (tmpsum - 1)/ii
if tmax >= y1[0][ii+1]:
bget = True
break
if not bget:
tmax = (tmpsum + y1[0][m] -1)/m
y1 = y1 - tmax
y1[y1<0.0] = 0.0
return y1
def cond_projsplx_multi(Y,a_mat):
n, m = Y.shape
A = a_mat
s = -np.sort(-(A*Y),axis=1)
index = np.argsort(-(A*Y), axis=1)
tmpsum = np.zeros(n)
tmpsumdom = np.zeros(n)
bget = np.zeros(n, dtype=bool)
A_sort = A[np.arange(np.shape(A)[0])[:,np.newaxis], index]
cond_s = s/(A_sort**2)
tmax = np.zeros(n)
for ii in xrange(0,m-1):
active = (bget==False)
tmpsum[active] = tmpsum[active] + cond_s[active][:,ii]
tmpsumdom[active] = tmpsumdom[active]+ 1.0/A_sort[active][:,ii]**2
tmax[active] = (tmpsum[active] - 1)/tmpsumdom[active]
deactivate = (tmax >= s[:,ii+1]) & active
bget[deactivate] = True
active = (bget==False)
tmax[active] = (tmpsum[active] + cond_s[active][:,m-1] - 1)/(tmpsumdom[active]+1.0/(A_sort[active][:,m-1])**2)
X = (Y - np.matlib.repmat(tmax.reshape(n,1),1,m)*1.0/A)
X[X<0.0] = 0.0
X = X/A
return X
def get_symmetry_code_tri(pts):
if len(pts) == 1:
return '_s3()'
elif len(pts) == 3:
# Symmetry group [[a, a, b], [a, b, a], [b, a, a]].
# Find the equal value `a`.
tol = 1.0e-12
beta = pts[0] - pts[0][0]
ct = numpy.count_nonzero(abs(beta) < tol)
assert ct in [1, 2], beta
val = pts[0][0] if ct == 2 else pts[0][1]
return '_s21({:.15e})'.format(val)
# Symmetry group [[a, b, c], [c, a, b], ...].
assert len(pts) == 6
# Take the two largest value from a, b, c.
pt0 = numpy.sort(pts[0])
return '_s111({:.15e}, {:.15e})'.format(pt0[2], pt0[1])
def get_quadrature_points(order):
"""
Returns the quadrature points for Gauss-Lobatto quadrature
as a function of the order of the polynomial we want to
represent.
See: https://en.wikipedia.org/wiki/Gaussian_quadrature
"""
return np.sort(np.concatenate((np.array([-1,1]),
poly.basis(order).deriv().roots())))
def trataGroups(objeto):
current = list(filter(None.__ne__, objeto))
current = np.sort(current, axis=0)
for i in range(len(current[0])):
current_ = [j[i] for j in current]
mean_ = np.round(np.mean(current_, axis=0), 4)
deviation_ = np.round(np.std(current_, axis=0, ddof=1), 4)
return [mean_, deviation_]
def PA(samples, variables):
datasets = 5000
eig_vals = []
for i in range(datasets):
data = np.random.standard_normal((variables, samples))
cor_ = np.corrcoef(data)
eig_vals.append(np.sort(np.linalg.eig(cor_)[0])[::-1])
quantile = (np.round(np.percentile(eig_vals, 95.0, axis=0), 4))
mean_ = (np.round(np.mean(eig_vals, axis=0), 4))
return quantile