本文整理汇总了Python中numpy.broadcast_arrays方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.broadcast_arrays方法的具体用法?Python numpy.broadcast_arrays怎么用?Python numpy.broadcast_arrays使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.broadcast_arrays方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: assert_same_as_ufunc
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def assert_same_as_ufunc(shape0, shape1, transposed=False, flipped=False):
# Broadcast two shapes against each other and check that the data layout
# is the same as if a ufunc did the broadcasting.
x0 = np.zeros(shape0, dtype=int)
# Note that multiply.reduce's identity element is 1.0, so when shape1==(),
# this gives the desired n==1.
n = int(np.multiply.reduce(shape1))
x1 = np.arange(n).reshape(shape1)
if transposed:
x0 = x0.T
x1 = x1.T
if flipped:
x0 = x0[::-1]
x1 = x1[::-1]
# Use the add ufunc to do the broadcasting. Since we're adding 0s to x1, the
# result should be exactly the same as the broadcasted view of x1.
y = x0 + x1
b0, b1 = broadcast_arrays(x0, x1)
assert_array_equal(y, b1)
示例2: test_writeable
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def test_writeable():
# broadcast_to should return a readonly array
original = np.array([1, 2, 3])
result = broadcast_to(original, (2, 3))
assert_equal(result.flags.writeable, False)
assert_raises(ValueError, result.__setitem__, slice(None), 0)
# but the result of broadcast_arrays needs to be writeable (for now), to
# preserve backwards compatibility
for results in [broadcast_arrays(original),
broadcast_arrays(0, original)]:
for result in results:
assert_equal(result.flags.writeable, True)
# keep readonly input readonly
original.flags.writeable = False
_, result = broadcast_arrays(0, original)
assert_equal(result.flags.writeable, False)
# regression test for GH6491
shape = (2,)
strides = [0]
tricky_array = as_strided(np.array(0), shape, strides)
other = np.zeros((1,))
first, second = broadcast_arrays(tricky_array, other)
assert_(first.shape == second.shape)
示例3: loglike
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def loglike(self, y, f):
r"""
Bernoulli log likelihood.
Parameters
----------
y: ndarray
array of 0, 1 valued integers of targets
f: ndarray
latent function from the GLM prior (:math:`\mathbf{f} =
\boldsymbol\Phi \mathbf{w}`)
Returns
-------
logp: ndarray
the log likelihood of each y given each f under this
likelihood.
"""
# way faster than calling bernoulli.logpmf
y, f = np.broadcast_arrays(y, f)
ll = y * f - softplus(f)
return ll
示例4: df
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def df(self, y, f):
r"""
Derivative of Bernoulli log likelihood w.r.t.\ f.
Parameters
----------
y: ndarray
array of 0, 1 valued integers of targets
f: ndarray
latent function from the GLM prior (:math:`\mathbf{f} =
\boldsymbol\Phi \mathbf{w}`)
Returns
-------
df: ndarray
the derivative :math:`\partial \log p(y|f) / \partial f`
"""
y, f = np.broadcast_arrays(y, f)
return y - expit(f)
示例5: __setitem__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def __setitem__(self, index, x):
# Process arrays from IndexMixin
i, j = self._unpack_index(index)
i, j = self._index_to_arrays(i, j)
if isspmatrix(x):
x = x.toarray()
# Make x and i into the same shape
x = np.asarray(x, dtype=self.dtype)
x, _ = np.broadcast_arrays(x, i)
if x.shape != i.shape:
raise ValueError("shape mismatch in assignment")
# Set values
for ii, jj, xx in zip(i.ravel(), j.ravel(), x.ravel()):
self._set_one(ii, jj, xx)
示例6: _prep_smooth
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def _prep_smooth(t, y, dy, span, t_out, span_out, period):
"""Private function to prepare & check variables for smooth utilities"""
# If period is provided, sort by phases. Otherwise sort by t
if period:
t = t % period
if t_out is not None:
t_out = t_out % period
t, y, dy = validate_inputs(t, y, dy, sort_by=t)
if span_out is not None:
if t_out is None:
raise ValueError("Must specify t_out when span_out is given")
if span is not None:
raise ValueError("Must specify only one of span, span_out")
span, t_out = np.broadcast_arrays(span_out, t_out)
indices = np.searchsorted(t, t_out)
elif span is None:
raise ValueError("Must specify either span_out or span")
else:
indices = None
return t, y, dy, span, t_out, span_out, indices
示例7: test_square_rescale_manual
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def test_square_rescale_manual(self):
points = np.array([(0,0), (0,100), (10,100), (10,0), (1, 5)], dtype=np.double)
points_rescaled = np.array([(0,0), (0,1), (1,1), (1,0), (0.1, 0.05)], dtype=np.double)
values = np.array([1., 2., -3., 5., 9.], dtype=np.double)
xx, yy = np.broadcast_arrays(np.linspace(0, 10, 14)[:,None],
np.linspace(0, 100, 14)[None,:])
xx = xx.ravel()
yy = yy.ravel()
xi = np.array([xx, yy]).T.copy()
for method in ('nearest', 'linear', 'cubic'):
msg = method
zi = griddata(points_rescaled, values, xi/np.array([10, 100.]),
method=method)
zi_rescaled = griddata(points, values, xi, method=method,
rescale=True)
assert_allclose(zi, zi_rescaled, err_msg=msg,
atol=1e-12)
示例8: test_square_rescale
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def test_square_rescale(self):
# Test barycentric interpolation on a rectangle with rescaling
# agaings the same implementation without rescaling
points = np.array([(0,0), (0,100), (10,100), (10,0)], dtype=np.double)
values = np.array([1., 2., -3., 5.], dtype=np.double)
xx, yy = np.broadcast_arrays(np.linspace(0, 10, 14)[:,None],
np.linspace(0, 100, 14)[None,:])
xx = xx.ravel()
yy = yy.ravel()
xi = np.array([xx, yy]).T.copy()
zi = interpnd.LinearNDInterpolator(points, values)(xi)
zi_rescaled = interpnd.LinearNDInterpolator(points, values,
rescale=True)(xi)
assert_almost_equal(zi, zi_rescaled)
示例9: broadcast_arrays
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def broadcast_arrays(ys, *args):
d[args] = tuple(tangent.unbroadcast_to(dy, numpy.shape(arg))
for arg, dy in zip(args, d[ys]))
示例10: test_sparse
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def test_sparse(self):
grid_full = mgrid[-1:1:10j, -2:2:10j]
grid_sparse = ogrid[-1:1:10j, -2:2:10j]
# sparse grids can be made dense by broadcasting
grid_broadcast = np.broadcast_arrays(*grid_sparse)
for f, b in zip(grid_full, grid_broadcast):
assert_equal(f, b)
示例11: assert_shapes_correct
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def assert_shapes_correct(input_shapes, expected_shape):
# Broadcast a list of arrays with the given input shapes and check the
# common output shape.
inarrays = [np.zeros(s) for s in input_shapes]
outarrays = broadcast_arrays(*inarrays)
outshapes = [a.shape for a in outarrays]
expected = [expected_shape] * len(inarrays)
assert_equal(outshapes, expected)
示例12: assert_incompatible_shapes_raise
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def assert_incompatible_shapes_raise(input_shapes):
# Broadcast a list of arrays with the given (incompatible) input shapes
# and check that they raise a ValueError.
inarrays = [np.zeros(s) for s in input_shapes]
assert_raises(ValueError, broadcast_arrays, *inarrays)
示例13: test_broadcast_kwargs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def test_broadcast_kwargs():
# ensure that a TypeError is appropriately raised when
# np.broadcast_arrays() is called with any keyword
# argument other than 'subok'
x = np.arange(10)
y = np.arange(10)
with assert_raises_regex(TypeError,
r'broadcast_arrays\(\) got an unexpected keyword*'):
broadcast_arrays(x, y, dtype='float64')
示例14: test_one_off
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def test_one_off():
x = np.array([[1, 2, 3]])
y = np.array([[1], [2], [3]])
bx, by = broadcast_arrays(x, y)
bx0 = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
by0 = bx0.T
assert_array_equal(bx0, bx)
assert_array_equal(by0, by)
示例15: test_broadcast_shape
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import broadcast_arrays [as 别名]
def test_broadcast_shape():
# broadcast_shape is already exercized indirectly by broadcast_arrays
assert_equal(_broadcast_shape(), ())
assert_equal(_broadcast_shape([1, 2]), (2,))
assert_equal(_broadcast_shape(np.ones((1, 1))), (1, 1))
assert_equal(_broadcast_shape(np.ones((1, 1)), np.ones((3, 4))), (3, 4))
assert_equal(_broadcast_shape(*([np.ones((1, 2))] * 32)), (1, 2))
assert_equal(_broadcast_shape(*([np.ones((1, 2))] * 100)), (1, 2))
# regression tests for gh-5862
assert_equal(_broadcast_shape(*([np.ones(2)] * 32 + [1])), (2,))
bad_args = [np.ones(2)] * 32 + [np.ones(3)] * 32
assert_raises(ValueError, lambda: _broadcast_shape(*bad_args))