本文整理汇总了Python中numpy.cumsum方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.cumsum方法的具体用法?Python numpy.cumsum怎么用?Python numpy.cumsum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.cumsum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: active_net_list
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def active_net_list(self):
net_list = [["input", 0, 0]]
active_cnt = np.arange(self.net_info.input_num + self.net_info.node_num + self.net_info.out_num)
active_cnt[self.net_info.input_num:] = np.cumsum(self.is_active)
for n, is_a in enumerate(self.is_active):
if is_a:
t = self.gene[n][0]
if n < self.net_info.node_num: # intermediate node
type_str = self.net_info.func_type[t]
else: # output node
type_str = self.net_info.out_type[t]
connections = [active_cnt[self.gene[n][i+1]] for i in range(self.net_info.max_in_num)]
net_list.append([type_str] + connections)
return net_list
# CGP with (1 + \lambda)-ES
示例2: step_function
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def step_function(x, center=0, FWHM=0.1, height=1, check=True):
"""
Compute a step function as the cumulative summation of a gaussian
distribution of a given vector.
:Parameters:
#. x (numpy.ndarray): The vector to compute the gaussian. gaussian
is computed as a function of x.
#. center (number): The center of the step function which is the
the center of the gaussian.
#. FWHM (number): The Full Width at Half Maximum of the gaussian.
#. height (number): The height of the step function.
#. check (boolean): whether to check arguments before generating
vectors.
"""
if check:
assert is_number(height), LOGGER.error("height must be a number")
height = FLOAT_TYPE(height)
g = gaussian(x, center=center, FWHM=FWHM, normalize=False, check=check)
sf = np.cumsum(g)
sf /= sf[-1]
return (sf*height).astype(FLOAT_TYPE)
示例3: calc_pr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def calc_pr(gt, out, wt=None):
if wt is None:
wt = np.ones((gt.size,1))
gt = gt.astype(np.float64).reshape((-1,1))
wt = wt.astype(np.float64).reshape((-1,1))
out = out.astype(np.float64).reshape((-1,1))
gt = gt*wt
tog = np.concatenate([gt, wt, out], axis=1)*1.
ind = np.argsort(tog[:,2], axis=0)[::-1]
tog = tog[ind,:]
cumsumsortgt = np.cumsum(tog[:,0])
cumsumsortwt = np.cumsum(tog[:,1])
prec = cumsumsortgt / cumsumsortwt
rec = cumsumsortgt / np.sum(tog[:,0])
ap = voc_ap(rec, prec)
return ap, rec, prec
示例4: sample
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def sample(self):
"""
This is the core sampling method. Samples a state from a
demonstration, in accordance with the configuration.
"""
# chooses a sampling scheme randomly based on the mixing ratios
seed = random.uniform(0, 1)
ratio = np.cumsum(self.scheme_ratios)
ratio = ratio > seed
for i, v in enumerate(ratio):
if v:
break
sample_method = getattr(self, self.sample_method_dict[self.sampling_schemes[i]])
return sample_method()
示例5: zipf_distribution
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def zipf_distribution(nbr_symbols, alpha):
"""Helper function: Create a Zipf distribution.
Args:
nbr_symbols: number of symbols to use in the distribution.
alpha: float, Zipf's Law Distribution parameter. Default = 1.5.
Usually for modelling natural text distribution is in
the range [1.1-1.6].
Returns:
distr_map: list of float, Zipf's distribution over nbr_symbols.
"""
tmp = np.power(np.arange(1, nbr_symbols + 1), -alpha)
zeta = np.r_[0.0, np.cumsum(tmp)]
return [x / zeta[-1] for x in zeta]
示例6: gen_dla
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def gen_dla(
n_dim=100, n_branch=20, branch_length=100, rand_multiplier=2, seed=37, sigma=4
):
np.random.seed(seed)
M = np.cumsum(-1 + rand_multiplier * np.random.rand(branch_length, n_dim), 0)
for i in range(n_branch - 1):
ind = np.random.randint(branch_length)
new_branch = np.cumsum(
-1 + rand_multiplier * np.random.rand(branch_length, n_dim), 0
)
M = np.concatenate([M, new_branch + M[ind, :]])
noise = np.random.normal(0, sigma, M.shape)
M = M + noise
# returns the group labels for each point to make it easier to visualize
# embeddings
C = np.array([i // branch_length for i in range(n_branch * branch_length)])
return M, C
示例7: residual_resample
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def residual_resample(weights):
n = len(weights)
indices = np.zeros(n, np.uint32)
# take int(N*w) copies of each weight
num_copies = (n * weights).astype(np.uint32)
k = 0
for i in range(n):
for _ in range(num_copies[i]): # make n copies
indices[k] = i
k += 1
# use multinormial resample on the residual to fill up the rest.
residual = weights - num_copies # get fractional part
residual /= np.sum(residual)
cumsum = np.cumsum(residual)
cumsum[-1] = 1
indices[k:n] = np.searchsorted(cumsum, np.random.uniform(0, 1, n - k))
return indices
示例8: create_indices
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def create_indices(positions, weights):
n = len(weights)
indices = np.zeros(n, np.uint32)
cumsum = np.cumsum(weights)
i, j = 0, 0
while i < n:
if positions[i] < cumsum[j]:
indices[i] = j
i += 1
else:
j += 1
return indices
### end rlabbe's resampling functions
示例9: unserialize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def unserialize(cls, data, block_size, back_window):
uncompressed = lz4.block.decompress(data)
nb_points = (
len(uncompressed) // cls._SERIALIZATION_TIMESTAMP_VALUE_LEN
)
try:
timestamps = numpy.frombuffer(uncompressed, dtype='<Q',
count=nb_points)
values = numpy.frombuffer(
uncompressed, dtype='<d',
offset=nb_points * cls._SERIALIZATION_TIMESTAMP_LEN)
except ValueError:
raise InvalidData
return cls.from_data(
numpy.cumsum(timestamps),
values,
block_size=block_size,
back_window=back_window)
示例10: vector_to_amplitudes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def vector_to_amplitudes(vec, nmo, nocc, nkpts=1):
nocca, noccb = nocc
nmoa, nmob = nmo
nvira, nvirb = nmoa - nocca, nmob - noccb
sizes = (nkpts*nocca*nvira, nkpts*noccb*nvirb,
nkpts**3*nocca**2*nvira**2, nkpts**3*nocca*noccb*nvira*nvirb,
nkpts**3*noccb**2*nvirb**2)
sections = np.cumsum(sizes[:-1])
t1a, t1b, t2aa, t2ab, t2bb = np.split(vec, sections)
t1a = t1a.reshape(nkpts,nocca,nvira)
t1b = t1b.reshape(nkpts,noccb,nvirb)
t2aa = t2aa.reshape(nkpts,nkpts,nkpts,nocca,nocca,nvira,nvira)
t2ab = t2ab.reshape(nkpts,nkpts,nkpts,nocca,noccb,nvira,nvirb)
t2bb = t2bb.reshape(nkpts,nkpts,nkpts,noccb,noccb,nvirb,nvirb)
return (t1a,t1b), (t2aa,t2ab,t2bb)
示例11: work_balanced_partition
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def work_balanced_partition(tasks, costs=None):
if costs is None:
costs = numpy.ones(tasks)
if rank == 0:
segsize = float(sum(costs)) / pool.size
loads = []
cum_costs = numpy.cumsum(costs)
start_id = 0
for k in range(pool.size):
stop_id = numpy.argmin(abs(cum_costs - (k+1)*segsize)) + 1
stop_id = max(stop_id, start_id+1)
loads.append([start_id,stop_id])
start_id = stop_id
comm.bcast(loads)
else:
loads = comm.bcast()
if rank < len(loads):
start, stop = loads[rank]
return tasks[start:stop]
else:
return tasks[:0]
示例12: vector_to_amplitudes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def vector_to_amplitudes(vector, nmo, nocc):
nocca, noccb = nocc
nmoa, nmob = nmo
nvira, nvirb = nmoa-nocca, nmob-noccb
nocc = nocca + noccb
nvir = nvira + nvirb
nov = nocc * nvir
size = nov + nocc*(nocc-1)//2*nvir*(nvir-1)//2
if vector.size == size:
#return ccsd.vector_to_amplitudes_s4(vector, nmo, nocc)
raise RuntimeError('Input vector is GCCSD vecotr')
else:
sizea = nocca * nvira + nocca*(nocca-1)//2*nvira*(nvira-1)//2
sizeb = noccb * nvirb + noccb*(noccb-1)//2*nvirb*(nvirb-1)//2
sections = np.cumsum([sizea, sizeb])
veca, vecb, t2ab = np.split(vector, sections)
t1a, t2aa = ccsd.vector_to_amplitudes_s4(veca, nmoa, nocca)
t1b, t2bb = ccsd.vector_to_amplitudes_s4(vecb, nmob, noccb)
t2ab = t2ab.copy().reshape(nocca,noccb,nvira,nvirb)
return (t1a,t1b), (t2aa,t2ab,t2bb)
示例13: cisdvec_to_amplitudes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def cisdvec_to_amplitudes(civec, nmo, nocc):
norba, norbb = nmo
nocca, noccb = nocc
nvira = norba - nocca
nvirb = norbb - noccb
nooa = nocca * (nocca-1) // 2
nvva = nvira * (nvira-1) // 2
noob = noccb * (noccb-1) // 2
nvvb = nvirb * (nvirb-1) // 2
size = (1, nocca*nvira, noccb*nvirb, nocca*noccb*nvira*nvirb,
nooa*nvva, noob*nvvb)
loc = numpy.cumsum(size)
c0 = civec[0]
c1a = civec[loc[0]:loc[1]].reshape(nocca,nvira)
c1b = civec[loc[1]:loc[2]].reshape(noccb,nvirb)
c2ab = civec[loc[2]:loc[3]].reshape(nocca,noccb,nvira,nvirb)
c2aa = _unpack_4fold(civec[loc[3]:loc[4]], nocca, nvira)
c2bb = _unpack_4fold(civec[loc[4]:loc[5]], noccb, nvirb)
return c0, (c1a,c1b), (c2aa,c2ab,c2bb)
示例14: _ecg_findpeaks_MWA
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def _ecg_findpeaks_MWA(signal, window_size):
"""From https://github.com/berndporr/py-ecg-detectors/"""
mwa = np.zeros(len(signal))
sums = np.cumsum(signal)
def get_mean(begin, end):
if begin == 0:
return sums[end - 1] / end
dif = sums[end - 1] - sums[begin - 1]
return dif / (end - begin)
for i in range(len(signal)): # pylint: disable=C0200
if i < window_size:
section = signal[0:i]
else:
section = get_mean(i - window_size, i)
if i != 0:
mwa[i] = np.mean(section)
else:
mwa[i] = signal[i]
return mwa
示例15: _signal_changepoints_cost_mean
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import cumsum [as 别名]
def _signal_changepoints_cost_mean(signal):
"""Cost function for a normally distributed signal with a changing mean."""
i_variance_2 = 1 / (np.var(signal) ** 2)
cmm = [0.0]
cmm.extend(np.cumsum(signal))
cmm2 = [0.0]
cmm2.extend(np.cumsum(np.abs(signal)))
def cost(start, end):
cmm2_diff = cmm2[end] - cmm2[start]
cmm_diff = pow(cmm[end] - cmm[start], 2)
i_diff = end - start
diff = cmm2_diff - cmm_diff
return (diff / i_diff) * i_variance_2
return cost