本文整理汇总了Python中autograd.numpy.all方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.all方法的具体用法?Python numpy.all怎么用?Python numpy.all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autograd.numpy
的用法示例。
在下文中一共展示了numpy.all方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_problems
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def test_problems(self):
for entry in problems:
name, params = entry
print("Testing: " + name)
X, F, CV = load(name)
if F is None:
print("Warning: No correctness check for %s" % name)
continue
problem = get_problem(name, *params)
_F, _G, _CV, _dF, _dG = problem.evaluate(X, return_values_of=["F", "G", "CV", "dF", "dG"])
if problem.n_obj == 1:
F = F[:, None]
self.assertTrue(anp.all(anp.abs(_F - F) < 0.00001))
if problem.n_constr > 0:
self.assertTrue(anp.all(anp.abs(_CV[:, 0] - CV) < 0.0001))
示例2: truncate0
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def truncate0(x, axis=None, strict=False, tol=1e-13):
'''make sure everything in x is non-negative'''
# the maximum along axis
maxes = np.maximum(np.amax(x, axis=axis), 1e-300)
# the negative part of minimum along axis
mins = np.maximum(-np.amin(x, axis=axis), 0.0)
# assert the negative numbers are small (relative to maxes)
assert np.all(mins <= tol * maxes)
if axis is not None:
idx = [slice(None)] * x.ndim
idx[axis] = np.newaxis
mins = mins[idx]
maxes = maxes[idx]
if strict:
# set everything below the tolerance to 0
return set0(x, x < tol * maxes)
else:
# set everything of same magnitude as most negative number, to 0
return set0(x, x < 2 * mins)
示例3: _expected_sfs
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def _expected_sfs(demography, configs, folded, error_matrices):
if np.any(configs.sampled_n != demography.sampled_n) or np.any(configs.sampled_pops != demography.sampled_pops):
raise ValueError(
"configs and demography must have same sampled_n, sampled_pops. Use Demography.copy() or ConfigList.copy() to make a copy with different sampled_n.")
vecs, idxs = configs._vecs_and_idxs(folded)
if error_matrices is not None:
vecs = _apply_error_matrices(vecs, error_matrices)
vals = expected_sfs_tensor_prod(vecs, demography)
sfs = vals[idxs['idx_2_row']]
if folded:
sfs = sfs + vals[idxs['folded_2_row']]
denom = vals[idxs['denom_idx']]
for i in (0, 1):
denom = denom - vals[idxs[("corrections_2_denom", i)]]
#assert np.all(np.logical_or(vals >= 0.0, np.isclose(vals, 0.0)))
return sfs, denom
示例4: _get_subsample_counts
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def _get_subsample_counts(configs, n):
subconfigs, weights = [], []
for pop_comb in it.combinations_with_replacement(configs.sampled_pops, n):
subsample_n = co.Counter(pop_comb)
subsample_n = np.array([subsample_n[pop]
for pop in configs.sampled_pops], dtype=int)
if np.any(subsample_n > configs.sampled_n):
continue
for sfs_entry in it.product(*(range(sub_n + 1)
for sub_n in subsample_n)):
sfs_entry = np.array(sfs_entry, dtype=int)
if np.all(sfs_entry == 0) or np.all(sfs_entry == subsample_n):
# monomorphic
continue
sfs_entry = np.transpose([subsample_n - sfs_entry, sfs_entry])
cnt_vec = configs.subsample_probs(sfs_entry)
if not np.all(cnt_vec == 0):
subconfigs.append(sfs_entry)
weights.append(cnt_vec)
return np.array(subconfigs), np.array(weights)
示例5: build_full_config_list
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def build_full_config_list(sampled_pops, sampled_n, ascertainment_pop=None):
sampled_n = np.array(sampled_n)
if ascertainment_pop is None:
ascertainment_pop = [True] * len(sampled_pops)
ascertainment_pop = np.array(ascertainment_pop)
ranges = [list(range(n + 1)) for n in sampled_n]
config_list = []
for x in it.product(*ranges):
x = np.array(x, dtype=int)
if not (np.all(x[ascertainment_pop] == 0) or np.all(
x[ascertainment_pop] == sampled_n[ascertainment_pop])):
config_list.append(x)
return build_config_list(
sampled_pops, np.array(config_list, dtype=int), sampled_n,
ascertainment_pop=ascertainment_pop)
示例6: fit_gaussian_draw
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def fit_gaussian_draw(X, J, seed=28, reg=1e-7, eig_pow=1.0):
"""
Fit a multivariate normal to the data X (n x d) and draw J points
from the fit.
- reg: regularizer to use with the covariance matrix
- eig_pow: raise eigenvalues of the covariance matrix to this power to construct
a new covariance matrix before drawing samples. Useful to shrink the spread
of the variance.
"""
with NumpySeedContext(seed=seed):
d = X.shape[1]
mean_x = np.mean(X, 0)
cov_x = np.cov(X.T)
if d==1:
cov_x = np.array([[cov_x]])
[evals, evecs] = np.linalg.eig(cov_x)
evals = np.maximum(0, np.real(evals))
assert np.all(np.isfinite(evals))
evecs = np.real(evecs)
shrunk_cov = evecs.dot(np.diag(evals**eig_pow)).dot(evecs.T) + reg*np.eye(d)
V = np.random.multivariate_normal(mean_x, shrunk_cov, J)
return V
示例7: bound_by_data
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def bound_by_data(Z, Data):
"""
Determine lower and upper bound for each dimension from the Data, and project
Z so that all points in Z live in the bounds.
Z: m x d
Data: n x d
Return a projected Z of size m x d.
"""
n, d = Z.shape
Low = np.min(Data, 0)
Up = np.max(Data, 0)
LowMat = np.repeat(Low[np.newaxis, :], n, axis=0)
UpMat = np.repeat(Up[np.newaxis, :], n, axis=0)
Z = np.maximum(LowMat, Z)
Z = np.minimum(UpMat, Z)
return Z
示例8: _set_transmat
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def _set_transmat(self, transmat_val):
if transmat_val is None:
transmat = np.tile(1.0 / self.n_components,
(self.n_components, self.n_components))
else:
transmat_val[np.isnan(transmat_val)] = 0.0
normalize(transmat_val, axis=1)
if (np.asarray(transmat_val).shape == (self.n_components,
self.n_components)):
transmat = np.copy(transmat_val)
elif transmat_val.shape[0] == self.n_unique:
transmat = self._ntied_transmat(transmat_val)
else:
raise ValueError("cannot match shape of transmat")
if not np.all(np.allclose(np.sum(transmat, axis=1), 1.0)):
raise ValueError('Rows of transmat must sum to 1.0')
self._log_transmat = np.log(np.asarray(transmat).copy())
underflow_idx = np.isnan(self._log_transmat)
self._log_transmat[underflow_idx] = NEGINF
示例9: test_problems
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def test_problems(self):
for n_obj, n_var, k in [(2, 6, 4), (3, 6, 4), (10, 20, 18)]:
problems = [
get_problem("wfg1", n_var, n_obj, k),
get_problem("wfg2", n_var, n_obj, k),
get_problem("wfg3", n_var, n_obj, k),
get_problem("wfg4", n_var, n_obj, k),
get_problem("wfg5", n_var, n_obj, k),
get_problem("wfg6", n_var, n_obj, k),
get_problem("wfg7", n_var, n_obj, k),
get_problem("wfg8", n_var, n_obj, k),
get_problem("wfg9", n_var, n_obj, k)
]
for problem in problems:
name = str(problem.__class__.__name__)
print("Testing: " + name + "-" + str(n_obj))
X, F, CV = load(name, n_obj)
# other = from_optproblems(problem)
# F = np.row_stack([other.objective_function(x) for x in X])
if F is None:
print("Warning: No correctness check for %s" % name)
continue
_F, _G, _CV = problem.evaluate(X, return_values_of=["F", "G", "CV"])
if problem.n_obj == 1:
F = F[:, None]
self.assertTrue(anp.all(anp.abs(_F - F) < 0.00001))
if problem.n_constr > 0:
self.assertTrue(anp.all(anp.abs(_CV[:, 0] - CV) < 0.0001))
示例10: _centered
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def _centered(arr, newshape):
"""Return the center newshape portion of the array.
This function is used by `fft_convolve` to remove
the zero padded region of the convolution.
Note: If the array shape is odd and the target is even,
the center of `arr` is shifted to the center-right
pixel position.
This is slightly different than the scipy implementation,
which uses the center-left pixel for the array center.
The reason for the difference is that we have
adopted the convention of `np.fft.fftshift` in order
to make sure that changing back and forth from
fft standard order (0 frequency and position is
in the bottom left) to 0 position in the center.
"""
newshape = np.asarray(newshape)
currshape = np.array(arr.shape)
if not np.all(newshape <= currshape):
msg = (
"arr must be larger than newshape in both dimensions, received {0}, and {1}"
)
raise ValueError(msg.format(arr.shape, newshape))
startind = (currshape - newshape + 1) // 2
endind = startind + newshape
myslice = [slice(startind[k], endind[k]) for k in range(len(endind))]
return arr[tuple(myslice)]
示例11: combine_loci
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def combine_loci(self):
# return copy with all loci combined
return self.from_matrix(self.freqs_matrix.sum(axis=1),
self.configs, self.folded,
self._length)
示例12: __init__
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def __init__(self, sampled_pops, conf_arr, sampled_n=None,
ascertainment_pop=None):
"""Use build_config_list() instead of calling this constructor directly"""
# If sampled_n=None, ConfigList.sampled_n will be the max number of
# observed individuals/alleles per population.
self.sampled_pops = tuple(sampled_pops)
self.value = conf_arr
if ascertainment_pop is None:
ascertainment_pop = [True] * len(sampled_pops)
self.ascertainment_pop = np.array(ascertainment_pop)
self.ascertainment_pop.setflags(write=False)
if all(not a for a in self.ascertainment_pop):
raise ValueError(
"At least one of the populations must be used for "
"ascertainment of polymorphic sites")
max_n = np.max(np.sum(self.value, axis=2), axis=0)
if sampled_n is None:
sampled_n = max_n
sampled_n = np.array(sampled_n)
if np.any(sampled_n < max_n):
raise ValueError("config greater than sampled_n")
self.sampled_n = sampled_n
if not np.sum(sampled_n[self.ascertainment_pop]) >= 2:
raise ValueError("The total sample size of the ascertainment "
"populations must be >= 2")
config_sampled_n = np.sum(self.value, axis=2)
self.has_missing_data = np.any(config_sampled_n != self.sampled_n)
if np.any(np.sum(self.value[:, self.ascertainment_pop, :], axis=1)
== 0):
raise ValueError("Monomorphic sites not allowed. In addition, all"
" sites must be polymorphic when restricted to"
" the ascertainment populations")
示例13: __eq__
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def __eq__(self, other):
conf_arr = self.value
try:
return np.all(conf_arr == other.value)
except AttributeError:
return False
示例14: transformed_expi
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def transformed_expi(x):
abs_x = np.abs(x)
ser = abs_x < 1. / 45.
nser = np.logical_not(ser)
# ret = np.zeros(x.shape)
# ret[ser], ret[nser] = transformed_expi_series(x[ser]), transformed_expi_naive(x[nser])))
# return ret
# We use np.concatenate to combine.
# would be better to use ret[ser] and ret[nser] as commented out above
# but array assignment not yet supported by autograd
assert np.all(abs_x[:-1] >= abs_x[1:])
return np.concatenate((transformed_expi_naive(x[nser]), transformed_expi_series(x[ser])))
示例15: expm1d
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import all [as 别名]
def expm1d(x, eps=1e-6):
x = np.array(x)
abs_x = np.abs(x)
if x.shape:
# FIXME: don't require abs_x to be increasing
assert np.all(abs_x[1:] >= abs_x[:-1])
small = abs_x < eps
big = ~small
return np.concatenate([expm1d_taylor(x[small]),
expm1d_naive(x[big])])
elif abs_x < eps:
return expm1d_taylor(x)
else:
return expm1d_naive(x)