本文整理汇总了Python中autograd.numpy.arange方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.arange方法的具体用法?Python numpy.arange怎么用?Python numpy.arange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autograd.numpy
的用法示例。
在下文中一共展示了numpy.arange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: expected_tmrca
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def expected_tmrca(demography, sampled_pops=None, sampled_n=None):
"""
The expected time to most recent common ancestor of the sample.
Parameters
----------
demography : Demography
Returns
-------
tmrca : float-like
See Also
--------
expected_deme_tmrca : tmrca of subsample within a deme
expected_sfs_tensor_prod : compute general class of summary statistics
"""
vecs = [np.ones(n + 1) for n in demography.sampled_n]
n0 = len(vecs[0]) - 1.0
vecs[0] = np.arange(n0 + 1) / n0
return np.squeeze(expected_sfs_tensor_prod(vecs, demography))
示例2: resample
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def resample(self):
"""Create a new SFS by resampling blocks with replacement.
Note the resampled SFS is assumed to have the same length in base pairs \
as the original SFS, which may be a poor assumption if the blocks are not of equal length.
:returns: Resampled SFS
:rtype: :class:`Sfs`
"""
loci = np.random.choice(
np.arange(self.n_loci), size=self.n_loci, replace=True)
mat = self.freqs_matrix[:, loci]
to_keep = np.asarray(mat.sum(axis=1) > 0).squeeze()
to_keep = np.arange(len(self.configs))[to_keep]
mat = mat[to_keep, :]
configs = _ConfigList_Subset(self.configs, to_keep)
return self.from_matrix(mat, configs, self.folded, self.length)
示例3: sfs
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def sfs(self, n):
if n == 0:
return np.array([0.])
Et_jj = self.etjj(n)
#assert np.all(Et_jj[:-1] - Et_jj[1:] >= 0.0) and np.all(Et_jj >= 0.0) and np.all(Et_jj <= self.tau)
ret = np.sum(Et_jj[:, None] * Wmatrix(n), axis=0)
before_tmrca = self.tau - np.sum(ret * np.arange(1, n) / n)
# ignore branch length above untruncated TMRCA
if self.tau == float('inf'):
before_tmrca = 0.0
ret = np.concatenate((np.array([0.0]), ret, np.array([before_tmrca])))
return ret
# def transition_prob(self, v, axis=0):
# return moran_model.moran_action(self.scaled_time, v, axis=axis)
示例4: average_path_length
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def average_path_length(tree, X):
"""Compute average path length: cost of simulating the average
example; this is used in the objective function.
@param tree: DecisionTreeClassifier instance
@param X: NumPy array (D x N)
D := number of dimensions
N := number of examples
@return path_length: float
average path length
"""
leaf_indices = tree.apply(X)
leaf_counts = np.bincount(leaf_indices)
leaf_i = np.arange(tree.tree_.node_count)
path_length = np.dot(leaf_i, leaf_counts) / float(X.shape[0])
return path_length
示例5: get_ith_minibatch_ixs_fences
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def get_ith_minibatch_ixs_fences(b_i, batch_size, fences):
"""Split timeseries data of uneven sequence lengths into batches.
This is how we handle different sized sequences.
@param b_i: integer
iteration index
@param batch_size: integer
size of batch
@param fences: list of integers
sequence of cutoff array
@return idx: integer
@return batch_slice: slice object
"""
num_data = len(fences) - 1
num_minibatches = num_data / batch_size + ((num_data % batch_size) > 0)
b_i = b_i % num_minibatches
idx = slice(b_i * batch_size, (b_i+1) * batch_size)
batch_i = np.arange(num_data)[idx]
batch_slice = np.concatenate([range(i, j) for i, j in
zip(fences[batch_i], fences[batch_i+1])])
return idx, batch_slice
示例6: advect
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def advect(f, vx, vy):
"""Move field f according to x and y velocities (u and v)
using an implicit Euler integrator."""
rows, cols = f.shape
cell_xs, cell_ys = np.meshgrid(np.arange(cols), np.arange(rows))
center_xs = (cell_xs - vx).ravel()
center_ys = (cell_ys - vy).ravel()
# Compute indices of source cells.
left_ix = np.floor(center_ys).astype(int)
top_ix = np.floor(center_xs).astype(int)
rw = center_ys - left_ix # Relative weight of right-hand cells.
bw = center_xs - top_ix # Relative weight of bottom cells.
left_ix = np.mod(left_ix, rows) # Wrap around edges of simulation.
right_ix = np.mod(left_ix + 1, rows)
top_ix = np.mod(top_ix, cols)
bot_ix = np.mod(top_ix + 1, cols)
# A linearly-weighted sum of the 4 surrounding cells.
flat_f = (1 - rw) * ((1 - bw)*f[left_ix, top_ix] + bw*f[left_ix, bot_ix]) \
+ rw * ((1 - bw)*f[right_ix, top_ix] + bw*f[right_ix, bot_ix])
return np.reshape(flat_f, (rows, cols))
示例7: advect
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def advect(f, vx, vy):
"""Move field f according to x and y velocities (u and v)
using an implicit Euler integrator."""
rows, cols = f.shape
cell_ys, cell_xs = np.meshgrid(np.arange(rows), np.arange(cols))
center_xs = (cell_xs - vx).ravel()
center_ys = (cell_ys - vy).ravel()
# Compute indices of source cells.
left_ix = np.floor(center_xs).astype(int)
top_ix = np.floor(center_ys).astype(int)
rw = center_xs - left_ix # Relative weight of right-hand cells.
bw = center_ys - top_ix # Relative weight of bottom cells.
left_ix = np.mod(left_ix, rows) # Wrap around edges of simulation.
right_ix = np.mod(left_ix + 1, rows)
top_ix = np.mod(top_ix, cols)
bot_ix = np.mod(top_ix + 1, cols)
# A linearly-weighted sum of the 4 surrounding cells.
flat_f = (1 - rw) * ((1 - bw)*f[left_ix, top_ix] + bw*f[left_ix, bot_ix]) \
+ rw * ((1 - bw)*f[right_ix, top_ix] + bw*f[right_ix, bot_ix])
return np.reshape(flat_f, (rows, cols))
示例8: compute_f
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def compute_f(theta, lambda0, dL, shape):
""" Compute the 'vacuum' field vector """
# get plane wave k vector components (in units of grid cells)
k0 = 2 * npa.pi / lambda0 * dL
kx = k0 * npa.sin(theta)
ky = -k0 * npa.cos(theta) # negative because downwards
# array to write into
f_src = npa.zeros(shape, dtype=npa.complex128)
# get coordinates
Nx, Ny = shape
xpoints = npa.arange(Nx)
ypoints = npa.arange(Ny)
xv, yv = npa.meshgrid(xpoints, ypoints, indexing='ij')
# compute values and insert into array
x_PW = npa.exp(1j * xpoints * kx)[:, None]
y_PW = npa.exp(1j * ypoints * ky)[:, None]
f_src[xv, yv] = npa.outer(x_PW, y_PW)
return f_src.flatten()
示例9: make_IO_matrices
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def make_IO_matrices(indices, N):
""" Makes matrices that relate the sparse matrix entries to their locations in the matrix
The kth column of I is a 'one hot' vector specifing the k-th entries row index into A
The kth column of J is a 'one hot' vector specifing the k-th entries columnn index into A
O = J^T is for notational convenience.
Armed with a vector of M entries 'a', we can construct the sparse matrix 'A' as:
A = I @ diag(a) @ O
where 'diag(a)' is a (MxM) matrix with vector 'a' along its diagonal.
In index notation:
A_ij = I_ik * a_k * O_kj
In an opposite way, given sparse matrix 'A' we can strip out the entries `a` using the IO matrices as follows:
a = diag(I^T @ A @ O^T)
In index notation:
a_k = I_ik * A_ij * O_kj
"""
M = indices.shape[1] # number of indices in the matrix
entries_1 = npa.ones(M) # M entries of all 1's
ik, jk = indices # separate i and j components of the indices
indices_I = npa.vstack((ik, npa.arange(M))) # indices into the I matrix
indices_J = npa.vstack((jk, npa.arange(M))) # indices into the J matrix
I = make_sparse(entries_1, indices_I, shape=(N, M)) # construct the I matrix
J = make_sparse(entries_1, indices_J, shape=(N, M)) # construct the J matrix
O = J.T # make O = J^T matrix for consistency with my notes.
return I, O
示例10: _make_A
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def _make_A(self, eps_vec):
eps_vec_xx, eps_vec_yy = self._grid_average_2d(eps_vec)
eps_vec_xx_inv = 1 / (eps_vec_xx + 1e-5) # the 1e-5 is for numerical stability
eps_vec_yy_inv = 1 / (eps_vec_yy + 1e-5) # autograd throws 'divide by zero' errors.
indices_diag = npa.vstack((npa.arange(self.N), npa.arange(self.N)))
entries_DxEpsy, indices_DxEpsy = spsp_mult(self.entries_Dxb, self.indices_Dxb, eps_vec_yy_inv, indices_diag, self.N)
entires_DxEpsyDx, indices_DxEpsyDx = spsp_mult(entries_DxEpsy, indices_DxEpsy, self.entries_Dxf, self.indices_Dxf, self.N)
entries_DyEpsx, indices_DyEpsx = spsp_mult(self.entries_Dyb, self.indices_Dyb, eps_vec_xx_inv, indices_diag, self.N)
entires_DyEpsxDy, indices_DyEpsxDy = spsp_mult(entries_DyEpsx, indices_DyEpsx, self.entries_Dyf, self.indices_Dyf, self.N)
entries_d = 1 / EPSILON_0 * npa.hstack((entires_DxEpsyDx, entires_DyEpsxDy))
indices_d = npa.hstack((indices_DxEpsyDx, indices_DyEpsxDy))
entries_diag = MU_0 * self.omega**2 * npa.ones(self.N)
entries_a = npa.hstack((entries_d, entries_diag))
indices_a = npa.hstack((indices_d, indices_diag))
return entries_a, indices_a
示例11: get_scale
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def get_scale(n, scale_factor):
return anp.power(anp.full(n, scale_factor), anp.arange(n))
示例12: _evaluate
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def _evaluate(self, x, out, *args, **kwargs):
a = anp.sum(0.5 * anp.arange(1, self.n_var + 1) * x, axis=1)
out["F"] = anp.sum(anp.square(x), axis=1) + anp.square(a) + anp.power(a, 4)
示例13: __init__
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def __init__(self):
super().__init__(n_var=5, n_obj=2, n_constr=19, type_var=anp.int)
# ri, ro, t, F, Z
# self.xl = anp.array([60, 90, 1, 600, 2])
self.xl = anp.array([0, 0, 0, 0, 0])
self.xu = anp.array([20, 20, 4, 400, 7])
self.x1 = anp.arange(60, 81)
self.x2 = anp.arange(90, 111)
self.x3 = anp.arange(1, 3.5, 0.5)
self.x4 = anp.arange(600, 1001)
self.x5 = anp.arange(2, 11)
示例14: moffat
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def moffat(y, x, alpha=4.7, beta=1.5, bbox=None):
"""Symmetric 2D Moffat function
.. math::
(1+\frac{(x-x0)^2+(y-y0)^2}{\alpha^2})^{-\beta}
Parameters
----------
y: float
Vertical coordinate of the center
x: float
Horizontal coordinate of the center
alpha: float
Core width
beta: float
Power-law index
bbox: Box
Bounding box over which to evaluate the function
Returns
-------
result: array
A 2D circular gaussian sampled at the coordinates `(y_i, x_j)`
for all i and j in `shape`.
"""
Y = np.arange(bbox.shape[1]) + bbox.origin[1]
X = np.arange(bbox.shape[2]) + bbox.origin[2]
X, Y = np.meshgrid(X, Y)
# TODO: has no pixel-integration formula
return ((1 + ((X - x) ** 2 + (Y - y) ** 2) / alpha ** 2) ** -beta)[None, :, :]
示例15: gaussian
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import arange [as 别名]
def gaussian(y, x, sigma=1, integrate=True, bbox=None):
"""Circular Gaussian Function
Parameters
----------
y: float
Vertical coordinate of the center
x: float
Horizontal coordinate of the center
sigma: float
Standard deviation of the gaussian
integrate: bool
Whether pixel integration is performed
bbox: Box
Bounding box over which to evaluate the function
Returns
-------
result: array
A 2D circular gaussian sampled at the coordinates `(y_i, x_j)`
for all i and j in `shape`.
"""
Y = np.arange(bbox.shape[1]) + bbox.origin[1]
X = np.arange(bbox.shape[2]) + bbox.origin[2]
def f(X):
if not integrate:
return np.exp(-(X ** 2) / (2 * sigma ** 2))
else:
sqrt2 = np.sqrt(2)
return (
np.sqrt(np.pi / 2)
* sigma
* (
scipy.special.erf((0.5 - X) / (sqrt2 * sigma))
+ scipy.special.erf((2 * X + 1) / (2 * sqrt2 * sigma))
)
)
return (f(Y - y)[:, None] * f(X - x)[None, :])[None, :, :]