numpy.apply_along_axis - python examples

Here are the examples of the python api numpy.apply_along_axis taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

123 Examples 7

3 View Complete Implementation : ParticleSwarm.py
Copyright MIT License
Author : 100
    def _score(self, pos):
        """
        Applies objective function to all members of swarm

        :param pos: position matrix
        :return: score vector
        """
        return apply_along_axis(self._objective, 1, pos)

3 View Complete Implementation : apply.py
Copyright MIT License
Author : alvarob96
    def apply_raw(self):
        """ apply to the values as a numpy array """

        try:
            result = reduction.reduce(self.values, self.f, axis=self.axis)
        except Exception:
            result = np.apply_along_axis(self.f, self.axis, self.values)

        # TODO: mixed type case
        if result.ndim == 2:
            return self.obj._constructor(result,
                                         index=self.index,
                                         columns=self.columns)
        else:
            return self.obj._constructor_sliced(result,
                                                index=self.agg_axis)

3 View Complete Implementation : test_upfirdn.py
Copyright MIT License
Author : alvarob96
    def scrub(self, x, axis=-1):
        yr = np.apply_along_axis(upfirdn_naive, axis, x,
                                 self.h, self.up, self.down)
        y = upfirdn(self.h, x, self.up, self.down, axis=axis)
        dtypes = (self.h.dtype, x.dtype)
        if all(d == np.complex64 for d in dtypes):
            astert_equal(y.dtype, np.complex64)
        elif np.complex64 in dtypes and np.float32 in dtypes:
            astert_equal(y.dtype, np.complex64)
        elif all(d == np.float32 for d in dtypes):
            astert_equal(y.dtype, np.float32)
        elif np.complex128 in dtypes or np.complex64 in dtypes:
            astert_equal(y.dtype, np.complex128)
        else:
            astert_equal(y.dtype, np.float64)
        astert_allclose(yr, y)

3 View Complete Implementation : bicluster.py
Copyright MIT License
Author : alvarob96
    def _fit_best_piecewise(self, vectors, n_best, n_clusters):
        """Find the ``n_best`` vectors that are best approximated by piecewise
        constant vectors.

        The piecewise vectors are found by k-means; the best is chosen
        according to Euclidean distance.

        """
        def make_piecewise(v):
            centroid, labels = self._k_means(v.reshape(-1, 1), n_clusters)
            return centroid[labels].ravel()
        piecewise_vectors = np.apply_along_axis(make_piecewise,
                                                axis=1, arr=vectors)
        dists = np.apply_along_axis(norm, axis=1,
                                    arr=(vectors - piecewise_vectors))
        result = vectors[np.argsort(dists)[:n_best]]
        return result

3 View Complete Implementation : sparse_utils.py
Copyright GNU Affero General Public License v3.0
Author : cerebis
    @staticmethod
    def symm(_m):
        """
        Make a 4D COO matrix symmetric, all elements above and below the diagonal are included.
        Duplicate entries will be summed.

        :param _m: the NxNx2x2 matrix to make symmetric
        :return: a new symmetric version
        """
        # collect indices of diagonal elements along primary axes (0 and 1)
        ix = np.where(~np.apply_along_axis(lambda x: x[0]==x[1], 0, _m.coords))[0]
        # append every non-zero, non-diag coord and accompanying data to a new sparse object
        # and also perform the transpose (i,j), (k,l) -> (j,i), (l,k)
        _coords = np.hstack((_m.coords, np.apply_along_axis(Sparse4DAccameulator._flip, 0, _m.coords[:,ix])))
        _data = np.hstack((_m.data, _m.data[ix]))
        return sparse.COO(_coords, _data, shape=_m.shape, has_duplicates=True)

3 View Complete Implementation : measures.py
Copyright MIT License
Author : dluvizon
def valid_joints(y, min_valid=-1e6):
    def and_all(x):
        if x.all():
            return 1
        return 0

    return np.apply_along_axis(and_all, axis=1, arr=(y > min_valid))

3 View Complete Implementation : measures.py
Copyright MIT License
Author : dluvizon
def _valid_joints(y, min_valid=-1e6):
    def and_all(x):
        if x.all():
            return 1
        return 0

    return np.apply_along_axis(and_all, axis=1, arr=(y > min_valid))

3 View Complete Implementation : pose.py
Copyright MIT License
Author : dluvizon
def get_visible_joints(x, margin=0.0):

    visible = np.apply_along_axis(_func_and, axis=1, arr=(x > margin))
    visible *= np.apply_along_axis(_func_and, axis=1, arr=(x < 1 - margin))

    return visible

3 View Complete Implementation : geometry.py
Copyright MIT License
Author : eric-wieser
@converts_from_numpy(Point)
def numpy_to_point(arr):
	if arr.shape[-1] == 4:
		arr = arr[...,:-1] / arr[...,-1]

	if len(arr.shape) == 1:
		return Point(*arr)
	else:
		return np.apply_along_axis(lambda v: Point(*v), axis=-1, arr=arr)

3 View Complete Implementation : geometry.py
Copyright MIT License
Author : eric-wieser
@converts_from_numpy(Quaternion)
def numpy_to_quat(arr):
	astert arr.shape[-1] == 4

	if len(arr.shape) == 1:
		return Quaternion(*arr)
	else:
		return np.apply_along_axis(lambda v: Quaternion(*v), axis=-1, arr=arr)