python truediv_Python numpy.true_divide() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def test_zero_safe_divide(self):

from blmath.numerics.operations import zero_safe_divide

numerator = np.ones((5, 5))

numerator[3, 3] = 0.

denominator = np.ones((5, 5))

denominator[2, 2] = 0.

denominator[3, 3] = 0.

with warnings.catch_warnings():

warnings.simplefilter("ignore", RuntimeWarning)

true_divide = np.true_divide(numerator, denominator)

safe_divide = zero_safe_divide(numerator, denominator)

self.assertTrue(np.isinf(true_divide[2, 2]))

self.assertEqual(safe_divide[2, 2], 0.)

self.assertTrue(np.isnan(true_divide[3, 3]))

self.assertEqual(safe_divide[3, 3], 0.)

Example 2

def zero_safe_divide(a, b, default_error_value=0.):

"""Element-wise division that accounts for floating point errors.

Both invalid floating-point (e.g. 0. / 0.) and divide be zero errors are

suppressed. Resulting values (NaN and Inf respectively) are replaced with

`default_error_value`.

"""

import numpy as np

with np.errstate(invalid='ignore', divide='ignore'):

quotient = np.true_divide(a, b)

bad_value_indices = np.logical_or(

np.isnan(quotient), np.isinf(quotient))

quotient[bad_value_indices] = default_error_value

return quotient

Example 3

def V_short(self,eta):

sum0 = np.zeros(7,dtype=float)

sum1 = np.zeros(7,dtype=float)

for n1,n2 in product(range(self.N1+1),range(self.N2+1)):

wdo = comb(self.N1,n1,exact=True)*comb(self.N2,n2,exact=True)

wdox10 = comb(self.N1-1,n1,exact=True)*comb(self.N2,n2,exact=True)

wdox11 = comb(self.N1-1,n1-1,exact=True)*comb(self.N2,n2,exact=True)

wdox20 = comb(self.N1,n1,exact=True)*comb(self.N2-1,n2,exact=True)

wdox21 = comb(self.N1,n1,exact=True)*comb(self.N2-1,n2-1,exact=True)

w = np.asarray([wdox10,wdox20,wdox11,wdox21,wdo,wdo,wdo])

pz0,pz1 = self.p_n_given_z(n1,n2)

counts = [self.N1-n1,self.N2-n2,n1,n2,1,1,1]

Q = (eta*pz0*counts*(1-self.pZgivenA)+eta*pz1*counts*self.pZgivenA).sum()

ratio = np.nan_to_num(np.true_divide(pz0*(1-self.pZgivenA)+pz1*self.pZgivenA,Q))

sum0 += np.asfarray(w*pz0*ratio)

sum1 += np.asfarray(w*pz1*ratio)

result = self.pZgivenA*sum1+(1-self.pZgivenA)*sum0

return result

Example 4

def run(self,T,model):

if T <= model.K: # result is not defined if the horizon is shorter than the number of actions

self.best_action = None

return np.nan

actions = range(0,model.K)

self.trials = np.ones(model.K)

self.success = model.sample_multiple(actions,1)

for t in range(model.K,T):

arm = argmax_rand(self.upper_bound(t))

self.trials[arm] += 1

self.success[arm] +=model.sample_multiple(arm,1)

mu = np.true_divide(self.success,self.trials)

self.best_action = argmax_rand(mu)

return max(model.expected_rewards) - model.expected_rewards[self.best_action]

Example 5

def fit(self, x, y, verbose=True):

#setting data attributes for the model instance

X = tfidf_to_counts(x)

#splitting by target class so we can calculate the log-count ratio

X_pos = X[np.where(y == 1)]

X_neg = X[np.where(y == 0)]

self.r = log_count_ratio(X_pos, X_neg)

#setting the npos and nneg variables

n_pos = X_pos.shape[0]

n_neg = X_neg.shape[0]

#getting the bais for the MNB model

self.nb_bias = np.log(np.true_divide(n_pos, n_neg))

#trains, tests, and assesses the performance of the model

Example 6

def test_NotImplemented_not_returned(self):

# See gh-5964 and gh-2091. Some of these functions are not operator

# related and were fixed for other reasons in the past.

binary_funcs = [

np.power, np.add, np.subtract, np.multiply, np.divide,

np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,

np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,

np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,

np.logical_and, np.logical_or, np.logical_xor, np.maximum,

np.minimum, np.mod

]

# These functions still return NotImplemented. Will be fixed in

# future.

# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

a = np.array('1')

b = 1

for f in binary_funcs:

assert_raises(TypeError, f, a, b)

Example 7

def __itruediv__(self, other):

"""

True divide self by other in-place.

"""

other_data = getdata(other)

dom_mask = _DomainSafeDivide().__call__(self._data, other_data)

other_mask = getmask(other)

new_mask = mask_or(other_mask, dom_mask)

# The following 3 lines control the domain filling

if dom_mask.any():

(_, fval) = ufunc_fills[np.true_divide]

other_data = np.where(dom_mask, fval, other_data)

self._mask |= new_mask

self._data.__itruediv__(np.where(self._mask, self.dtype.type(1),

other_data))

return self

Example 8

def load_files(avg_file, std_file):

# load files

with open(avg_file) as f:

avg = simplejson.load(f)

with open(std_file) as f:

std = simplejson.load(f)

std = np.array(std)

print std

std = np.true_divide(std, 2.)

print std

avg = np.array(avg)

avg_upper = avg + std

avg_lower = avg - std

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值