本文整理汇总了Python中numpy.ndindex方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.ndindex方法的具体用法?Python numpy.ndindex怎么用?Python numpy.ndindex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.ndindex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_dark_channel
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def get_dark_channel(I, w=15):
"""Get the dark channel prior in the (RGB) image data.
Parameters
-----------
I: an M * N * 3 numpy array containing data ([0, 255]) in the image where
M is the height, N is the width, 3 represents R/G/B channels.
w: window size
Return
-----------
An M * N array for the dark channel prior ([0, 255]).
"""
M, N, _ = I.shape
padded = np.pad(I, ((w // 2, w // 2), (w // 2, w // 2), (0, 0)), 'edge')
darkch = np.zeros((M, N), dtype=I.dtype)
for i, j in np.ndindex(darkch.shape):
# This is from equation 5 in the above mentioned dark channel paper
darkch[i, j] = np.min(padded[i:i + w, j:j + w, :])
return darkch
示例2: upscale
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def upscale(image, ratio):
"""
return upscaled image array
Arguments:
image -- a (H,W,C) numpy.ndarray
ratio -- scaling factor (>1)
"""
if not isinstance(image, np.ndarray):
raise ValueError('Expected ndarray')
if ratio < 1:
raise ValueError('Ratio must be greater than 1 (ratio=%f)' % ratio)
width = int(math.floor(image.shape[1] * ratio))
height = int(math.floor(image.shape[0] * ratio))
channels = image.shape[2]
out = np.ndarray((height, width, channels), dtype=np.uint8)
for x, y in np.ndindex((width, height)):
out[y, x] = image[int(math.floor(y / ratio)), int(math.floor(x / ratio))]
return out
示例3: boundaries
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def boundaries(self, boundaries):
del self._boundaries, self.labels
self._boundaries = []
self.labels = labels = []
for boundset in boundaries:
boundarray = numpy.asarray(boundset,dtype=coord_dtype, order='C')
db = numpy.diff(boundarray)
if (db <= 0).any():
raise ValueError('boundary set must be strictly monotonically increasing')
self._boundaries.append(boundarray)
self._boundlens = numpy.array([len(boundset) for boundset in self._boundaries], dtype=index_dtype)
self.ndim = len(self._boundaries)
self.nbins = numpy.multiply.accumulate([1] + [len(bounds)-1 for bounds in self._boundaries])[-1]
_boundaries = self._boundaries
binspace_shape = tuple(self._boundlens[:]-1)
for index in numpy.ndindex(binspace_shape):
bounds = [(_boundaries[idim][index[idim]], boundaries[idim][index[idim]+1]) for idim in range(len(_boundaries))]
labels.append(repr(bounds))
示例4: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def __init__(self, **kw):
super(Chess, self).__init__(**kw)
w, h = self.frame_size
self.grid_size = sx, sy = 10, 7
white_quads = []
black_quads = []
for i, j in np.ndindex(sy, sx):
q = [[j, i, 0], [j+1, i, 0], [j+1, i+1, 0], [j, i+1, 0]]
[white_quads, black_quads][(i + j) % 2].append(q)
self.white_quads = np.float32(white_quads)
self.black_quads = np.float32(black_quads)
fx = 0.9
self.K = np.float64([[fx*w, 0, 0.5*(w-1)],
[0, fx*w, 0.5*(h-1)],
[0.0,0.0, 1.0]])
self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
self.t = 0
示例5: call
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def call(self, inputs):
batch_shape = tf.shape(inputs)[:-1]
length = tf.shape(inputs)[-1]
ngram_range_counts = []
for n in range(self.minval, self.maxval):
# Reshape inputs from [..., length] to [..., 1, length // n, n], dropping
# remainder elements. Each n-vector is an ngram.
reshaped_inputs = tf.reshape(
inputs[..., :(n * (length // n))],
tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0))
# Count the number of times each ngram appears in the input. We do so by
# checking whether each n-vector in the input is equal to each n-vector
# in a Tensor of all possible ngrams. The comparison is batched between
# the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor
# of shape [..., input_dim**n, 1, n].
ngrams = tf.reshape(
list(np.ndindex((self.input_dim,) * n)),
[1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n])
is_ngram = tf.equal(
tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32),
axis=-1),
n)
ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1)
ngram_range_counts.append(ngram_counts)
return tf.concat(ngram_range_counts, axis=-1)
示例6: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def __init__(self, **kw):
super(Chess, self).__init__(**kw)
w, h = self.frame_size
self.grid_size = sx, sy = 10, 7
white_quads = []
black_quads = []
for i, j in np.ndindex(sy, sx):
q = [[j, i, 0], [j + 1, i, 0], [j + 1, i + 1, 0], [j, i + 1, 0]]
[white_quads, black_quads][(i + j) % 2].append(q)
self.white_quads = np.float32(white_quads)
self.black_quads = np.float32(black_quads)
fx = 0.9
self.K = np.float64([[fx * w, 0, 0.5 * (w - 1)],
[0, fx * w, 0.5 * (h - 1)],
[0.0, 0.0, 1.0]])
self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
self.t = 0
示例7: test_add_chemical_potential
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def test_add_chemical_potential(self):
"""Test adding a chemical potential."""
self.quad_ham_npc.add_chemical_potential(2.4)
combined_hermitian_part = self.quad_ham_npc.combined_hermitian_part
hermitian_part = self.quad_ham_npc.hermitian_part
want_combined = (self.combined_hermitian -
2.4 * numpy.eye(self.n_qubits))
want_hermitian = self.hermitian_mat
for i in numpy.ndindex(combined_hermitian_part.shape):
self.assertAlmostEqual(combined_hermitian_part[i],
want_combined[i])
for i in numpy.ndindex(hermitian_part.shape):
self.assertAlmostEqual(hermitian_part[i], want_hermitian[i])
self.assertAlmostEqual(2.4 + self.chemical_potential,
self.quad_ham_npc.chemical_potential)
示例8: test_ndindex
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def test_ndindex():
x = list(np.ndindex(1, 2, 3))
expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))]
assert_array_equal(x, expected)
x = list(np.ndindex((1, 2, 3)))
assert_array_equal(x, expected)
# Test use of scalars and tuples
x = list(np.ndindex((3,)))
assert_array_equal(x, list(np.ndindex(3)))
# Make sure size argument is optional
x = list(np.ndindex())
assert_equal(x, [()])
x = list(np.ndindex(()))
assert_equal(x, [()])
# Make sure 0-sized ndindex works correctly
x = list(np.ndindex(*[0]))
assert_equal(x, [])
示例9: _pool_patches
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def _pool_patches(a, k, strides, padding):
f = np.ones(k[1:] + [a.shape[-1]])
out_shape, src = _prepare_patches(a, f, strides, padding, "edge")
patches = np.empty(tuple(out_shape) + f.shape).astype(a.dtype)
s = (slice(None),)
e = (Ellipsis,)
en = (Ellipsis, np.newaxis)
for coord in np.ndindex(*out_shape[1:]):
pos = np.array(strides[1:]) * coord
patches[s + coord + e] = \
src[s + tuple(slice(*tpl) for tpl in zip(pos, pos + f.shape[:-1]))][en] * f
return patches
示例10: expected_forward_without_reduce
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def expected_forward_without_reduce(self, x_data, t_data, class_weight):
x = numpy.rollaxis(x_data, 1, x_data.ndim).reshape(
(t_data.size, x_data.shape[1]))
t = t_data.ravel()
loss_shape = x_data.shape[0:1] + x_data.shape[2:]
loss_expect = numpy.zeros(loss_shape, x_data.dtype)
for i, (ti, loss_idx) in enumerate(zip(t, numpy.ndindex(*loss_shape))):
xi = x[i]
if ti == -1:
continue
log_z = numpy.ufunc.reduce(numpy.logaddexp, xi)
if class_weight is None:
loss_expect[loss_idx] = -(xi - log_z)[ti]
else:
loss_expect[loss_idx] = -(xi - log_z)[ti] * class_weight[ti]
return numpy.asarray(loss_expect, dtype=x.dtype)
示例11: check_forward
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def check_forward(self, y):
y = F.upsampling_2d(
self.pooled_y, self.indices, ksize=self.ksize,
stride=self.stride, outsize=self.in_shape[2:])
if isinstance(y.array, numpy.ndarray):
y = conv.im2col_cpu(
y.array, self.ksize, self.ksize, self.stride, self.stride,
0, 0)
else:
y = conv.im2col_gpu(
y.array, self.ksize, self.ksize, self.stride, self.stride,
0, 0)
for i in numpy.ndindex(y.shape):
n, c, ky, kx, oy, ox = i
up_y = y[n, c, ky, kx, oy, ox]
if ky * y.shape[3] + kx == self.indices[n, c, oy, ox]:
in_y = self.pooled_y.array[n, c, oy, ox]
testing.assert_allclose(in_y, up_y)
else:
testing.assert_allclose(up_y, 0)
示例12: test_idxiter
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def test_idxiter():
n_channels = data.shape[0]
# Upper-triangular part, including diag
idx0, idx1 = np.triu_indices(n_channels)
triu_indices = np.array([np.arange(idx0.size), idx0, idx1])
triu_indices2 = np.array(list(_idxiter(n_channels, include_diag=True)))
# Upper-triangular part, without diag
idx2, idx3 = np.triu_indices(n_channels, 1)
triu_indices_nodiag = np.array([np.arange(idx2.size), idx2, idx3])
triu_indices2_nodiag = np.array(list(_idxiter(n_channels,
include_diag=False)))
assert_almost_equal(triu_indices, triu_indices2.transpose())
assert_almost_equal(triu_indices_nodiag, triu_indices2_nodiag.transpose())
# Upper and lower-triangular parts, without diag
expected = [(i, j) for _, (i, j) in
enumerate(np.ndindex((n_channels, n_channels))) if i != j]
assert_equal(np.array([(i, j) for _, i, j in _idxiter(n_channels,
triu=False)]),
expected)
示例13: _get_Ndim_args_exprs_funcs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def _get_Ndim_args_exprs_funcs(order):
args = x, y = se.symbols('x y')
# Higher dimensional inputs
def f_a(index, _x, _y):
a, b, c, d = index
return _x**a + _y**b + (_x+_y)**-d
nd_exprs_a = np.zeros((3, 5, 1, 4), dtype=object, order=order)
for index in np.ndindex(*nd_exprs_a.shape):
nd_exprs_a[index] = f_a(index, x, y)
def f_b(index, _x, _y):
a, b, c = index
return b/(_x + _y)
nd_exprs_b = np.zeros((1, 7, 1), dtype=object, order=order)
for index in np.ndindex(*nd_exprs_b.shape):
nd_exprs_b[index] = f_b(index, x, y)
return args, nd_exprs_a, nd_exprs_b, f_a, f_b
示例14: test_Lambdify_Ndimensional_order_C
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def test_Lambdify_Ndimensional_order_C():
args, nd_exprs_a, nd_exprs_b, f_a, f_b = _get_Ndim_args_exprs_funcs(order='C')
lmb4 = se.Lambdify(args, nd_exprs_a, nd_exprs_b, order='C')
nargs = len(args)
inp_extra_shape = (3, 5, 4)
inp_shape = inp_extra_shape + (nargs,)
inp4 = np.arange(reduce(mul, inp_shape)*1.0).reshape(inp_shape, order='C')
out4a, out4b = lmb4(inp4)
assert out4a.ndim == 7
assert out4a.shape == inp_extra_shape + nd_exprs_a.shape
assert out4b.ndim == 6
assert out4b.shape == inp_extra_shape + nd_exprs_b.shape
raises(ValueError, lambda: (lmb4(inp4.T)))
for b, c, d in np.ndindex(inp_extra_shape):
_x, _y = inp4[b, c, d, :]
for index in np.ndindex(*nd_exprs_a.shape):
assert np.isclose(out4a[(b, c, d) + index], f_a(index, _x, _y))
for index in np.ndindex(*nd_exprs_b.shape):
assert np.isclose(out4b[(b, c, d) + index], f_b(index, _x, _y))
示例15: test_Lambdify_Ndimensional_order_F
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndindex [as 别名]
def test_Lambdify_Ndimensional_order_F():
args, nd_exprs_a, nd_exprs_b, f_a, f_b = _get_Ndim_args_exprs_funcs(order='F')
lmb4 = se.Lambdify(args, nd_exprs_a, nd_exprs_b, order='F')
nargs = len(args)
inp_extra_shape = (3, 5, 4)
inp_shape = (nargs,)+inp_extra_shape
inp4 = np.arange(reduce(mul, inp_shape)*1.0).reshape(inp_shape, order='F')
out4a, out4b = lmb4(inp4)
assert out4a.ndim == 7
assert out4a.shape == nd_exprs_a.shape + inp_extra_shape
assert out4b.ndim == 6
assert out4b.shape == nd_exprs_b.shape + inp_extra_shape
raises(ValueError, lambda: (lmb4(inp4.T)))
for b, c, d in np.ndindex(inp_extra_shape):
_x, _y = inp4[:, b, c, d]
for index in np.ndindex(*nd_exprs_a.shape):
assert np.isclose(out4a[index + (b, c, d)], f_a(index, _x, _y))
for index in np.ndindex(*nd_exprs_b.shape):
assert np.isclose(out4b[index + (b, c, d)], f_b(index, _x, _y))