本文整理汇总了Python中numpy.piecewise方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.piecewise方法的具体用法?Python numpy.piecewise怎么用?Python numpy.piecewise使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.piecewise方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: quintic_spline
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def quintic_spline(dx, dtype=np.float64):
def inner(x):
return 1 + x ** 3 / 12 * (-95 + 138 * x - 55 * x ** 2)
def middle(x):
return (x - 1) * (x - 2) / 24 * (-138 + 348 * x - 249 * x ** 2 + 55 * x ** 3)
def outer(x):
return (x - 2) * (x - 3) ** 2 / 24 * (-54 + 50 * x - 11 * x ** 2)
window = np.arange(-3, 4)
x = np.abs(dx - window)
result = np.piecewise(
x,
[x <= 1, (x > 1) & (x <= 2), (x > 2) & (x <= 3)],
[lambda x: inner(x), lambda x: middle(x), lambda x: outer(x)],
)
return result, window
示例2: B_0123
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def B_0123(x, der=0):
"""A quadratic B-spline function B(x | 0, 1, 2, 3)."""
x = np.atleast_1d(x)
conds = [x < 1, (x > 1) & (x < 2), x > 2]
if der == 0:
funcs = [lambda x: x*x/2.,
lambda x: 3./4 - (x-3./2)**2,
lambda x: (3.-x)**2 / 2]
elif der == 2:
funcs = [lambda x: 1.,
lambda x: -2.,
lambda x: 1.]
else:
raise ValueError('never be here: der=%s' % der)
pieces = np.piecewise(x, conds, funcs)
return pieces
示例3: minimum
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def minimum(self, ndim):
return -2.903534 * np.ones(ndim)
# class Simionescu(OptimizationTestFunction):
#
# def __init__(self):
# OptimizationTestFunction.__init__(self, mindim=2, maxdim=2, domain=np.array([-1.25, 1.25]))
#
# @staticmethod
# def function(x):
# rt = 1
# rs = 0.2
# n = 8
# return np.piecewise(x,
# [x[0]**2 + x[1]**2 <= (rt + rs*np.cos(n*np.arctan(x[0]/x[1])))**2,
# x[0]**2 + x[1]**2 > (rt + rs*np.cos(n*np.arctan(x[0]/x[1])))**2], [0.1*x[0]*x[1], 1])
#
#
# def minimum(self, ndim):
# assert ndim == 2
# return -0.84852813*np.ones(ndim)
示例4: get_random
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def get_random(self, size=None):
"""Draw a random number from the distribution.
If size is not None but an integer N, return an array of N numbers.
For the MultivariateNumericalDistribution, the PDF from which the
random numbers are drawn is approximated to be piecewise constant in
hypercubes around the points of the lattice spanned by the `xi`. A finer
lattice spacing will lead to a smoother distribution of random numbers
(but will also be slower).
"""
if size is None:
return self._get_random()
else:
return np.array([self._get_random() for i in range(size)])
示例5: get_frame
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def get_frame(self,frame_data):
# print(frame_data.size)
frame = np.rec.array(None, dtype=[('value', np.float16),('valid', np.bool_)], shape=(self.height, self.width))
frame.valid.fill(False)
frame.value.fill(0.)
# print(frame.size)
for datum in np.nditer(frame_data, flags=['zerosize_ok']):
# print(datum['y'])
ts_val = datum['ts']
f_data = frame[datum['y'], datum['x']]
f_data.value += 1
img = frame.value/20*255
img = img.astype('uint8')
# img = np.piecewise(img, [img <= 0, (img > 0) & (img < 255), img >= 255], [0, lambda x: x, 255])
# cv2.normalize(img,img,0,255,cv2.NORM_L1)
cv2.normalize(img,img,0,255,cv2.NORM_MINMAX)
img = cv2.flip(img, 1)
img = np.rot90(img)
# cv2.imshow('img_f', img)
# cv2.waitKey(0)
return img
示例6: get_frame_negative
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def get_frame_negative(self,frame_data):
# print(frame_data.size)
frame = np.rec.array(None, dtype=[('value', np.float16),('valid', np.bool_)], shape=(self.height, self.width))
frame.valid.fill(False)
frame.value.fill(0.)
# print(frame.size)
for datum in np.nditer(frame_data):
# print(datum['y'])
ts_val = datum['ts']
f_data = frame[datum['y'], datum['x']]
if(datum['p']== False):
f_data.value += 1
img = frame.value/20*255
img = img.astype('uint8')
# img = np.piecewise(img, [img <= 0, (img > 0) & (img < 255), img >= 255], [0, lambda x: x, 255])
cv2.normalize(img,img,0,255,cv2.NORM_MINMAX)
img = cv2.flip(img, 1)
img = np.rot90(img)
# cv2.imshow('img_neg', img)
# cv2.waitKey(0)
return img
示例7: _SetSize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def _SetSize(self):
"""Set minimum size (GB) necessary to achieve _iops level.
Rating performance levels as of May 2017, sources found below.
GCP: ratings from https://cloud.google.com/compute/docs/disks/. Storage can
go as high as 64TB per disk but IOPS maxes out at 30,000 iops/disk
or (1000GB).
AWS: ratings from
http://docs.aws.amazon.com/AWSEC2/latest/
UserGuide/EBSVolumeTypes.html#EBSVolumeTypes_gp2. Storage can go as
high as 16TiB per disk but IOPS maxes out at 10000 IOPS/volume size.
Reference gives volume size in GiB so converted to GB below.
"""
if self._provider == GCP:
self._size = min(int(math.ceil(self._iops / 30.0)), 30000 / 30)
elif self._provider == AWS:
value = self._iops
value = numpy.array(value)
self._size = int(
numpy.piecewise([value], [[value <= 100], [(value > 100) & (
value <= 9999)], [value > 9999]], [
lambda x: int(math.ceil(1.07374)),
lambda x: int(math.ceil(3 * value)),
lambda x: int(math.ceil(3579.855))]))
示例8: _SetCPUCount
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def _SetCPUCount(self):
"""Set cpu count.
GCP: ratings from
https://cloud.google.com/compute/docs/disks/performance#ssd-pd-performance
AWS: to achieve good performance on EBS, one needs to use an
EBS-optimized VM instance, and the smallest VM instance that can be EBS
optimized is *.large VM types (e.g., c4.large), those comes with 2 cores.
ratings from
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html
"""
if self._provider == GCP:
value = self._iops
self._cpu_count = int(
numpy.piecewise([value], [[value <= 15000], [
(value > 15000) & (value <= 25000)
], [value > 25000]], [lambda x: 1, lambda x: 16, lambda x: 32]))
elif self._provider == AWS:
self._cpu_count = 2
示例9: B_012
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def B_012(x):
""" A linear B-spline function B(x | 0, 1, 2)."""
x = np.atleast_1d(x)
return np.piecewise(x, [(x < 0) | (x > 2),
(x >= 0) & (x < 1),
(x >= 1) & (x <= 2)],
[lambda x: 0., lambda x: x, lambda x: 2.-x])
示例10: gaussian_square
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def gaussian_square(times: np.ndarray, amp: complex, center: float, square_width: float,
sigma: float, zeroed_width: Optional[float] = None) -> np.ndarray:
r"""Continuous gaussian square pulse.
Args:
times: Times to output pulse for.
amp: Pulse amplitude.
center: Center of the square pulse component.
square_width: Width of the square pulse component.
sigma: Standard deviation of Gaussian rise/fall portion of the pulse.
zeroed_width: Subtract baseline of gaussian square pulse
to enforce $\OmegaSquare(center \pm zeroed_width/2)=0$.
Raises:
PulseError: if zeroed_width is not compatible with square_width.
"""
square_start = center-square_width/2
square_stop = center+square_width/2
if zeroed_width:
if zeroed_width < square_width:
raise PulseError("zeroed_width cannot be smaller than square_width.")
gaussian_zeroed_width = zeroed_width-square_width
else:
gaussian_zeroed_width = None
funclist = [functools.partial(gaussian, amp=amp, center=square_start, sigma=sigma,
zeroed_width=gaussian_zeroed_width, rescale_amp=True),
functools.partial(gaussian, amp=amp, center=square_stop, sigma=sigma,
zeroed_width=gaussian_zeroed_width, rescale_amp=True),
functools.partial(constant, amp=amp)]
condlist = [times <= square_start, times >= square_stop]
return np.piecewise(times.astype(np.complex_), condlist, funclist)
示例11: backward_var
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def backward_var(self, grad, index, **kwargs):
a = self.variables[index]
return grad * np.piecewise(
a.data, [a.data < 0, a.data == 0, a.data > 0], [-1, np.nan, 1]
)
示例12: backward_var
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def backward_var(self, grad, index, **kwargs):
a = self.variables[index]
x = a.data
return np.pi * grad * np.piecewise(x, [x == 0, x != 0], [np.zeros_like, _dsinc])
示例13: __call__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def __call__(self, a):
self.variables = (a,)
return np.piecewise(
a.data, [a.data == 0, a.data != 0], [np.pi / 2, lambda x: np.arctan(1 / x)]
)
示例14: get_generator
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def get_generator(self):
return lambda x: numpy.piecewise(x, self.domain(x), [self._generators[j] for j in range(len(self._generators))] + [self.finals[-1]])
示例15: cdf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import piecewise [as 别名]
def cdf(self, x):
cdf0 = self.scipy_dist.cdf(0)
cdf = (self.scipy_dist.cdf(x) - cdf0)/(1-cdf0)
return np.piecewise(
np.asarray(x, dtype=float),
[x<0, x>=0],
[0., cdf]) # return 0 for negative x