本文整理汇总了Python中numpy.maximum函数的典型用法代码示例。如果您正苦于以下问题:Python maximum函数的具体用法?Python maximum怎么用?Python maximum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了maximum函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: nms
def nms(dets, thresh):
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
ovr = inter / ((areas[i] + areas[order[1:]] - inter)+0.0000001)
inds = np.where(ovr <= thresh)[0]
order = order[inds + 1]
return keep
示例2: ParseMDH
def ParseMDH (self, n = 0, dry = False):
''' Parse one MDH entry '''
self.bm = struct.unpack_from ('2I', self.buf, n + 20)
if (self.bm[0] & SYNCDATA):
return n + MDHSIZE
if (self.bm[0] & NOISEADJSCAN):
self.lc = struct.unpack_from ('%dH' % 16, self.buf, n + 28) # loop counters
self.ch = struct.unpack_from ('H', self.buf, n + 124)
if (dry):
if (self.noisedims[0] == 0):
self.noisedims[0] = self.lc[0]
self.noisedims[1] = struct.unpack_from ('H', self.buf, n + 30)[0]
self.noisencolb = self.noisedims[md.COL] * self.ndds
self.noisedims[2:16] = np.array(self.lc[2:16])
else:
self.noisedims[2:16] = np.maximum (self.noisedims[2:16],self.lc[2:16])
return n + MDHSIZE
self.lc = struct.unpack_from ('%dH' % 16, self.buf, n + 28) # loop counters
self.ch = struct.unpack_from ('H', self.buf, n + 124)
if (dry):
if (self.dims[0] == 0):
self.dims[0] = self.lc[0]
self.dims[1] = struct.unpack_from ('H', self.buf, n + 30)[0]
self.ncolb = self.dims[md.COL] * self.ds
self.dims[2:16] = np.array(self.lc[2:16])
else:
self.dims[2:16] = np.maximum (self.dims[2:16],self.lc[2:16])
return n + MDHSIZE
示例3: autoRange
def autoRange(self,zmin=None,zmax=None,coordList=None):
""" determine X,Y,Z limits
"""
if coordList==None:
coordList = self.coordList
# autorange in X,Y
minX = 1.e50
maxX = -1.e50
minY = 1.e50
maxY = -1.e50
minZ = 1.e50
maxZ = -1.e50
for iCoord in coordList:
#if self.interpPresent:
# X,Y = self.interpGrids[iCoord]
# Z = self.interpValues[iCoord]
#else:
X,Y,Z = self.getXYZpoints(coordList=coordList)
minX = numpy.minimum(minX,X.min())
maxX = numpy.maximum(maxX,X.max())
minY = numpy.minimum(minY,Y.min())
maxY = numpy.maximum(maxY,Y.max())
minZ = numpy.minimum(minZ,Z.min())
maxZ = numpy.maximum(maxZ,Z.max())
if zmin!=None:
minZ = zmin
if zmax!=None:
maxZ = zmax
return minX,maxX,minY,maxY,minZ,maxZ
示例4: update_swe
def update_swe(self):
#--------------------------------------------------------
# Note: The Meteorology component uses air temperature
# to compute P_rain (precip that falls as liquid) and
# P_snow (precip that falls as snow or ice) separately.
# P_snow = (self.P * (self.T_air <= 0))
#----------------------------------------------------------
# Note: This method must be written to work regardless
# of whether P_rain and T are scalars or grids. (3/14/07)
#------------------------------------------------------------
# If P or T_air is a grid, then h_swe and h_snow are grids.
# This is set up in initialize_computed_vars().
#------------------------------------------------------------
#------------------------------------------------
# Increase snow water equivalent due to snowfall
#------------------------------------------------
# Meteorology and Channel components may have
# different time steps, but then self.P_snow
# will be a time-interpolated value.
#------------------------------------------------
dh1_swe = (self.P_snow * self.dt)
self.h_swe += dh1_swe
#------------------------------------------------
# Decrease snow water equivalent due to melting
# Note that SM depends partly on h_snow.
#------------------------------------------------
dh2_swe = self.SM * self.dt
self.h_swe -= dh2_swe
np.maximum(self.h_swe, np.float64(0), self.h_swe) # (in place)
示例5: _joint_probabilities
def _joint_probabilities(distances, desired_perplexity, verbose):
"""Compute joint probabilities p_ij from distances.
Parameters
----------
distances : array, shape (n_samples * (n_samples-1) / 2,)
Distances of samples are stored as condensed matrices, i.e.
we omit the diagonal and duplicate entries and store everything
in a one-dimensional array.
desired_perplexity : float
Desired perplexity of the joint probability distributions.
verbose : int
Verbosity level.
Returns
-------
P : array, shape (n_samples * (n_samples-1) / 2,)
Condensed joint probability matrix.
"""
# Compute conditional probabilities such that they approximately match
# the desired perplexity
distances = distances.astype(np.float32, copy=False)
conditional_P = _utils._binary_search_perplexity(
distances, None, desired_perplexity, verbose)
P = conditional_P + conditional_P.T
sum_P = np.maximum(np.sum(P), MACHINE_EPSILON)
P = np.maximum(squareform(P) / sum_P, MACHINE_EPSILON)
return P
示例6: shifted_corr
def shifted_corr(reference, image, displacement):
"""Calculate the correlation between the reference and the image shifted
by the given displacement.
Parameters
----------
reference : np.ndarray
image : np.ndarray
displacement : np.ndarray
Returns
-------
correlation : float
"""
ref_cuts = np.maximum(0, displacement)
ref = reference[ref_cuts[0]:, ref_cuts[1]:, ref_cuts[2]:]
im_cuts = np.maximum(0, -displacement)
im = image[im_cuts[0]:, im_cuts[1]:, im_cuts[2]:]
s = np.minimum(im.shape, ref.shape)
ref = ref[:s[0], :s[1], :s[2]]
im = im[:s[0], :s[1], :s[2]]
ref -= nanmean(ref.reshape(-1, ref.shape[-1]), axis=0)
ref = np.nan_to_num(ref)
im -= nanmean(im.reshape(-1, im.shape[-1]), axis=0)
im = np.nan_to_num(im)
assert np.all(np.isfinite(ref)) and np.all(np.isfinite(im))
corr = nanmean(
[old_div(np.sum(i * r), np.sqrt(np.sum(i * i) * np.sum(r * r))) for
i, r in zip(np.rollaxis(im, -1), np.rollaxis(ref, -1))])
return corr
示例7: _init_energy
def _init_energy(self, pc):
if pc is self._pc:
return
self.set_transform(self._t, pc)
self._pc = pc
self._res[:] = self.data[:, self._t] - self.mu[:]
self._V = np.maximum(self.offset + np.mean(self._res ** 2), SMALL)
self._res0[:] = self.data[:, self._t] - self.mu0
self._V0 = np.maximum(self.offset0 + np.mean(self._res0 ** 2), SMALL)
if self.use_derivatives:
# linearize the data wrt the transform parameters
# use the auxiliary array to save the current resampled data
self._aux[:] = self.data[:, self._t]
basis = np.eye(6)
for j in range(pc.size):
self.set_transform(self._t, pc + self.stepsize * basis[j])
self.A[:, j] = (self.data[:, self._t] - self._aux)\
/ self.stepsize
self.transforms[self._t].param = pc
self.data[:, self._t] = self._aux[:]
# pre-compute gradient and hessian of numerator and
# denominator
c = 2 / float(self.data.shape[0])
self._dV = c * np.dot(self.A.T, self._res)
self._dV0 = c * np.dot(self.A.T, self._res0)
self._H = c * np.dot(self.A.T, self.A)
示例8: _logpmf
def _logpmf(self, x, mu, alpha, p):
mu_p = mu ** (p - 1.)
a1 = np.maximum(np.nextafter(0, 1), 1 + alpha * mu_p)
a2 = np.maximum(np.nextafter(0, 1), mu + (a1 - 1.) * x)
logpmf_ = np.log(mu) + (x - 1.) * np.log(a2)
logpmf_ -= x * np.log(a1) + gammaln(x + 1.) + a2 / a1
return logpmf_
示例9: nms2d
def nms2d(boxes, overlap=0.3):
"""Compute the nms given a set of scored boxes,
as numpy array with 5 columns <x1> <y1> <x2> <y2> <score>
return the indices of the tubelets to keep
"""
if boxes.size == 0:
return np.array([],dtype=np.int32)
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
scores = boxes[:, 4]
areas = (x2-x1+1) * (y2-y1+1)
I = np.argsort(scores)
indices = np.zeros(scores.shape, dtype=np.int32)
counter = 0
while I.size > 0:
i = I[-1]
indices[counter] = i
counter += 1
xx1 = np.maximum(x1[i],x1[I[:-1]])
yy1 = np.maximum(y1[i],y1[I[:-1]])
xx2 = np.minimum(x2[i],x2[I[:-1]])
yy2 = np.minimum(y2[i],y2[I[:-1]])
inter = np.maximum(0.0, xx2 - xx1 + 1) * np.maximum(0.0, yy2 - yy1 + 1)
iou = inter / (areas[i] + areas[I[:-1]] - inter)
I = I[np.where(iou <= overlap)[0]]
return indices[:counter]
示例10: pdf
def pdf(self, t):
"""
Implementing the code distributed with Logan et al. 2013 using
the reparametrization given above.
Also, the constant theta is added as usual.
"""
t=np.maximum(t-self.theta, 1e-5) # absorbed into pdf
sqrt_t=np.sqrt(t)
# reparametrization
a=self.A/2.0
k=self.alpha-self.A/2.0
l=self.gamma
if self.A<1e-10: # this is the solution without starting-point variability
r=self.alpha/(np.sqrt(2*np.pi*(t**3)))*np.exp(- ((self.alpha-self.gamma*t)**2)/(2*t))
elif self.gamma<1e-10:
r=np.exp( -.5*( np.log(2)+np.log(np.pi)+np.log(t))
+ np.log( np.exp(-( (k-a)**2/(2*t)))-np.exp(-( (k+a)**2/(2*t) )) )
- np.log(2) - np.log(a) )
else:
r=np.exp( np.log( (np.exp(- (a-k+t*l)**2/(2*t) )-np.exp(- (a+k-t*l)**2/(2*t) ))/np.sqrt(2*np.pi*t)
+ np.exp(np.log(.5)+np.log(l))*( 2*pnormP( (-k+a)/sqrt_t + sqrt_t*l)-1
+ 2*pnormP( (k+a)/sqrt_t - sqrt_t*l)-1) )
- np.log(2) - np.log(a))
return np.maximum(0.0, np.where( np.isnan(r), 0, r))
示例11: cdf
def cdf(self,t):
t=np.maximum(t-self.theta, 1e-5) # absorbed into cdf
sqrt_t=np.sqrt(t)
# reparametrization
a=self.A/2.0
k=self.alpha-self.A/2.0
l=self.gamma
if self.A<1e-10: # this is the solution without starting-point variability
r=pnormP( (self.gamma*t-self.alpha)/sqrt_t)+np.exp(2*self.alpha*self.gamma)*pnormP(-(self.gamma*t+self.alpha)/(sqrt_t))
elif self.gamma<1e-10:
r=(( -(k+a)*(2*pnormP( (k+a)/sqrt_t)-1)
-(k-a)*(2*pnormP(-(k-a)/sqrt_t)-1))/(2*a)) \
+ (1 + np.exp(-.5*(k-a)**2/t - .5*np.log(2) - .5*np.log(np.pi) + .5*np.log(t) - np.log(a))
- np.exp(-.5*(k+a)**2/t - .5*np.log(2) - .5*np.log(np.pi) + .5*np.log(t) - np.log(a)))
else:
t1=np.exp( .5*np.log(t)-.5*np.log(2*np.pi) ) * ( np.exp( -((k-a-t*l)**2/t)/2.)
- np.exp( -((k+a-t*l)**2/t)/2.) ) # ok
t2=a+( np.exp(2*l*(k+a)+np.log(pnormP(-(k+a+t*l)/sqrt_t)))
- np.exp(2*l*(k-a)+np.log(pnormP(-(k-a+t*l)/sqrt_t))) )/(2*l) # ok
t4= (.5*(t*l-a-k+.5/l)) * ( 2*pnormP((k+a)/sqrt_t-sqrt_t*l)-1) \
+ .5*(k-a-t*l-.5/l)*( 2*pnormP((k-a)/sqrt_t-sqrt_t*l)-1)
r=(t4+t2+t1)/(2*a)
return np.minimum( np.maximum( 0., np.where( np.isnan(r), 0, r) ), 1.)
示例12: nms
def nms(boxes, threshold, method):
if boxes.size == 0:
return np.empty((0, 3))
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
s = boxes[:, 4]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
I = np.argsort(s)
pick = np.zeros_like(s, dtype=np.int16)
counter = 0
while I.size > 0:
i = I[-1]
pick[counter] = i
counter += 1
idx = I[0:-1]
xx1 = np.maximum(x1[i], x1[idx])
yy1 = np.maximum(y1[i], y1[idx])
xx2 = np.minimum(x2[i], x2[idx])
yy2 = np.minimum(y2[i], y2[idx])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
if method is 'Min':
o = inter / np.minimum(area[i], area[idx])
else:
o = inter / (area[i] + area[idx] - inter)
I = I[np.where(o <= threshold)]
pick = pick[0:counter]
return pick
示例13: test_elementwise_max_grad
def test_elementwise_max_grad(self, n, m, d, gc, dc):
go = np.random.rand(n, m, d).astype(np.float32)
X = np.random.rand(n, m, d).astype(np.float32)
Y = np.random.rand(n, m, d).astype(np.float32)
Z = np.random.rand(n, m, d).astype(np.float32)
mx = np.maximum(np.maximum(X, Y), Z)
inputs = [mx, go, X, Y, Z]
def max_grad_op(mx, go, X, Y, Z):
def mx_grad(a):
return go * (mx == a)
return [mx_grad(a) for a in [X, Y, Z]]
op = core.CreateOperator(
"MaxGradient",
["mx", "go", "X", "Y", "Z"],
["gX", "gY", "gZ"]
)
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=inputs,
reference=max_grad_op,
)
self.assertDeviceChecks(dc, op, inputs, [0, 1, 2])
示例14: prox_l1
def prox_l1(Y, alpha, n_orient):
"""proximity operator for l1 norm with multiple orientation support
L2 over orientation and L1 over position (space + time)
Example
-------
>>> Y = np.tile(np.array([1, 2, 3, 2, 0], dtype=np.float), (2, 1))
>>> Y = np.r_[Y, np.zeros_like(Y)]
>>> print Y
[[ 1. 2. 3. 2. 0.]
[ 1. 2. 3. 2. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
>>> Yp, active_set = prox_l1(Y, 2, 2)
>>> print Yp
[[ 0. 0.58578644 1.58578644 0.58578644 0. ]
[ 0. 0.58578644 1.58578644 0.58578644 0. ]]
>>> print active_set
[ True True False False]
"""
n_positions = Y.shape[0] // n_orient
norms = np.sqrt(np.sum((np.abs(Y) ** 2).T.reshape(-1, n_orient), axis=1))
# Ensure shrink is >= 0 while avoiding any division by zero
shrink = np.maximum(1.0 - alpha / np.maximum(norms, alpha), 0.0)
shrink = shrink.reshape(-1, n_positions).T
active_set = np.any(shrink > 0.0, axis=1)
shrink = shrink[active_set]
if n_orient > 1:
active_set = np.tile(active_set[:, None], [1, n_orient]).ravel()
Y = Y[active_set]
if len(Y) > 0:
for o in range(n_orient):
Y[o::n_orient] *= shrink
return Y, active_set
示例15: test_maximum_minimum_scalar
def test_maximum_minimum_scalar():
data1 = mx.symbol.Variable('data')
shape = (3, 4)
data_tmp1 = np.random.rand(3,4)
data_tmp1[:] = 2
arr_data1 = mx.nd.array(data_tmp1)
arr_grad1 = mx.nd.empty(shape)
test = mx.sym.maximum(data1,3) + mx.sym.maximum(9,data1) + mx.sym.minimum(5,data1) + mx.sym.minimum(data1,4)
exe_test = test.bind(mx.cpu(), args=[arr_data1], args_grad=[arr_grad1])
exe_test.forward()
out = exe_test.outputs[0].asnumpy()
npout = np.maximum(data_tmp1,3) + np.maximum(9,data_tmp1) + np.minimum(5,data_tmp1) + np.minimum(data_tmp1,4)
assert reldiff(out, npout) < 1e-6
out_grad = mx.nd.empty(shape)
out_grad[:] = 2
exe_test.backward(out_grad)
npout_grad = np.ones(shape)
npout_grad[:] = 2
mask1 = (data_tmp1 > 3).astype('float')
mask2 = (9 > data_tmp1).astype('float')
mask3 = (5 < data_tmp1).astype('float')
mask4 = (data_tmp1 < 4).astype('float')
npout_grad1 = npout_grad * mask1 + (npout_grad - npout_grad * mask2) + (npout_grad - npout_grad * mask3) + npout_grad * mask4
assert reldiff(arr_grad1.asnumpy(), npout_grad1) < 1e-6