本文整理汇总了Python中numpy.isposinf方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.isposinf方法的具体用法?Python numpy.isposinf怎么用?Python numpy.isposinf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.isposinf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_scalar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_scalar(self):
x = np.inf
actual = np.isposinf(x)
expected = np.True_
assert_equal(actual, expected)
assert_equal(type(actual), type(expected))
x = -3.4
actual = np.fix(x)
expected = np.float64(-3.0)
assert_equal(actual, expected)
assert_equal(type(actual), type(expected))
out = np.array(0.0)
actual = np.fix(x, out=out)
assert_(actual is out)
示例2: probabilities
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def probabilities(self) -> np.ndarray:
"""Creates the probability matrix from the weights
"""
axis = 1
maxv = np.max(self.weights, axis=1, keepdims=True)
hasposinf = np.isposinf(maxv)
maxv[np.isinf(maxv)] = 0 # avoid indeterminations
exp: np.ndarray = np.exp(self.weights - maxv)
# deal with infinite positives special case
# by ignoring (0 proba) non-infinte on same row
if np.any(hasposinf):
is_inf = np.isposinf(self.weights)
is_ignored = np.logical_and(np.logical_not(is_inf), hasposinf)
exp[is_inf] = 1
exp[is_ignored] = 0
# random choice if sums to 0
sums0 = np.sum(exp, axis=axis) == 0
exp[sums0, :] = 1
exp /= np.sum(exp, axis=axis, keepdims=True) # normalize
return exp
示例3: test_is_inf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_is_inf(self):
if legacy_opset_pre_ver(10):
raise unittest.SkipTest("ONNX version {} doesn't support IsInf.".format(
defs.onnx_opset_version()))
input = np.array([-1.2, np.nan, np.inf, 2.8, np.NINF, np.inf],
dtype=np.float32)
expected_output = {
"node_def": np.isinf(input),
"node_def_neg_false": np.isposinf(input),
"node_def_pos_false": np.isneginf(input)
}
node_defs = {
"node_def":
helper.make_node("IsInf", ["X"], ["Y"]),
"node_def_neg_false":
helper.make_node("IsInf", ["X"], ["Y"], detect_negative=0),
"node_def_pos_false":
helper.make_node("IsInf", ["X"], ["Y"], detect_positive=0)
}
for key in node_defs:
output = run_node(node_defs[key], [input])
np.testing.assert_equal(output["Y"], expected_output[key])
示例4: test_kl_div
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_kl_div():
def xfunc(x, y):
if x < 0 or y < 0 or (y == 0 and x != 0):
# extension of natural domain to preserve convexity
return np.inf
elif np.isposinf(x) or np.isposinf(y):
# limits within the natural domain
return np.inf
elif x == 0:
return y
else:
return special.xlogy(x, x/y) - x + y
values = (0, 0.5, 1.0)
signs = [-1, 1]
arr = []
for sgna, va, sgnb, vb in itertools.product(signs, values, signs, values):
arr.append((sgna*va, sgnb*vb))
z = np.array(arr, dtype=float)
w = np.vectorize(xfunc, otypes=[np.float64])(z[:,0], z[:,1])
assert_func_equal(special.kl_div, w, z, rtol=1e-13, atol=1e-13)
示例5: isposinf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def isposinf(x, out=None):
"""
Test element-wise for positive infinity, return result as sparse ``bool`` array.
Parameters
----------
x
Input
out, optional
Output array
Examples
--------
>>> import sparse
>>> x = sparse.as_coo(np.array([np.inf]))
>>> sparse.isposinf(x).todense()
array([ True])
See Also
--------
numpy.isposinf : The NumPy equivalent
"""
from .core import elemwise
return elemwise(lambda x, out=None, dtype=None: np.isposinf(x, out=out), x, out=out)
示例6: fill_inf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def fill_inf(arr, pos_value=0, neg_value=0, copy=True):
"""Replaces positive and negative infinity entries in an array with the
provided values.
Parameters
----------
arr : np.array
pos_value : float
Fill value for np.inf
neg_value : float
Fill value for -np.inf
copy : bool, optional
If True, creates a copy of x, otherwise replaces values in-place.
By default, True.
"""
if copy:
arr = arr.copy()
arr[np.isposinf(arr)] = pos_value
arr[np.isneginf(arr)] = neg_value
return arr
示例7: jsonify_floats
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def jsonify_floats(json_object):
"""
Traverses through the JSON object and converts non JSON-spec compliant
floats(nan, -inf, inf) to their string representations.
Parameters
----------
json_object
JSON object
"""
if isinstance(json_object, dict):
return {k: jsonify_floats(v) for k, v in json_object.items()}
elif isinstance(json_object, list):
return [jsonify_floats(item) for item in json_object]
elif isinstance(json_object, float):
if np.isnan(json_object):
return "NaN"
elif np.isposinf(json_object):
return "Infinity"
elif np.isneginf(json_object):
return "-Infinity"
return json_object
return json_object
示例8: output
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def output(self, value, mask):
if mask:
return self._null_output
if np.isfinite(value):
if not np.isscalar(value):
value = value.dtype.type(value)
result = self._output_format.format(value)
if result.startswith('array'):
raise RuntimeError()
if (self._output_format[2] == 'r' and
result.endswith('.0')):
result = result[:-2]
return result
elif np.isnan(value):
return 'NaN'
elif np.isposinf(value):
return '+InF'
elif np.isneginf(value):
return '-InF'
# Should never raise
vo_raise(f"Invalid floating point value '{value}'")
示例9: test_init_owa_inf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_init_owa_inf(self):
r"""Test of initialization and __init__ -- OWA.
Method: An affordance to allow you to set OWA = +Infinity from a JSON
specs-file is offered by OpticalSystem: if OWA is supplied as 0, it is
set to +Infinity. We instantiate OpticalSystem objects and verify that
this is done.
"""
for specs in [specs_default, specs_simple, specs_multi]:
# the input dict is modified in-place -- so copy it
our_specs = deepcopy(specs)
our_specs['OWA'] = 0
for syst in our_specs['starlightSuppressionSystems']:
syst['OWA'] = 0
optsys = self.fixture(**deepcopy(our_specs))
self.assertTrue(np.isposinf(optsys.OWA.value))
for syst in optsys.starlightSuppressionSystems:
self.assertTrue(np.isposinf(syst['OWA'].value))
# repeat, but allow the special value to propagate up
for specs in [specs_default, specs_simple, specs_multi]:
# the input dict is modified in-place -- so copy it
our_specs = deepcopy(specs)
for syst in our_specs['starlightSuppressionSystems']:
syst['OWA'] = 0
optsys = self.fixture(**deepcopy(our_specs))
self.assertTrue(np.isposinf(optsys.OWA.value))
示例10: test_isposinf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_isposinf(self):
a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
out = nx.zeros(a.shape, bool)
tgt = nx.array([True, False, False, False, False, False])
res = ufl.isposinf(a)
assert_equal(res, tgt)
res = ufl.isposinf(a, out)
assert_equal(res, tgt)
assert_equal(out, tgt)
a = a.astype(np.complex)
with assert_raises(TypeError):
ufl.isposinf(a)
示例11: test_deprecated
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_deprecated(self):
# NumPy 1.13.0, 2017-04-26
assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2))
示例12: set_logp_to_neg_inf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def set_logp_to_neg_inf(X, logp, bounds):
"""Set `logp` to negative infinity when `X` is outside the allowed bounds.
# Arguments
X: tensorflow.Tensor
The variable to apply the bounds to
logp: tensorflow.Tensor
The log probability corrosponding to `X`
bounds: list of `Region` objects
The regions corrosponding to allowed regions of `X`
# Returns
logp: tensorflow.Tensor
The newly bounded log probability
"""
conditions = []
for l, u in bounds:
lower_is_neg_inf = not isinstance(l, tf.Tensor) and np.isneginf(l)
upper_is_pos_inf = not isinstance(u, tf.Tensor) and np.isposinf(u)
if not lower_is_neg_inf and upper_is_pos_inf:
conditions.append(tf.greater(X, l))
elif lower_is_neg_inf and not upper_is_pos_inf:
conditions.append(tf.less(X, u))
elif not (lower_is_neg_inf or upper_is_pos_inf):
conditions.append(tf.logical_and(tf.greater(X, l), tf.less(X, u)))
if len(conditions) > 0:
is_inside_bounds = conditions[0]
for condition in conditions[1:]:
is_inside_bounds = tf.logical_or(is_inside_bounds, condition)
logp = tf.select(
is_inside_bounds,
logp,
tf.fill(tf.shape(X), config.dtype(-np.inf))
)
return logp
示例13: test_isposinf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_isposinf(self):
a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
out = nx.zeros(a.shape, bool)
tgt = nx.array([True, False, False, False, False, False])
res = ufl.isposinf(a)
assert_equal(res, tgt)
res = ufl.isposinf(a, out)
assert_equal(res, tgt)
assert_equal(out, tgt)
示例14: assert_almost_equal_inf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def assert_almost_equal_inf(x, y, decimal=6, msg=None):
x = np.atleast_1d(x)
y = np.atleast_1d(y)
assert_equal(np.isposinf(x), np.isposinf(y))
assert_equal(np.isneginf(x), np.isneginf(y))
assert_equal(np.isnan(x), np.isnan(y))
assert_almost_equal(x[np.isfinite(x)], y[np.isfinite(y)])
示例15: test_infimputer_fill_values
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isposinf [as 别名]
def test_infimputer_fill_values():
"""
InfImputer when fill values are provided
"""
base_x = np.random.random((100, 10)).astype(np.float32)
flat_view = base_x.ravel()
pos_inf_idxs = [1, 2, 3, 4, 5]
neg_inf_idxs = [6, 7, 8, 9, 10]
flat_view[pos_inf_idxs] = np.inf
flat_view[neg_inf_idxs] = -np.inf
# Our base x should now be littered with pos/neg inf values
assert np.isposinf(base_x).sum() > 0, "Expected some positive infinity values here"
assert np.isneginf(base_x).sum() > 0, "Expected some negative infinity values here"
imputer = InfImputer(inf_fill_value=9999.0, neg_inf_fill_value=-9999.0)
X = imputer.fit_transform(base_x)
np.equal(
X.ravel()[[pos_inf_idxs]], np.array([9999.0, 9999.0, 9999.0, 9999.0, 9999.0])
)
np.equal(
X.ravel()[[neg_inf_idxs]],
np.array([-9999.0, -9999.0, -9999.0, -9999.0, -9999.0]),
)