本文整理匯總了Python中numpy.nanvar方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.nanvar方法的具體用法?Python numpy.nanvar怎麽用?Python numpy.nanvar使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.nanvar方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_dtype_from_dtype
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def test_dtype_from_dtype(self):
mat = np.eye(3)
codes = 'efdgFDG'
for nf, rf in zip(self.nanfuncs, self.stdfuncs):
for c in codes:
with suppress_warnings() as sup:
if nf in {np.nanstd, np.nanvar} and c in 'FDG':
# Giving the warning is a small bug, see gh-8000
sup.filter(np.ComplexWarning)
tgt = rf(mat, dtype=np.dtype(c), axis=1).dtype.type
res = nf(mat, dtype=np.dtype(c), axis=1).dtype.type
assert_(res is tgt)
# scalar case
tgt = rf(mat, dtype=np.dtype(c), axis=None).dtype.type
res = nf(mat, dtype=np.dtype(c), axis=None).dtype.type
assert_(res is tgt)
示例2: test_dtype_from_char
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def test_dtype_from_char(self):
mat = np.eye(3)
codes = 'efdgFDG'
for nf, rf in zip(self.nanfuncs, self.stdfuncs):
for c in codes:
with suppress_warnings() as sup:
if nf in {np.nanstd, np.nanvar} and c in 'FDG':
# Giving the warning is a small bug, see gh-8000
sup.filter(np.ComplexWarning)
tgt = rf(mat, dtype=c, axis=1).dtype.type
res = nf(mat, dtype=c, axis=1).dtype.type
assert_(res is tgt)
# scalar case
tgt = rf(mat, dtype=c, axis=None).dtype.type
res = nf(mat, dtype=c, axis=None).dtype.type
assert_(res is tgt)
示例3: test_ddof_too_big
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def test_ddof_too_big(self):
nanfuncs = [np.nanvar, np.nanstd]
stdfuncs = [np.var, np.std]
dsize = [len(d) for d in _rdat]
for nf, rf in zip(nanfuncs, stdfuncs):
for ddof in range(5):
with suppress_warnings() as sup:
sup.record(RuntimeWarning)
sup.filter(np.ComplexWarning)
tgt = [ddof >= d for d in dsize]
res = nf(_ndat, axis=1, ddof=ddof)
assert_equal(np.isnan(res), tgt)
if any(tgt):
assert_(len(sup.log) == 1)
else:
assert_(len(sup.log) == 0)
示例4: test_ddof_too_big
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def test_ddof_too_big(self):
nanfuncs = [np.nanvar, np.nanstd]
stdfuncs = [np.var, np.std]
dsize = [len(d) for d in _rdat]
for nf, rf in zip(nanfuncs, stdfuncs):
for ddof in range(5):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
tgt = [ddof >= d for d in dsize]
res = nf(_ndat, axis=1, ddof=ddof)
assert_equal(np.isnan(res), tgt)
if any(tgt):
assert_(len(w) == 1)
assert_(issubclass(w[0].category, RuntimeWarning))
else:
assert_(len(w) == 0)
示例5: nanvar
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
"""Returns the variance along an axis ignoring NaN values.
Args:
a (cupy.ndarray): Array to compute variance.
axis (int): Along which axis to compute variance. The flattened array
is used by default.
dtype: Data type specifier.
out (cupy.ndarray): Output array.
keepdims (bool): If ``True``, the axis is remained as an axis of
size one.
Returns:
cupy.ndarray: The variance of the input array along the axis.
.. seealso:: :func:`numpy.nanvar`
"""
if a.dtype.kind in 'biu':
return a.var(axis=axis, dtype=dtype, out=out, ddof=ddof,
keepdims=keepdims)
# TODO(okuta): check type
return _statistics._nanvar(
a, axis=axis, dtype=dtype, out=out, ddof=ddof, keepdims=keepdims)
示例6: residual
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def residual(self, X_normalized):
total = 0
if self.center_rows:
row_means = np.nanmean(X_normalized, axis=1)
total += (row_means ** 2).sum()
if self.center_columns:
column_means = np.nanmean(X_normalized, axis=0)
total += (column_means ** 2).sum()
if self.scale_rows:
row_variances = np.nanvar(X_normalized, axis=1)
row_variances[row_variances == 0] = 1.0
total += (np.log(row_variances) ** 2).sum()
if self.scale_columns:
column_variances = np.nanvar(X_normalized, axis=0)
column_variances[column_variances == 0] = 1.0
total += (np.log(column_variances) ** 2).sum()
return total
示例7: __init__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def __init__(self, vals=None):
"""Initializes the mean and variance of the Gaussian variable."""
DType.__init__(self)
if vals is None:
vals = [0, 1] # some dummy. This is more for information.
# Ignore NaNs
n = np.count_nonzero(~np.isnan(vals))
if n > 0:
self.mean = np.nanmean(vals)
self.variance = np.nanvar(vals)
else:
self.mean = 0
self.variance = 0
示例8: test_observer_content_aware_subjective_model
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def test_observer_content_aware_subjective_model(self):
subjective_model = MaximumLikelihoodEstimationModel.from_dataset_file(
self.dataset_filepath)
result = subjective_model.run_modeling(force_subjbias_zeromean=False)
self.assertAlmostEqual(float(np.nansum(result['content_ambiguity'])), 2.653508643860357, places=4)
self.assertAlmostEqual(float(np.nanvar(result['content_ambiguity'])), 0.0092892978862108271, places=4)
self.assertAlmostEqual(float(np.sum(result['observer_bias'])), -0.020313188445860726, places=4)
self.assertAlmostEqual(float(np.var(result['observer_bias'])), 0.091830942654165318, places=4)
self.assertAlmostEqual(float(np.sum(result['observer_inconsistency'])), 11.232923468639161, places=4)
self.assertAlmostEqual(float(np.var(result['observer_inconsistency'])), 0.027721095664357907, places=4)
self.assertAlmostEqual(float(np.sum(result['quality_scores'])), 177.88599894484821, places=4)
self.assertAlmostEqual(float(np.var(result['quality_scores'])), 1.4896077857605587, places=4)
# self.assertAlmostEqual(np.nansum(result['content_ambiguity_std']), 0.30465244947706538, places=4)
self.assertAlmostEqual(float(np.sum(result['observer_bias_std'])), 2.165903882505483, places=4)
self.assertAlmostEqual(float(np.sum(result['observer_inconsistency_std'])), 27.520643824238352, places=4)
self.assertAlmostEqual(float(np.sum(result['quality_scores_std'])), 5.7355563435912256, places=4)
示例9: calc_12_mom_labeling
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def calc_12_mom_labeling(data, t, calculate_2_mom=True):
t_uniq = np.unique(t)
m = np.zeros((data.shape[0], len(t_uniq)))
if calculate_2_mom: v =np.zeros((data.shape[0], len(t_uniq)))
for i in range(data.shape[0]):
data_ = (
np.array(data[i].A.flatten(), dtype=float)
if issparse(data)
else np.array(data[i], dtype=float)
) # consider using the `adata.obs_vector`, `adata.var_vector` methods or accessing the array directly.
m[i] = strat_mom(data_, t, np.nanmean)
if calculate_2_mom: v[i] = strat_mom(data_, t, np.nanvar)
return (m, v, t_uniq) if calculate_2_mom else (m, t_uniq)
示例10: __init__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def __init__(self, adata, time_key="Time", has_nan=False):
# self.data = adata
self.__dict__ = adata.__dict__
# calculate first and second moments from data
self.times = np.array(self.obs[time_key].values, dtype=float)
self.uniq_times = np.unique(self.times)
nT = self.get_n_times()
ng = self.get_n_genes()
self.M = np.zeros((ng, nT)) # first moments (data)
self.V = np.zeros((ng, nT)) # second moments (data)
for g in tqdm(range(ng), desc="calculating 1/2 moments"):
tmp = self[:, g].layers["new"]
L = (
np.array(tmp.A, dtype=float)
if issparse(tmp)
else np.array(tmp, dtype=float)
) # consider using the `adata.obs_vector`, `adata.var_vector` methods or accessing the array directly.
if has_nan:
self.M[g] = strat_mom(L, self.times, np.nanmean)
self.V[g] = strat_mom(L, self.times, np.nanvar)
else:
self.M[g] = strat_mom(L, self.times, np.mean)
self.V[g] = strat_mom(L, self.times, np.var)
示例11: _calc_var
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanvar [as 別名]
def _calc_var(self):
if self.data is None:
raise RuntimeError('Fit the data model first.')
data = self.data.T
# variance calc
var = np.nanvar(data, axis=1)
total_var = var.sum()
self.var_exp = self.eig_vals.cumsum() / total_var