本文整理汇总了Python中numpy.apply_along_axis方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.apply_along_axis方法的具体用法?Python numpy.apply_along_axis怎么用?Python numpy.apply_along_axis使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.apply_along_axis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_bp_indexes_labranchor
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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()
示例2: apply
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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)
示例3: test_apply_along_axis_matrix
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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)
示例4: _nanmedian
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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
示例5: _nanquantile_unchecked
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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
示例6: test_preserve_subclass
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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)
示例7: test_empty
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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)
示例8: test_rank_methods_frame
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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)
示例9: apply_raw
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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)
示例10: _add_shadows_get_imps
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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
示例11: spc2npow
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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
示例12: plot_cost_to_go_mountain_car
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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()
示例13: assign
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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
示例14: init_pop_numpy
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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
示例15: defaultNumPyInit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import apply_along_axis [as 别名]
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