Python numpy.broadcast_arrays() Examples
The following are 30
code examples of numpy.broadcast_arrays().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: likelihoods.py From revrand with Apache License 2.0 | 6 votes |
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
Example #2
Source File: test_stride_tricks.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_stride_tricks.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_stride_tricks.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
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)
Example #5
Source File: likelihoods.py From revrand with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: likelihoods.py From revrand with Apache License 2.0 | 6 votes |
def df(self, y, f, n): r""" Derivative of Binomial 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}`) n: ndarray the total number of observations Returns ------- df: ndarray the derivative :math:`\partial \log p(y|f) / \partial f` """ y, f, n = np.broadcast_arrays(y, f, n) return y - expit(f) * n
Example #7
Source File: likelihoods.py From revrand with Apache License 2.0 | 6 votes |
def df(self, y, f, var): r""" Derivative of Gaussian 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}`) var: float, ndarray, optional The variance of the distribution, if not input, the initial value of variance is used. Returns ------- df: ndarray the derivative :math:`\partial \log p(y|f) / \partial f` """ var = self._check_param(var) y, f = np.broadcast_arrays(y, f) return (y - f) / var
Example #8
Source File: likelihoods.py From revrand with Apache License 2.0 | 6 votes |
def df(self, y, f): r""" Derivative of Poisson 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) if self.tranfcn == 'exp': return y - np.exp(f) else: return expit(f) * (y / safesoftplus(f) - 1)
Example #9
Source File: test_stride_tricks.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
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)
Example #10
Source File: utils.py From supersmoother with BSD 2-Clause "Simplified" License | 6 votes |
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
Example #11
Source File: compressed.py From Computable with MIT License | 6 votes |
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)
Example #12
Source File: test_ndgriddata.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #13
Source File: test_interpnd.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #14
Source File: test_api.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_broadcast_arrays(): # Test user defined dtypes a = np.array([(1, 2, 3)], dtype='u4,u4,u4') b = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype='u4,u4,u4') result = np.broadcast_arrays(a, b) assert_equal(result[0], np.array([(1, 2, 3), (1, 2, 3), (1, 2, 3)], dtype='u4,u4,u4')) assert_equal(result[1], np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype='u4,u4,u4'))
Example #15
Source File: test_swish.py From chainer with MIT License | 5 votes |
def _broadcast_to(array, shape): if hasattr(numpy, 'broadcast_to'): return numpy.broadcast_to(array, shape) dummy = numpy.empty(shape, array.dtype) return numpy.broadcast_arrays(array, dummy)[0]
Example #16
Source File: decorrelated_batch_normalization.py From chainer with MIT License | 5 votes |
def _broadcast_to(array, shape): if hasattr(numpy, 'broadcast_to'): return numpy.broadcast_to(array, shape) else: # numpy 1.9 doesn't support broadcast_to method dummy = numpy.empty(shape) bx, _ = numpy.broadcast_arrays(array, dummy) return bx
Example #17
Source File: ltv.py From BAG_framework with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __call__(self, t, tau, debug=False): """Calculate h(t, tau). Compute h(t, tau), which is the output at t subject to an impulse at time tau. standard numpy broadcasting rules apply. Parameters ---------- t : array-like the output time. tau : array-like the input impulse time. debug : bool True to print debug messages. Returns ------- val : :class:`numpy.ndarray` the time-varying impulse response evaluated at the given coordinates. """ # broadcast arguments to same shape t, tau = np.broadcast_arrays(t, tau) # compute impulse using efficient matrix multiply and numpy broadcasting. dt = t - tau zero_indices = (dt < 0) | (dt > self.k * self.tper) t_row = t.reshape((1, -1)) dt_row = dt.reshape((1, -1)) tmp = np.dot(self.hmat, np.exp(np.dot(self.n_col, dt_row))) * np.exp(np.dot(self.m_col, t_row)) result = np.sum(tmp, axis=0).reshape(dt.shape) # zero element such that dt < 0 or dt > k * T. result[zero_indices] = 0.0 if debug: self._print_debug_msg(result) # discard imaginary part return np.real(result)
Example #18
Source File: test_stride_tricks.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #19
Source File: test_stride_tricks.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #20
Source File: test_index_tricks.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #21
Source File: test_polyint.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def check_shape(interpolator_cls, x_shape, y_shape, deriv_shape=None, axis=0, extra_args={}): np.random.seed(1234) x = [-1, 0, 1, 2, 3, 4] s = list(range(1, len(y_shape)+1)) s.insert(axis % (len(y_shape)+1), 0) y = np.random.rand(*((6,) + y_shape)).transpose(s) # Cython code chokes on y.shape = (0, 3) etc, skip them if y.size == 0: return xi = np.zeros(x_shape) yi = interpolator_cls(x, y, axis=axis, **extra_args)(xi) target_shape = ((deriv_shape or ()) + y.shape[:axis] + x_shape + y.shape[axis:][1:]) assert_equal(yi.shape, target_shape) # check it works also with lists if x_shape and y.size > 0: interpolator_cls(list(x), list(y), axis=axis, **extra_args)(list(xi)) # check also values if xi.size > 0 and deriv_shape is None: bs_shape = y.shape[:axis] + (1,)*len(x_shape) + y.shape[axis:][1:] yv = y[((slice(None,),)*(axis % y.ndim)) + (1,)] yv = yv.reshape(bs_shape) yi, y = np.broadcast_arrays(yi, yv) assert_allclose(yi, y)
Example #22
Source File: smoother.py From supersmoother with BSD 2-Clause "Simplified" License | 5 votes |
def _validate_inputs(self, t, y, dy, presorted=False): t, y, dy = np.broadcast_arrays(t, y, dy) if presorted: self.isort = slice(None) elif hasattr(self, 'period') and self.period: self.isort = np.argsort(t % self.period) else: self.isort = np.argsort(t) return t[self.isort], y[self.isort], dy[self.isort]
Example #23
Source File: test_utils.py From supersmoother with BSD 2-Clause "Simplified" License | 5 votes |
def _variable_span_sum(a, span, subtract_mid=False): """variable-span sum, using multiple runs of _fixed_span_sum""" a, spans = np.broadcast_arrays(a, span) unique_spans, inv = np.unique(spans, return_inverse=True) results = [_fixed_span_sum(a, s, subtract_mid) for s in unique_spans] return np.asarray([results[j][i] for i, j in enumerate(inv)])
Example #24
Source File: test_lib.py From afnumpy with BSD 2-Clause "Simplified" License | 5 votes |
def test_broadcast_arrays(): # Currently arrayfire is missing support for int64 x2 = numpy.array([[1,2,3]], dtype=numpy.float32) y2 = numpy.array([[1],[2],[3]], dtype=numpy.float32) x1 = afnumpy.array(x2) y1 = afnumpy.array(y2) iassert(afnumpy.broadcast_arrays(x1, y1), numpy.broadcast_arrays(x2, y2)) x1 = afnumpy.array([2]) y1 = afnumpy.array(2) x2 = numpy.array([2]) y2 = numpy.array(2) iassert(afnumpy.broadcast_arrays(x1, y1), numpy.broadcast_arrays(x2, y2))
Example #25
Source File: utils.py From supersmoother with BSD 2-Clause "Simplified" License | 5 votes |
def validate_inputs(*arrays, **kwargs): """Validate input arrays This checks that - Arrays are mutually broadcastable - Broadcasted arrays are one-dimensional Optionally, arrays are sorted according to the ``sort_by`` argument. Parameters ---------- *args : ndarrays All non-keyword arguments are arrays which will be validated sort_by : array If specified, sort all inputs by the order given in this array. """ arrays = np.broadcast_arrays(*arrays) sort_by = kwargs.pop('sort_by', None) if kwargs: raise ValueError("unrecognized arguments: {0}".format(kwargs.keys())) if arrays[0].ndim != 1: raise ValueError("Input arrays should be one-dimensional.") if sort_by is not None: isort = np.argsort(sort_by) if isort.shape != arrays[0].shape: raise ValueError("sort shape must equal array shape.") arrays = tuple([a[isort] for a in arrays]) return arrays
Example #26
Source File: test_index_tricks.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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)
Example #27
Source File: basic.py From Computable with MIT License | 5 votes |
def hyp0f1(v, z): r"""Confluent hypergeometric limit function 0F1. Parameters ---------- v, z : array_like Input values. Returns ------- hyp0f1 : ndarray The confluent hypergeometric limit function. Notes ----- This function is defined as: .. math:: _0F_1(v,z) = \sum_{k=0}^{\inf}\frac{z^k}{(v)_k k!}. It's also the limit as q -> infinity of ``1F1(q;v;z/q)``, and satisfies the differential equation :math:``f''(z) + vf'(z) = f(z)`. """ v = atleast_1d(v) z = atleast_1d(z) v, z = np.broadcast_arrays(v, z) arg = 2 * sqrt(abs(z)) old_err = np.seterr(all='ignore') # for z=0, a<1 and num=inf, next lines num = where(z.real >= 0, iv(v - 1, arg), jv(v - 1, arg)) den = abs(z)**((v - 1.0) / 2) num *= gamma(v) np.seterr(**old_err) num[z == 0] = 1 den[z == 0] = 1 return num / den
Example #28
Source File: test_api.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_broadcast_arrays(): # Test user defined dtypes a = np.array([(1, 2, 3)], dtype='u4,u4,u4') b = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype='u4,u4,u4') result = np.broadcast_arrays(a, b) assert_equal(result[0], np.array([(1, 2, 3), (1, 2, 3), (1, 2, 3)], dtype='u4,u4,u4')) assert_equal(result[1], np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype='u4,u4,u4'))
Example #29
Source File: _util.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def _lazyselect(condlist, choicelist, arrays, default=0): """ Mimic `np.select(condlist, choicelist)`. Notice it assumes that all `arrays` are of the same shape, or can be broadcasted together. All functions in `choicelist` must accept array arguments in the order given in `arrays` and must return an array of the same shape as broadcasted `arrays`. Examples -------- >>> x = np.arange(6) >>> np.select([x <3, x > 3], [x**2, x**3], default=0) array([ 0, 1, 4, 0, 64, 125]) >>> _lazyselect([x < 3, x > 3], [lambda x: x**2, lambda x: x**3], (x,)) array([ 0., 1., 4., 0., 64., 125.]) >>> a = -np.ones_like(x) >>> _lazyselect([x < 3, x > 3], ... [lambda x, a: x**2, lambda x, a: a * x**3], ... (x, a), default=np.nan) array([ 0., 1., 4., nan, -64., -125.]) """ arrays = np.broadcast_arrays(*arrays) tcode = np.mintypecode([a.dtype.char for a in arrays]) out = _valarray(np.shape(arrays[0]), value=default, typecode=tcode) for index in range(len(condlist)): func, cond = choicelist[index], condlist[index] if np.all(cond is False): continue cond, _ = np.broadcast_arrays(cond, arrays[0]) temp = tuple(np.extract(cond, arr) for arr in arrays) np.place(out, cond, func(*temp)) return out
Example #30
Source File: test_stride_tricks.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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')