本文整理汇总了Python中numpy.logaddexp2函数的典型用法代码示例。如果您正苦于以下问题:Python logaddexp2函数的具体用法?Python logaddexp2怎么用?Python logaddexp2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了logaddexp2函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nan
def test_nan(self):
err = np.seterr(invalid="ignore")
try:
assert np.isnan(np.logaddexp2(np.nan, np.inf))
assert np.isnan(np.logaddexp2(np.inf, np.nan))
assert np.isnan(np.logaddexp2(np.nan, 0))
assert np.isnan(np.logaddexp2(0, np.nan))
assert np.isnan(np.logaddexp2(np.nan, np.nan))
finally:
np.seterr(**err)
示例2: _nllx
def _nllx(xi, i):
ll = 0.0
zi[0] = sum(xi ** 2.0)
zi[1 : k + 1] = xi
for j in range(l):
gij = snp_matrix[i, j]
if gij == geosnp.MISSING:
continue
qnf = zi.T.dot(Y[j])
r = numpy.logaddexp2(-qnf, 0)
r2 = numpy.logaddexp2(qnf, 0)
ll -= (gij * r) + ((2.0 - gij) * (r2))
# return NLL in order to minimize
return -ll
示例3: _nlly
def _nlly(yj, j):
ll = 0.0
q, a, b = yj[0], yj[1 : k + 1], yj[-1]
for i in range(n):
gij = snp_matrix[i, j]
if gij == geosnp.MISSING:
continue
xi = X[i]
qnf = (q * sum(xi ** 2.0)) + a.dot(xi) + b
r = numpy.logaddexp2(-qnf, 0)
r2 = numpy.logaddexp2(qnf, 0)
ll -= (gij * r) + ((2.0 - gij) * (r2))
# return NLL in order to minimize
return -ll
示例4: _increment_by_frecency
def _increment_by_frecency(self, frecency_added, multiplier=1.):
"""Increment this frecency by another frecency, with optional multiplier.
NOTE: No attempt is made here to handle differing timescales or other parameters.
"""
log2_multiplier = log2(multiplier)
log2_weight_added = frecency_added.log2_value + log2_multiplier
self.log2_value = logaddexp2(self.log2_value, log2_weight_added)
示例5: add
def add(self, x, y):
# Convert log_b probabilities to log_2 probabilities.
x2 = x * np.log2(base)
y2 = y * np.log2(base)
z = np.logaddexp2(x2, y2)
# Convert log_2 probabilities to log_b probabilities.
z *= self.log(2)
return z
示例6: _baum_welch_step
def _baum_welch_step(self, sequence, model, symbol_to_number):
N = len(model._states)
M = len(model._symbols)
T = len(sequence)
# compute forward and backward probabilities
alpha = model._forward_probability(sequence)
beta = model._backward_probability(sequence)
# find the log probability of the sequence
lpk = logsumexp2(alpha[T - 1])
A_numer = _ninf_array((N, N))
B_numer = _ninf_array((N, M))
A_denom = _ninf_array(N)
B_denom = _ninf_array(N)
transitions_logprob = model._transitions_matrix().T
for t in range(T):
symbol = sequence[t][_TEXT] # not found? FIXME
next_symbol = None
if t < T - 1:
next_symbol = sequence[t + 1][_TEXT] # not found? FIXME
xi = symbol_to_number[symbol]
next_outputs_logprob = model._outputs_vector(next_symbol)
alpha_plus_beta = alpha[t] + beta[t]
if t < T - 1:
numer_add = (
transitions_logprob
+ next_outputs_logprob
+ beta[t + 1]
+ alpha[t].reshape(N, 1)
)
A_numer = np.logaddexp2(A_numer, numer_add)
A_denom = np.logaddexp2(A_denom, alpha_plus_beta)
else:
B_denom = np.logaddexp2(A_denom, alpha_plus_beta)
B_numer[:, xi] = np.logaddexp2(B_numer[:, xi], alpha_plus_beta)
return lpk, A_numer, A_denom, B_numer, B_denom
示例7: test_logaddexp2_range
def test_logaddexp2_range(self):
x = [1000000, -1000000, 1000200, -1000200]
y = [1000200, -1000200, 1000000, -1000000]
z = [1000200, -1000000, 1000200, -1000000]
for dt in ["f", "d", "g"]:
logxf = np.array(x, dtype=dt)
logyf = np.array(y, dtype=dt)
logzf = np.array(z, dtype=dt)
assert_almost_equal(np.logaddexp2(logxf, logyf), logzf)
示例8: test_logaddexp2_values
def test_logaddexp2_values(self):
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
z = [6, 6, 6, 6, 6]
for dt, dec in zip(["f", "d", "g"], [6, 15, 15]):
xf = np.log2(np.array(x, dtype=dt))
yf = np.log2(np.array(y, dtype=dt))
zf = np.log2(np.array(z, dtype=dt))
assert_almost_equal(np.logaddexp2(xf, yf), zf, decimal=dec)
示例9: misc_floating
def misc_floating(mod, x):
"miscellaneous"
# y0 = math.erfc(x)
y1 = math.atan2(x, x)
y2 = np.arctan2(x, x)
y3 = np.logaddexp(x, x)
y4 = np.logaddexp2(x, x)
return (y1, y2, y3, y4) #(y0, y1)
示例10: test_inf
def test_inf(self):
inf = np.inf
x = [inf, -inf, inf, -inf, inf, 1, -inf, 1]
y = [inf, inf, -inf, -inf, 1, inf, 1, -inf]
z = [inf, inf, inf, -inf, inf, inf, 1, 1]
for dt in ["f", "d", "g"]:
logxf = np.array(x, dtype=dt)
logyf = np.array(y, dtype=dt)
logzf = np.array(z, dtype=dt)
assert_equal(np.logaddexp2(logxf, logyf), logzf)
示例11: increment
def increment(self, value_added=1., event_time=None):
"""
Increment frecency, with value_added weighted according to time of observation.
* *value_added* is the number or weight of current events to add to the Frecency counter. (e.g., 1 for one view)
* *event_time* can be used to set the time at which the new value was added; otherwise, the present time is used.
"""
if not event_time:
event_time = time.time()
log2_weight_added = (event_time - self.time0) / self.timescale + log2(value_added)
self.log2_value = logaddexp2(self.log2_value, log2_weight_added) # All calculations in log2 space to avoid overflow
示例12: test_inf
def test_inf(self) :
inf = np.inf
x = [inf, -inf, inf, -inf, inf, 1, -inf, 1]
y = [inf, inf, -inf, -inf, 1, inf, 1, -inf]
z = [inf, inf, inf, -inf, inf, inf, 1, 1]
with np.errstate(invalid='ignore'):
for dt in ['f', 'd', 'g'] :
logxf = np.array(x, dtype=dt)
logyf = np.array(y, dtype=dt)
logzf = np.array(z, dtype=dt)
assert_equal(np.logaddexp2(logxf, logyf), logzf)
示例13: test_inf
def test_inf(self):
err = np.seterr(invalid="ignore")
inf = np.inf
x = [inf, -inf, inf, -inf, inf, 1, -inf, 1]
y = [inf, inf, -inf, -inf, 1, inf, 1, -inf]
z = [inf, inf, inf, -inf, inf, inf, 1, 1]
try:
for dt in ["f", "d", "g"]:
logxf = np.array(x, dtype=dt)
logyf = np.array(y, dtype=dt)
logzf = np.array(z, dtype=dt)
assert_equal(np.logaddexp2(logxf, logyf), logzf)
finally:
np.seterr(**err)
示例14: add_inplace
def add_inplace(self, x, y):
x *= np.log2(base)
y2 = y * np.log2(base)
np.logaddexp2(x, y2, x)
x *= self.log(2)
return x
示例15: test_logaddexp2
def test_logaddexp2(self):
import math
import sys
float_max, float_min = sys.float_info.max, sys.float_info.min
from numpy import logaddexp2
log2 = math.log(2)
# From the numpy documentation
prob1 = math.log(1e-50) / log2
prob2 = math.log(2.5e-50) / log2
prob12 = logaddexp2(prob1, prob2)
assert math.fabs(-164.28904982231052 - prob12) < 0.000000000001
assert logaddexp2(0, 0) == 1
assert logaddexp2(float("-inf"), 0) == 0
assert logaddexp2(float_max, float_max) == float_max
assert logaddexp2(float_min, float_min) == 1.0
assert math.isnan(logaddexp2(float("nan"), 1))
assert math.isnan(logaddexp2(1, float("nan")))
assert math.isnan(logaddexp2(float("nan"), float("inf")))
assert math.isnan(logaddexp2(float("inf"), float("nan")))
assert logaddexp2(float("-inf"), float("-inf")) == float("-inf")
assert logaddexp2(float("-inf"), float("inf")) == float("inf")
assert logaddexp2(float("inf"), float("-inf")) == float("inf")
assert logaddexp2(float("inf"), float("inf")) == float("inf")