本文整理匯總了Python中numpy.moveaxis方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.moveaxis方法的具體用法?Python numpy.moveaxis怎麽用?Python numpy.moveaxis使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.moveaxis方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _nanquantile_ureduce_func
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False,
interpolation='linear'):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanpercentile for parameter usage
"""
if axis is None or a.ndim == 1:
part = a.ravel()
result = _nanquantile_1d(part, q, overwrite_input, interpolation)
else:
result = np.apply_along_axis(_nanquantile_1d, axis, a, q,
overwrite_input, interpolation)
# apply_along_axis fills in collapsed axis with results.
# Move that axis to the beginning to match percentile's
# convention.
if q.ndim != 0:
result = np.moveaxis(result, axis, 0)
if out is not None:
out[...] = result
return result
示例2: _pre_process
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def _pre_process(self, image: np.ndarray, shrink: float) -> torch.Tensor:
"""Takes N RGB image and performs and returns a set of bounding boxes as
detections
Args:
image (np.ndarray): shape [N, height, width, 3]
Returns:
torch.Tensor: shape [N, 3, height, width]
"""
assert image.dtype == np.uint8
height, width = image.shape[1:3]
image = image.astype(np.float32) - self.mean
image = np.moveaxis(image, -1, 1)
image = torch.from_numpy(image)
if self.max_resolution is not None:
shrink_factor = self.max_resolution / max((height, width))
if shrink_factor <= shrink:
shrink = shrink_factor
image = torch.nn.functional.interpolate(image, scale_factor=shrink)
image = image.to(self.device)
return image
示例3: test_errors
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def test_errors(self):
x = np.random.randn(1, 2, 3)
assert_raises_regex(ValueError, 'invalid axis .* `source`',
np.moveaxis, x, 3, 0)
assert_raises_regex(ValueError, 'invalid axis .* `source`',
np.moveaxis, x, -4, 0)
assert_raises_regex(ValueError, 'invalid axis .* `destination`',
np.moveaxis, x, 0, 5)
assert_raises_regex(ValueError, 'repeated axis in `source`',
np.moveaxis, x, [0, 0], [0, 1])
assert_raises_regex(ValueError, 'repeated axis in `destination`',
np.moveaxis, x, [0, 1], [1, 1])
assert_raises_regex(ValueError, 'must have the same number',
np.moveaxis, x, 0, [0, 1])
assert_raises_regex(ValueError, 'must have the same number',
np.moveaxis, x, [0, 1], [0])
示例4: __init__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def __init__(self, *dim):
"""
>>> Id(1)
Tensor(dom=Dim(1), cod=Dim(1), array=[1])
>>> list(Id(2).array.flatten())
[1.0, 0.0, 0.0, 1.0]
>>> Id(2).array.shape
(2, 2)
>>> list(Id(2, 2).array.flatten())[:8]
[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]
>>> list(Id(2, 2).array.flatten())[8:]
[0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
"""
dim = dim[0] if isinstance(dim[0], Dim) else Dim(*dim)
array = functools.reduce(
lambda a, x: np.tensordot(a, np.identity(x), 0)
if a.shape else np.identity(x), dim, np.array(1))
array = np.moveaxis(
array, [2 * i for i in range(len(dim))], list(range(len(dim))))
super().__init__(dim, dim, array)
示例5: n_moment
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def n_moment(x, counts, c, n):
x = np.squeeze(x)
if x.ndim is not 1:
raise ValueError("scale of x should be 1-dimensional")
if x.size not in counts.shape:
raise ValueError("operands could not be broadcast together with shapes %s %s" %(str(x.shape), str(counts.shape)))
if np.sum(counts)==0:
return 0
else:
if x.ndim == 1 and counts.ndim == 1:
return (np.sum((x-c)**n*counts) / np.sum(counts))**(1./n)
else:
if x.size in counts.shape:
dim_ = [i for i, v in enumerate(counts.shape) if v == x.size]
counts = np.moveaxis(counts, dim_, -1)
return (np.sum((x-c)**n*counts, axis=-1) / np.sum(counts, axis=-1))**(1./n)
示例6: _nanpercentile
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
interpolation='linear'):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanpercentile for parameter usage
"""
if axis is None or a.ndim == 1:
part = a.ravel()
result = _nanpercentile1d(part, q, overwrite_input, interpolation)
else:
result = np.apply_along_axis(_nanpercentile1d, axis, a, q,
overwrite_input, interpolation)
# apply_along_axis fills in collapsed axis with results.
# Move that axis to the beginning to match percentile's
# convention.
if q.ndim != 0:
result = np.moveaxis(result, axis, 0)
if out is not None:
out[...] = result
return result
示例7: augmentate_and_to_pytorch
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def augmentate_and_to_pytorch(item: {}):
res = augmentate(item)
return {'data': torch.from_numpy(np.moveaxis(res['data'].astype(np.float32) / 255., -1, 0)),
'target': torch.from_numpy(np.expand_dims(res['target'].astype(np.float32) / 255, axis=0))}
示例8: _render_to_rgb
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def _render_to_rgb(figure, close):
canvas = plt_backend_agg.FigureCanvasAgg(figure)
canvas.draw()
data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
w, h = figure.canvas.get_width_height()
image_hwc = data.reshape([h, w, 4])[..., :3]
image_chw = np.moveaxis(image_hwc, source=2, destination=0)
if close:
plt.close(figure)
return image_chw
示例9: _process_frame42
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def _process_frame42(frame):
frame = frame[34:34 + 160, :160]
# Resize by half, then down to 42x42 (essentially mipmapping). If
# we resize directly we lose pixels that, when mapped to 42x42,
# aren't close enough to the pixel boundary.
frame = cv2.resize(frame, (80, 80))
frame = cv2.resize(frame, (42, 42))
frame = frame.mean(2, keepdims=True)
frame = frame.astype(np.float32)
frame *= (1.0 / 255.0)
frame = np.moveaxis(frame, -1, 0)
return frame
示例10: test_extended_axis
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def test_extended_axis(self):
o = np.random.normal(size=(71, 23))
x = np.dstack([o] * 10)
assert_equal(np.percentile(x, 30, axis=(0, 1)), np.percentile(o, 30))
x = np.moveaxis(x, -1, 0)
assert_equal(np.percentile(x, 30, axis=(-2, -1)), np.percentile(o, 30))
x = x.swapaxes(0, 1).copy()
assert_equal(np.percentile(x, 30, axis=(0, -1)), np.percentile(o, 30))
x = x.swapaxes(0, 1).copy()
assert_equal(np.percentile(x, [25, 60], axis=(0, 1, 2)),
np.percentile(x, [25, 60], axis=None))
assert_equal(np.percentile(x, [25, 60], axis=(0,)),
np.percentile(x, [25, 60], axis=0))
d = np.arange(3 * 5 * 7 * 11).reshape((3, 5, 7, 11))
np.random.shuffle(d.ravel())
assert_equal(np.percentile(d, 25, axis=(0, 1, 2))[0],
np.percentile(d[:,:,:, 0].flatten(), 25))
assert_equal(np.percentile(d, [10, 90], axis=(0, 1, 3))[:, 1],
np.percentile(d[:,:, 1,:].flatten(), [10, 90]))
assert_equal(np.percentile(d, 25, axis=(3, 1, -4))[2],
np.percentile(d[:,:, 2,:].flatten(), 25))
assert_equal(np.percentile(d, 25, axis=(3, 1, 2))[2],
np.percentile(d[2,:,:,:].flatten(), 25))
assert_equal(np.percentile(d, 25, axis=(3, 2))[2, 1],
np.percentile(d[2, 1,:,:].flatten(), 25))
assert_equal(np.percentile(d, 25, axis=(1, -2))[2, 1],
np.percentile(d[2,:,:, 1].flatten(), 25))
assert_equal(np.percentile(d, 25, axis=(1, 3))[2, 2],
np.percentile(d[2,:, 2,:].flatten(), 25))
示例11: test_exceptions
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def test_exceptions(self):
# test axis must be in bounds
for ndim in [1, 2, 3]:
a = np.ones((1,)*ndim)
np.concatenate((a, a), axis=0) # OK
assert_raises(np.AxisError, np.concatenate, (a, a), axis=ndim)
assert_raises(np.AxisError, np.concatenate, (a, a), axis=-(ndim + 1))
# Scalars cannot be concatenated
assert_raises(ValueError, concatenate, (0,))
assert_raises(ValueError, concatenate, (np.array(0),))
# test shapes must match except for concatenation axis
a = np.ones((1, 2, 3))
b = np.ones((2, 2, 3))
axis = list(range(3))
for i in range(3):
np.concatenate((a, b), axis=axis[0]) # OK
assert_raises(ValueError, np.concatenate, (a, b), axis=axis[1])
assert_raises(ValueError, np.concatenate, (a, b), axis=axis[2])
a = np.moveaxis(a, -1, 0)
b = np.moveaxis(b, -1, 0)
axis.append(axis.pop(0))
# No arrays to concatenate raises ValueError
assert_raises(ValueError, concatenate, ())
示例12: test_move_to_end
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def test_move_to_end(self):
x = np.random.randn(5, 6, 7)
for source, expected in [(0, (6, 7, 5)),
(1, (5, 7, 6)),
(2, (5, 6, 7)),
(-1, (5, 6, 7))]:
actual = np.moveaxis(x, source, -1).shape
assert_(actual, expected)
示例13: test_move_new_position
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def test_move_new_position(self):
x = np.random.randn(1, 2, 3, 4)
for source, destination, expected in [
(0, 1, (2, 1, 3, 4)),
(1, 2, (1, 3, 2, 4)),
(1, -1, (1, 3, 4, 2)),
]:
actual = np.moveaxis(x, source, destination).shape
assert_(actual, expected)
示例14: test_preserve_order
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def test_preserve_order(self):
x = np.zeros((1, 2, 3, 4))
for source, destination in [
(0, 0),
(3, -1),
(-1, 3),
([0, -1], [0, -1]),
([2, 0], [2, 0]),
(range(4), range(4)),
]:
actual = np.moveaxis(x, source, destination).shape
assert_(actual, (1, 2, 3, 4))
示例15: test_move_multiples
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import moveaxis [as 別名]
def test_move_multiples(self):
x = np.zeros((0, 1, 2, 3))
for source, destination, expected in [
([0, 1], [2, 3], (2, 3, 0, 1)),
([2, 3], [0, 1], (2, 3, 0, 1)),
([0, 1, 2], [2, 3, 0], (2, 3, 0, 1)),
([3, 0], [1, 0], (0, 3, 1, 2)),
([0, 3], [0, 1], (0, 3, 1, 2)),
]:
actual = np.moveaxis(x, source, destination).shape
assert_(actual, expected)