Python numpy.apply_along_axis() Examples

The following are 30 code examples for showing how to use numpy.apply_along_axis(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: models   Author: kipoi   File: model.py    License: MIT License 6 votes vote down vote up
def _get_bp_indexes_labranchor(self, soi):
        """
        Get indexes of branch point regions in given sequences.

        :param soi: batch of sequences of interest for introns (intron-3..intron+6)
        :return: array of predicted bp indexes
        """
        encoded = [onehot(str(seq)[self.acc_i - 70:self.acc_i]) for seq in np.nditer(soi)]
        labr_in = np.stack(encoded, axis=0)
        out = self.labranchor.predict_on_batch(labr_in)
        # for each row, pick the base with max branchpoint probability, and get its index
        max_indexes = np.apply_along_axis(lambda x: self.acc_i - 70 + np.argmax(x), axis=1, arr=out)
        # self.write_bp(max_indexes)
        return max_indexes

# TODO boilerplate
#    def write_bp(self, max_indexes):
#        max_indexes = [str(seq) for seq in np.nditer(max_indexes)]
#        with open(''.join([this_dir, "/../customBP/example_files/bp_idx_chr21_labr.txt"]), "a") as bp_idx_file:
#            bp_idx_file.write('\n'.join(max_indexes))
#            bp_idx_file.write('\n')
#            bp_idx_file.close() 
Example 2
Project: seizure-prediction   Author: MichaelHills   File: transforms.py    License: MIT License 6 votes vote down vote up
def apply(self, X, meta):
        def apply_one(x):
            x -= x.mean()
            z = np.cumsum(x)
            r = (np.maximum.accumulate(z) - np.minimum.accumulate(z))[1:]
            s = pd.expanding_std(x)[1:]

            # prevent division by 0
            s[np.where(s == 0)] = 1e-12
            r += 1e-12

            y_axis = np.log(r / s)
            x_axis = np.log(np.arange(1, len(y_axis) + 1))
            x_axis = np.vstack([x_axis, np.ones(len(x_axis))]).T

            m, b = np.linalg.lstsq(x_axis, y_axis)[0]
            return m

        return np.apply_along_axis(apply_one, -1, X) 
Example 3
Project: recruit   Author: Frank-qlu   File: test_interaction.py    License: Apache License 2.0 6 votes vote down vote up
def test_apply_along_axis_matrix():
    # this test is particularly malicious because matrix
    # refuses to become 1d
    # 2018-04-29: moved here from core.tests.test_shape_base.
    def double(row):
        return row * 2

    m = np.matrix([[0, 1], [2, 3]])
    expected = np.matrix([[0, 2], [4, 6]])

    result = np.apply_along_axis(double, 0, m)
    assert_(isinstance(result, np.matrix))
    assert_array_equal(result, expected)

    result = np.apply_along_axis(double, 1, m)
    assert_(isinstance(result, np.matrix))
    assert_array_equal(result, expected) 
Example 4
Project: recruit   Author: Frank-qlu   File: nanfunctions.py    License: Apache License 2.0 6 votes vote down vote up
def _nanmedian(a, axis=None, out=None, overwrite_input=False):
    """
    Private function that doesn't support extended axis or keepdims.
    These methods are extended to this function using _ureduce
    See nanmedian for parameter usage

    """
    if axis is None or a.ndim == 1:
        part = a.ravel()
        if out is None:
            return _nanmedian1d(part, overwrite_input)
        else:
            out[...] = _nanmedian1d(part, overwrite_input)
            return out
    else:
        # for small medians use sort + indexing which is still faster than
        # apply_along_axis
        # benchmarked with shuffled (50, 50, x) containing a few NaN
        if a.shape[axis] < 600:
            return _nanmedian_small(a, axis, out, overwrite_input)
        result = np.apply_along_axis(_nanmedian1d, axis, a, overwrite_input)
        if out is not None:
            out[...] = result
        return result 
Example 5
Project: recruit   Author: Frank-qlu   File: nanfunctions.py    License: Apache License 2.0 6 votes vote down vote up
def _nanquantile_unchecked(a, q, axis=None, out=None, overwrite_input=False,
                           interpolation='linear', keepdims=np._NoValue):
    """Assumes that q is in [0, 1], and is an ndarray"""
    # apply_along_axis in _nanpercentile doesn't handle empty arrays well,
    # so deal them upfront
    if a.size == 0:
        return np.nanmean(a, axis, out=out, keepdims=keepdims)

    r, k = function_base._ureduce(
        a, func=_nanquantile_ureduce_func, q=q, axis=axis, out=out,
        overwrite_input=overwrite_input, interpolation=interpolation
    )
    if keepdims and keepdims is not np._NoValue:
        return r.reshape(q.shape + k)
    else:
        return r 
Example 6
Project: recruit   Author: Frank-qlu   File: test_shape_base.py    License: Apache License 2.0 6 votes vote down vote up
def test_preserve_subclass(self):
        def double(row):
            return row * 2

        class MyNDArray(np.ndarray):
            pass

        m = np.array([[0, 1], [2, 3]]).view(MyNDArray)
        expected = np.array([[0, 2], [4, 6]]).view(MyNDArray)

        result = apply_along_axis(double, 0, m)
        assert_(isinstance(result, MyNDArray))
        assert_array_equal(result, expected)

        result = apply_along_axis(double, 1, m)
        assert_(isinstance(result, MyNDArray))
        assert_array_equal(result, expected) 
Example 7
Project: recruit   Author: Frank-qlu   File: test_shape_base.py    License: Apache License 2.0 6 votes vote down vote up
def test_empty(self):
        # can't apply_along_axis when there's no chance to call the function
        def never_call(x):
            assert_(False) # should never be reached

        a = np.empty((0, 0))
        assert_raises(ValueError, np.apply_along_axis, never_call, 0, a)
        assert_raises(ValueError, np.apply_along_axis, never_call, 1, a)

        # but it's sometimes ok with some non-zero dimensions
        def empty_to_1(x):
            assert_(len(x) == 0)
            return 1

        a = np.empty((10, 0))
        actual = np.apply_along_axis(empty_to_1, 1, a)
        assert_equal(actual, np.ones(10))
        assert_raises(ValueError, np.apply_along_axis, empty_to_1, 0, a) 
Example 8
Project: recruit   Author: Frank-qlu   File: test_rank.py    License: Apache License 2.0 6 votes vote down vote up
def test_rank_methods_frame(self):
        pytest.importorskip('scipy.stats.special')
        rankdata = pytest.importorskip('scipy.stats.rankdata')
        import scipy

        xs = np.random.randint(0, 21, (100, 26))
        xs = (xs - 10.0) / 10.0
        cols = [chr(ord('z') - i) for i in range(xs.shape[1])]

        for vals in [xs, xs + 1e6, xs * 1e-6]:
            df = DataFrame(vals, columns=cols)

            for ax in [0, 1]:
                for m in ['average', 'min', 'max', 'first', 'dense']:
                    result = df.rank(axis=ax, method=m)
                    sprank = np.apply_along_axis(
                        rankdata, ax, vals,
                        m if m != 'first' else 'ordinal')
                    sprank = sprank.astype(np.float64)
                    expected = DataFrame(sprank, columns=cols)

                    if (LooseVersion(scipy.__version__) >=
                            LooseVersion('0.17.0')):
                        expected = expected.astype('float64')
                    tm.assert_frame_equal(result, expected) 
Example 9
Project: recruit   Author: Frank-qlu   File: apply.py    License: Apache License 2.0 6 votes vote down vote up
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) 
Example 10
Project: boruta_py   Author: scikit-learn-contrib   File: boruta_py.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _add_shadows_get_imps(self, X, y, dec_reg):
        # find features that are tentative still
        x_cur_ind = np.where(dec_reg >= 0)[0]
        x_cur = np.copy(X[:, x_cur_ind])
        x_cur_w = x_cur.shape[1]
        # deep copy the matrix for the shadow matrix
        x_sha = np.copy(x_cur)
        # make sure there's at least 5 columns in the shadow matrix for
        while (x_sha.shape[1] < 5):
            x_sha = np.hstack((x_sha, x_sha))
        # shuffle xSha
        x_sha = np.apply_along_axis(self._get_shuffle, 0, x_sha)
        # get importance of the merged matrix
        imp = self._get_imp(np.hstack((x_cur, x_sha)), y)
        # separate importances of real and shadow features
        imp_sha = imp[x_cur_w:]
        imp_real = np.zeros(X.shape[1])
        imp_real[:] = np.nan
        imp_real[x_cur_ind] = imp[:x_cur_w]
        return imp_real, imp_sha 
Example 11
Project: sprocket   Author: k2kobayashi   File: parameterizer.py    License: MIT License 6 votes vote down vote up
def spc2npow(spectrogram):
    """Calculate normalized power sequence from spectrogram

    Parameters
    ----------
    spectrogram : array, shape (T, `fftlen / 2 + 1`)
        Array of spectrum envelope

    Return
    ------
    npow : array, shape (`T`, `1`)
        Normalized power sequence

    """

    # frame based processing
    npow = np.apply_along_axis(_spvec2pow, 1, spectrogram)

    meanpow = np.mean(npow)
    npow = 10.0 * np.log10(npow / meanpow)

    return npow 
Example 12
Project: rl_algorithms   Author: DanielTakeshi   File: plotting.py    License: MIT License 6 votes vote down vote up
def plot_cost_to_go_mountain_car(env, estimator, num_tiles=20):
    x = np.linspace(env.observation_space.low[0], env.observation_space.high[0], num=num_tiles)
    y = np.linspace(env.observation_space.low[1], env.observation_space.high[1], num=num_tiles)
    X, Y = np.meshgrid(x, y)
    Z = np.apply_along_axis(lambda _: -np.max(estimator.predict(_)), 2, np.dstack([X, Y]))

    fig = plt.figure(figsize=(10, 5))
    ax = fig.add_subplot(111, projection='3d')
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
                           cmap=matplotlib.cm.coolwarm, vmin=-1.0, vmax=1.0)
    ax.set_xlabel('Position')
    ax.set_ylabel('Velocity')
    ax.set_zlabel('Value')
    ax.set_title("Mountain \"Cost To Go\" Function")
    fig.colorbar(surf)
    plt.show() 
Example 13
Project: westpa   Author: westpa   File: assign.py    License: MIT License 6 votes vote down vote up
def assign(self, coords, mask=None, output=None):
        if output is None:
            output = numpy.zeros((len(coords),), dtype=index_dtype)

        if mask is None:
            mask = numpy.ones((len(coords),), dtype=numpy.bool_)
        else:
            mask = numpy.require(mask, dtype=numpy.bool_)

        coord_subset = coords[mask]
        fnvals = numpy.empty((len(coord_subset), len(self.functions)), dtype=index_dtype)
        for ifn, fn in enumerate(self.functions):
            rsl = numpy.apply_along_axis(fn,0,coord_subset)
            if rsl.ndim > 1:
                # this should work like a squeeze, unless the function returned something truly
                # stupid (e.g., a 3d array with at least two dimensions greater than 1), in which
                # case a broadcast error will occur
                fnvals[:,ifn] = rsl.flat
            else:
                fnvals[:,ifn] = rsl
        amask = numpy.require(fnvals.argmax(axis=1), dtype=index_dtype)
        output[mask] = amask
        return output 
Example 14
Project: NiaPy   Author: NiaOrg   File: test_algorithm.py    License: MIT License 6 votes vote down vote up
def init_pop_numpy(task, NP, **kwargs):
	r"""Custom population initialization function for numpy individual type.

	Args:
		task (Task): Optimization task.
		np (int): Population size.
		**kwargs (Dict[str, Any]): Additional arguments.

	Returns:
		Tuple[numpy.ndarray, numpy.ndarray[float]):
			1. Initialized population.
			2. Initialized populations fitness/function values.
	"""
	pop = full((NP, task.D), 0.0)
	fpop = apply_along_axis(task.eval, 1, pop)
	return pop, fpop 
Example 15
Project: NiaPy   Author: NiaOrg   File: algorithm.py    License: MIT License 6 votes vote down vote up
def defaultNumPyInit(task, NP, rnd=rand, **kwargs):
	r"""Initialize starting population that is represented with `numpy.ndarray` with shape `{NP, task.D}`.

	Args:
		task (Task): Optimization task.
		NP (int): Number of individuals in population.
		rnd (Optional[mtrand.RandomState]): Random number generator.
		kwargs (Dict[str, Any]): Additional arguments.

	Returns:
		Tuple[numpy.ndarray, numpy.ndarray[float]]:
			1. New population with shape `{NP, task.D}`.
			2. New population function/fitness values.
	"""
	pop = task.Lower + rnd.rand(NP, task.D) * task.bRange
	fpop = apply_along_axis(task.eval, 1, pop)
	return pop, fpop 
Example 16
Project: NiaPy   Author: NiaOrg   File: nmm.py    License: MIT License 6 votes vote down vote up
def initPop(self, task, NP, **kwargs):
		r"""Init starting population.

		Args:
			NP (int): Number of individuals in population.
			task (Task): Optimization task.
			rnd (mtrand.RandomState): Random number generator.
			kwargs (Dict[str, Any]): Additional arguments.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray[float]]:
				1. New initialized population.
				2. New initialized population fitness/function values.
		"""
		X = self.uniform(task.Lower, task.Upper, [task.D if NP is None or NP < task.D else NP, task.D])
		X_f = apply_along_axis(task.eval, 1, X)
		return X, X_f 
Example 17
Project: NiaPy   Author: NiaOrg   File: mbo.py    License: MIT License 6 votes vote down vote up
def evaluateAndSort(self, task, Butterflies):
		r"""Evaluate and sort the butterfly population.

		Args:
			 task (Task): Optimization task
			 Butterflies (numpy.ndarray): Current butterfly population.

		Returns:
			 numpy.ndarray: Tuple[numpy.ndarray, float, numpy.ndarray]:
				  1. Best butterfly according to the evaluation.
				  2. The best fitness value.
				  3. Butterfly population.
		"""
		Fitness = apply_along_axis(task.eval, 1, Butterflies)
		indices = argsort(Fitness)
		Butterflies = Butterflies[indices]
		Fitness = Fitness[indices]

		return Fitness, Butterflies 
Example 18
Project: NiaPy   Author: NiaOrg   File: cs.py    License: MIT License 6 votes vote down vote up
def emptyNests(self, pop, fpop, pa_v, task):
		r"""Empty ensts.

		Args:
			pop (numpy.ndarray): Current population
			fpop (numpy.ndarray[float]): Current population fitness/funcion values
			pa_v (): TODO.
			task (Task): Optimization task

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray[float]]:
				1. New population
				2. New population fitness/function values
		"""
		si = argsort(fpop)[:int(pa_v):-1]
		pop[si] = task.Lower + self.rand(task.D) * task.bRange
		fpop[si] = apply_along_axis(task.eval, 1, pop[si])
		return pop, fpop 
Example 19
Project: NiaPy   Author: NiaOrg   File: foa.py    License: MIT License 6 votes vote down vote up
def localSeeding(self, task, trees):
		r"""Local optimum search stage.

		Args:
			 task (Task): Optimization task.
			 trees (numpy.ndarray): Zero age trees for local seeding.

		Returns:
			 numpy.ndarray: Resulting zero age trees.
		"""
		n = trees.shape[0]
		deltas = self.uniform(-self.dx, self.dx, (n, self.lsc))
		deltas = append(deltas, zeros((n, task.D - self.lsc)), axis=1)
		perms = self.rand([deltas.shape[0], deltas.shape[1]]).argsort(1)
		deltas = deltas[arange(deltas.shape[0])[:, None], perms]
		trees += deltas
		trees = apply_along_axis(limit_repair, 1, trees, task.Lower, task.Upper)
		return trees 
Example 20
Project: NiaPy   Author: NiaOrg   File: foa.py    License: MIT License 6 votes vote down vote up
def survivalOfTheFittest(self, task, trees, candidates, age):
		r"""Evaluate and filter current population.

		Args:
			 task (Task): Optimization task.
			 trees (numpy.ndarray): Population to evaluate.
			 candidates (numpy.ndarray): Candidate population array to be updated.
			 age (numpy.ndarray[int32]): Age of trees.

		Returns:
			 Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray[float], numpy.ndarray[int32]]:
				  1. Trees sorted by fitness value.
				  2. Updated candidate population.
				  3. Population fitness values.
				  4. Age of trees
		"""
		evaluations = apply_along_axis(task.eval, 1, trees)
		ei = evaluations.argsort()
		candidates = append(candidates, trees[ei[self.al:]], axis=0)
		trees = trees[ei[:self.al]]
		age = age[ei[:self.al]]
		evaluations = evaluations[ei[:self.al]]
		return trees, candidates, evaluations, age 
Example 21
Project: NiaPy   Author: NiaOrg   File: cro.py    License: MIT License 6 votes vote down vote up
def SexualCrossoverSimple(pop, p, task, rnd=rand, **kwargs):
	r"""Sexual reproduction of corals.

	Args:
		pop (numpy.ndarray): Current population.
		p (float): Probability in range [0, 1].
		task (Task): Optimization task.
		rnd (mtrand.RandomState): Random generator.
		**kwargs (Dict[str, Any]): Additional arguments.

	Returns:
		Tuple[numpy.ndarray, numpy.ndarray]:
			1. New population.
			2. New population function/fitness values.
	"""
	for i in range(len(pop) // 2): pop[i] = asarray([pop[i, d] if rnd.rand() < p else pop[i * 2, d] for d in range(task.D)])
	return pop, apply_along_axis(task.eval, 1, pop) 
Example 22
Project: NiaPy   Author: NiaOrg   File: cro.py    License: MIT License 6 votes vote down vote up
def MoveCorals(pop, p, F, task, rnd=rand, **kwargs):
	r"""Move corals.

	Args:
		pop (numpy.ndarray): Current population.
		p (float): Probability in range [0, 1].
		F (float): Factor.
		task (Task): Optimization task.
		rnd (mtrand.RandomState): Random generator.
		**kwargs (Dict[str, Any]): Additional arguments.

	Returns:
		Tuple[numpy.ndarray, numpy.ndarray]:
			1. New population.
			2. New population function/fitness values.
	"""
	for i in range(len(pop)): pop[i] = task.repair(asarray([pop[i, d] if rnd.rand() < p else pop[i, d] + F * rnd.rand() for d in range(task.D)]), rnd=rnd)
	return pop, apply_along_axis(task.eval, 1, pop) 
Example 23
Project: NiaPy   Author: NiaOrg   File: fwa.py    License: MIT License 6 votes vote down vote up
def NextGeneration(self, FW, FW_f, FWn, task):
		r"""Generate new generation of individuals.

		Args:
			FW (numpy.ndarray): Current population.
			FW_f (numpy.ndarray[float]): Currents population fitness/function values.
			FWn (numpy.ndarray): New population.
			task (Task): Optimization task.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray[float]]:
				1. New population.
				2. New populations fitness/function values.
		"""
		FWn_f = apply_along_axis(task.eval, 1, FWn)
		ib = argmin(FWn_f)
		if FWn_f[ib] < FW_f[0]: FW[0], FW_f[0] = FWn[ib], FWn_f[ib]
		R = asarray([self.R(FWn[i], FWn) for i in range(len(FWn))])
		Rs = sum(R)
		P = asarray([self.p(R[i], Rs) for i in range(len(FWn))])
		isort = argsort(P)[-(self.NP - 1):]
		FW[1:], FW_f[1:] = asarray(FWn)[isort], FWn_f[isort]
		return FW, FW_f 
Example 24
Project: NiaPy   Author: NiaOrg   File: fwa.py    License: MIT License 6 votes vote down vote up
def NextGeneration(self, FW, FW_f, FWn, task):
		r"""TODO.

		Args:
			FW (numpy.ndarray): Current population.
			FW_f (numpy.ndarray[float]): Current populations function/fitness values.
			FWn (numpy.ndarray): New population.
			task (Task): Optimization task.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray[float]]:
				1. New population.
				2. New populations function/fitness values.
		"""
		FWn_f = apply_along_axis(task.eval, 1, FWn)
		ib = argmin(FWn_f)
		for i, f in enumerate(FW_f):
			r = self.randint(len(FWn))
			if FWn_f[r] < f: FW[i], FW_f[i] = FWn[r], FWn_f[r]
		FW[0], FW_f[0] = FWn[ib], FWn_f[ib]
		return FW, FW_f 
Example 25
Project: NiaPy   Author: NiaOrg   File: fwa.py    License: MIT License 6 votes vote down vote up
def uCF(self, xnb, xcb, xcb_f, xb, xb_f, Acf, task):
		r"""TODO.

		Args:
			xnb:
			xcb:
			xcb_f:
			xb:
			xb_f:
			Acf:
			task (Task): Optimization task.

		Returns:
			Tuple[numpy.ndarray, float, numpy.ndarray]:
				1. TODO
		"""
		xnb_f = apply_along_axis(task.eval, 1, xnb)
		ib_f = argmin(xnb_f)
		if xnb_f[ib_f] <= xb_f: xb, xb_f = xnb[ib_f], xnb_f[ib_f]
		Acf = self.repair(Acf, task.bRange, self.epsilon)
		if xb_f >= xcb_f: xb, xb_f, Acf = xcb, xcb_f, Acf * self.C_a
		else: Acf = Acf * self.C_r
		return xb, xb_f, Acf 
Example 26
Project: lambda-packs   Author: ryfeus   File: nanfunctions.py    License: MIT License 6 votes vote down vote up
def _nanmedian(a, axis=None, out=None, overwrite_input=False):
    """
    Private function that doesn't support extended axis or keepdims.
    These methods are extended to this function using _ureduce
    See nanmedian for parameter usage

    """
    if axis is None or a.ndim == 1:
        part = a.ravel()
        if out is None:
            return _nanmedian1d(part, overwrite_input)
        else:
            out[...] = _nanmedian1d(part, overwrite_input)
            return out
    else:
        # for small medians use sort + indexing which is still faster than
        # apply_along_axis
        # benchmarked with shuffled (50, 50, x) containing a few NaN
        if a.shape[axis] < 600:
            return _nanmedian_small(a, axis, out, overwrite_input)
        result = np.apply_along_axis(_nanmedian1d, axis, a, overwrite_input)
        if out is not None:
            out[...] = result
        return result 
Example 27
Project: lambda-packs   Author: ryfeus   File: nanfunctions.py    License: MIT License 6 votes vote down vote up
def _nanquantile_unchecked(a, q, axis=None, out=None, overwrite_input=False,
                           interpolation='linear', keepdims=np._NoValue):
    """Assumes that q is in [0, 1], and is an ndarray"""
    # apply_along_axis in _nanpercentile doesn't handle empty arrays well,
    # so deal them upfront
    if a.size == 0:
        return np.nanmean(a, axis, out=out, keepdims=keepdims)

    r, k = function_base._ureduce(
        a, func=_nanquantile_ureduce_func, q=q, axis=axis, out=out,
        overwrite_input=overwrite_input, interpolation=interpolation
    )
    if keepdims and keepdims is not np._NoValue:
        return r.reshape(q.shape + k)
    else:
        return r 
Example 28
Project: lambda-packs   Author: ryfeus   File: nanfunctions.py    License: MIT License 6 votes vote down vote up
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 
Example 29
Project: lambda-packs   Author: ryfeus   File: nanfunctions.py    License: MIT License 6 votes vote down vote up
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.rollaxis(result, axis)

    if out is not None:
        out[...] = result
    return result 
Example 30
Project: neural-fingerprinting   Author: StephanZheng   File: util.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def mle_single(data, x, k=10):
    data = np.asarray(data, dtype=np.float32)
    x = np.asarray(x, dtype=np.float32)
    if x.ndim == 1:
        x = x.reshape((-1, x.shape[0]))
    # dim = x.shape[1]

    k = min(k, len(data)-1)
    f = lambda v: - k / np.sum(np.log(v/v[-1]))
    a = cdist(x, data)
    a = np.apply_along_axis(np.sort, axis=1, arr=a)[:,1:k+1]
    a = np.apply_along_axis(f, axis=1, arr=a)
    return a[0]

# lid of a batch of query points X