本文整理汇总了Python中numpy.isnan方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.isnan方法的具体用法?Python numpy.isnan怎么用?Python numpy.isnan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.isnan方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bounds
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def test_bounds(self):
"""
Test that out-of-bounds coordinates return NaN reddening, and that
in-bounds coordinates do not return NaN reddening.
"""
for mode in (['random_sample', 'random_sample_per_pix',
'median', 'samples', 'mean']):
# Draw random coordinates, both above and below dec = -30 degree line
n_pix = 1000
ra = -180. + 360.*np.random.random(n_pix)
dec = -75. + 90.*np.random.random(n_pix) # 45 degrees above/below
c = coords.SkyCoord(ra, dec, frame='icrs', unit='deg')
ebv_calc = self._bayestar(c, mode=mode)
nan_below = np.isnan(ebv_calc[dec < -35.])
nan_above = np.isnan(ebv_calc[dec > -25.])
pct_nan_above = np.sum(nan_above) / float(nan_above.size)
# print r'{:s}: {:.5f}% nan above dec=-25 deg.'.format(mode, 100.*pct_nan_above)
self.assertTrue(np.all(nan_below))
self.assertTrue(pct_nan_above < 0.05)
示例2: apply_cmap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def apply_cmap(zs, cmap, vmin=None, vmax=None, unit=None, logrescale=False):
'''
apply_cmap(z, cmap) applies the given cmap to the values in z; if vmin and/or vmax are passed,
they are used to scale z.
Note that this function can automatically rescale data into log-space if the colormap is a
neuropythy log-space colormap such as log_eccentricity. To enable this behaviour use the
optional argument logrescale=True.
'''
zs = pimms.mag(zs) if unit is None else pimms.mag(zs, unit)
zs = np.asarray(zs, dtype='float')
if pimms.is_str(cmap): cmap = matplotlib.cm.get_cmap(cmap)
if logrescale:
if vmin is None: vmin = np.log(np.nanmin(zs))
if vmax is None: vmax = np.log(np.nanmax(zs))
mn = np.exp(vmin)
u = zdivide(nanlog(zs + mn) - vmin, vmax - vmin, null=np.nan)
else:
if vmin is None: vmin = np.nanmin(zs)
if vmax is None: vmax = np.nanmax(zs)
u = zdivide(zs - vmin, vmax - vmin, null=np.nan)
u[np.isnan(u)] = -np.inf
return cmap(u)
示例3: almost_equal_ignore_nan
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def almost_equal_ignore_nan(a, b, rtol=None, atol=None):
"""Test that two NumPy arrays are almost equal (ignoring NaN in either array).
Combines a relative and absolute measure of approximate eqality.
If either the relative or absolute check passes, the arrays are considered equal.
Including an absolute check resolves issues with the relative check where all
array values are close to zero.
Parameters
----------
a : np.ndarray
b : np.ndarray
rtol : None or float
The relative threshold. Default threshold will be used if set to ``None``.
atol : None or float
The absolute threshold. Default threshold will be used if set to ``None``.
"""
a = np.copy(a)
b = np.copy(b)
nan_mask = np.logical_or(np.isnan(a), np.isnan(b))
a[nan_mask] = 0
b[nan_mask] = 0
return almost_equal(a, b, rtol, atol)
示例4: assert_almost_equal_ignore_nan
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def assert_almost_equal_ignore_nan(a, b, rtol=None, atol=None, names=('a', 'b')):
"""Test that two NumPy arrays are almost equal (ignoring NaN in either array).
Combines a relative and absolute measure of approximate eqality.
If either the relative or absolute check passes, the arrays are considered equal.
Including an absolute check resolves issues with the relative check where all
array values are close to zero.
Parameters
----------
a : np.ndarray
b : np.ndarray
rtol : None or float
The relative threshold. Default threshold will be used if set to ``None``.
atol : None or float
The absolute threshold. Default threshold will be used if set to ``None``.
"""
a = np.copy(a)
b = np.copy(b)
nan_mask = np.logical_or(np.isnan(a), np.isnan(b))
a[nan_mask] = 0
b[nan_mask] = 0
assert_almost_equal(a, b, rtol, atol, names)
示例5: train_step
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def train_step(sess, train_op, loss, global_step):
"""Runs a single training step."""
start_time = time.time()
_, loss_val, global_step_val = sess.run([train_op, loss, global_step])
duration = time.time() - start_time
# Logging
if global_step_val % 10 == 0:
examples_per_sec = FLAGS.batch_size / duration
sec_per_batch = float(duration)
format_str = ('step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)')
tf.logging.info(format_str % (global_step_val, loss_val, examples_per_sec,
sec_per_batch))
if np.isnan(loss_val):
raise OverflowError('Loss is nan')
return global_step_val
示例6: generate_egocentric_maps
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def generate_egocentric_maps(scaled_maps, map_scales, map_crop_sizes, loc,
x_axis, y_axis, theta):
maps = []
for i, (map_, sc, map_crop_size) in enumerate(zip(scaled_maps, map_scales, map_crop_sizes)):
maps_i = np.array(get_map_to_predict(loc*sc, x_axis, y_axis, map_,
map_crop_size,
interpolation=cv2.INTER_LINEAR)[0])
maps_i[np.isnan(maps_i)] = 0
maps.append(maps_i)
return maps
示例7: nan_helper
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def nan_helper(y):
"""Helper to handle indices and logical indices of NaNs.
Input:
- y, 1d numpy array with possible NaNs
Output:
- nans, logical indices of NaNs
- index, a function, with signature indices= index(logical_indices),
to convert logical indices of NaNs to 'equivalent' indices
Example:
>>> # linear interpolation of NaNs
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
"""
return np.isnan(y), lambda z: z.nonzero()[0]
示例8: _transform_col
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def _transform_col(self, x, i):
"""Encode one numerical feature column to quantiles.
Args:
x (pandas.Series): numerical feature column to encode
i (int): column index of the numerical feature
Returns:
Encoded feature (pandas.Series).
"""
# Map values to the emperical CDF between .1% and 99.9%
rv = np.ones_like(x) * -1
filt = ~np.isnan(x)
rv[filt] = np.floor((self.ecdfs[i](x[filt]) * 0.998 + .001) *
self.n_label)
return rv
示例9: fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def fit(self, observations, labels):
def closure():
predicted = self.predict(observations)
loss = self.loss_fn(predicted, labels)
self.optimizer.zero_grad()
loss.backward()
return loss
old_params = parameters_to_vector(self.model.parameters())
for lr in self.lr * .5**np.arange(10):
self.optimizer = optim.LBFGS(self.model.parameters(), lr=lr)
self.optimizer.step(closure)
current_params = parameters_to_vector(self.model.parameters())
if any(np.isnan(current_params.data.cpu().numpy())):
print("LBFGS optimization diverged. Rolling back update...")
vector_to_parameters(old_params, self.model.parameters())
else:
return
示例10: test_weighted_mean
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def test_weighted_mean(dim, level, wgts_name):
res = esmlab.weighted_mean(dset, dim=dim, weights=wgts[wgts_name])
df = dset.to_dataframe()
df_w = wgts.to_dataframe()[wgts_name]
if not dim:
res = res.to_array().data
d = pd.concat([df, df_w], axis=1)
expected = d.apply(
lambda x: np.ma.average(np.ma.MaskedArray(x, mask=np.isnan(x)), weights=d.t_s_wgts)
)[['da1', 'da2']]
expected = expected.to_xarray().data
np.testing.assert_allclose(res, expected)
else:
expected = df.groupby(level=level).apply(
wavg, weights=wgts[wgts_name].data, col_names=['da1', 'da2']
)
res = res.to_dataframe()
assert_frame_equal(res.sort_index(), expected.sort_index())
示例11: numpy2pil
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def numpy2pil(a, vmin, vmax, fill=0):
mask = np.isnan(a)
a = np.clip((a - vmin) / (vmax - vmin), 0., 1.)
a = (254.99 * a).astype('u1')
a[mask] = fill
return PIL.Image.fromarray(a)
示例12: _compile_metrics
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def _compile_metrics(metrics_output_directory):
""" Compiles metrics from given directory and returns
results as dict.
:param metrics_output_directory: Directory to get metrics from.
:returns: Compiled metrics as dict.
"""
songs = glob(join(metrics_output_directory, 'test/*.json'))
index = pd.MultiIndex.from_tuples(
product(_INSTRUMENTS, _METRICS),
names=['instrument', 'metric'])
pd.DataFrame([], index=['config1', 'config2'], columns=index)
metrics = {
instrument: {k: [] for k in _METRICS}
for instrument in _INSTRUMENTS}
for song in songs:
with open(song, 'r') as stream:
data = json.load(stream)
for target in data['targets']:
instrument = target['name']
for metric in _METRICS:
sdr_med = np.median([
frame['metrics'][metric]
for frame in target['frames']
if not np.isnan(frame['metrics'][metric])])
metrics[instrument][metric].append(sdr_med)
return metrics
示例13: _cmag_coord_idcs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def _cmag_coord_idcs(coordinates):
return [i for (i,(x,y)) in enumerate(zip(*coordinates))
if (np.issubdtype(type(x), np.float) or np.issubdtype(type(x), np.int))
if (np.issubdtype(type(y), np.float) or np.issubdtype(type(y), np.int))
if not np.isnan(x) and not np.isnan(y)]
示例14: nan_compare
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def nan_compare(f, x, y, nan_nan=False, nan_val=False, val_nan=False):
'''
nan_compare(f, x, y) is equivalent to f(x, y), which is assumed to be a boolean function that
broadcasts over x and y (such as numpy.less), except that NaN values in either x or y result
in a value of False instead of being run through f.
The argument f must be a numpy comparison function such as numpy.less that accepts the optional
arguments where and out.
The following optional arguments may be provided:
* nan_nan (default: False) specifies the return value (True or False) for comparisons
equivalent to f(nan, nan).
* nan_val (default: False) specifies the return value (True or False) for comparisons
equivalent to f(nan, non_nan).
* val_nan (default: False) specifies the return value (True or False) for comparisons
equivalent to f(non_nan, nan).
'''
#TODO: This should work with sparse matrices as well
x = np.asanyarray(x)
y = np.asanyarray(y)
xii = np.isnan(x)
yii = np.isnan(y)
if not xii.any() and not yii.any(): return f(x, y)
ii = (~xii) & (~yii)
out = np.zeros(ii.shape, dtype=np.bool)
if nan_nan == nan_val and nan_val == val_nan:
# All the nan-result values are the same; we can simplify a little...
if nan_nan: out[~ii] = nan_nan
else:
if nan_nan: out[ xii & yii] = nan_nan
if nan_val: out[ xii & (~yii)] = nan_val
if val_nan: out[(~xii) & yii] = val_nan
return f(x, y, out=out, where=ii)
示例15: get_orientation_matrix
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isnan [as 别名]
def get_orientation_matrix(arrayAxis, alignToAxis):
"""
Get the rotation matrix that aligns arrayAxis to alignToAxis
:Parameters:
#. arrayAxis (list, tuple, numpy.ndarray): xyzArray axis.
#. alignToAxis (list, tuple, numpy.ndarray): The axis to align to.
"""
# normalize alignToAxis
alignToAxisNorm = np.linalg.norm(alignToAxis)
assert alignToAxisNorm>0, LOGGER.error("alignToAxis returned 0 norm")
alignToAxis = np.array(alignToAxis, dtype=FLOAT_TYPE)/alignToAxisNorm
# normalize arrayAxis
arrayAxisNorm = np.linalg.norm(arrayAxis)
assert arrayAxisNorm>0, LOGGER.error("arrayAxis returned 0 norm")
arrayAxis = np.array(arrayAxis, dtype=FLOAT_TYPE)/arrayAxisNorm
# calculate rotationAngle
dotProduct = np.dot(arrayAxis, alignToAxis)
if np.abs(dotProduct-1) <= PRECISION :
rotationAngle = 0
elif np.abs(dotProduct+1) <= PRECISION :
rotationAngle = PI
else:
rotationAngle = np.arccos( dotProduct )
if np.isnan(rotationAngle) or np.abs(rotationAngle) <= PRECISION :
return np.array([[1.,0.,0.],[0.,1.,0.],[0.,0.,1.]]).astype(FLOAT_TYPE)
# calculate rotation axis.
if np.abs(rotationAngle-PI) <= PRECISION:
rotationAxis = get_random_perpendicular_vector(arrayAxis)
else:
rotationAxis = np.cross(alignToAxis, arrayAxis)
#rotationAxis /= np.linalg.norm(rotationAxis)
# calculate rotation matrix
return get_rotation_matrix(rotationAxis, rotationAngle)