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 guess(representation, sims, xi, a, a_, b):
sa = sims[xi[a]]
sa_ = sims[xi[a_]]
sb = sims[xi[b]]
add_sim = -sa+sa_+sb
if a in representation.wi:
add_sim[representation.wi[a]] = 0
if a_ in representation.wi:
add_sim[representation.wi[a_]] = 0
if b in representation.wi:
add_sim[representation.wi[b]] = 0
b_add = representation.iw[np.nanargmax(add_sim)]
mul_sim = sa_*sb*np.reciprocal(sa+0.01)
if a in representation.wi:
mul_sim[representation.wi[a]] = 0
if a_ in representation.wi:
mul_sim[representation.wi[a_]] = 0
if b in representation.wi:
mul_sim[representation.wi[b]] = 0
b_mul = representation.iw[np.nanargmax(mul_sim)]
return b_add, b_mul
Example 2
def guess(representation, sims, xi, a, a_, b):
sa = sims[xi[a]]
sa_ = sims[xi[a_]]
sb = sims[xi[b]]
add_sim = -sa+sa_+sb
if a in representation.wi:
add_sim[representation.wi[a]] = 0
if a_ in representation.wi:
add_sim[representation.wi[a_]] = 0
if b in representation.wi:
add_sim[representation.wi[b]] = 0
b_add = representation.iw[np.nanargmax(add_sim)]
mul_sim = sa_*sb*np.reciprocal(sa+0.01)
if a in representation.wi:
mul_sim[representation.wi[a]] = 0
if a_ in representation.wi:
mul_sim[representation.wi[a_]] = 0
if b in representation.wi:
mul_sim[representation.wi[b]] = 0
b_mul = representation.iw[np.nanargmax(mul_sim)]
return b_add, b_mul
Example 3
def test_nanargmax(self):
tgt = np.argmax(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmax(mat), tgt)
Example 4
def test_nanargmax(self):
tgt = np.argmax(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmax(mat), tgt)
Example 5
def generalized_esd(x, r, alpha=0.05, method='mean'):
"""Generalized ESD test for outliers
(http://www.itl.nist.gov/div898/handbook/eda/section3/eda35h3.htm).
Args:
x (numpy.ndarray): the data
r (int): max number of outliers
alpha (float): the signifiance level
method (str): 'median' or 'mean'
Returns:
list[int]: list of the index of outliers
"""
x = np.asarray(x, dtype=np.float64)
fn = __get_pd_median if method == 'median' else __get_pd_mean
NaN = float('nan')
outliers = []
N = len(x)
for i in range(1, r + 1):
if np.any(~np.isnan(x)):
m, e = fn(x)
if e != 0.:
y = np.abs(x - m)
j = np.nanargmax(y)
R = y[j]
lam = __get_lambda_critical(N, i, alpha)
if R > lam * e:
outliers.append(j)
x[j] = NaN
else:
break
else:
break
else:
break
return outliers
Example 6
def _select_best_score(scores, args):
return np.nanargmax(np.array(scores))
Example 7
def _select_best_measure_index(curr_measures, args):
idx = None
try:
if args.measure == 'aicc':
# The best score for AICc is the minimum.
idx = np.nanargmin(curr_measures)
elif args.measure in ['hmm-distance', 'wasserstein', 'mahalanobis']:
# The best score for the l-d measure is the maximum.
idx = np.nanargmax(curr_measures)
except:
idx = random.choice(range(len(curr_measures)))
assert idx is not None
return idx
Example 8
def test_nanargmax(self):
tgt = np.argmax(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmax(mat), tgt)
Example 9
def choose_arm(x, experts, explore):
n_arms = len(experts)
# make predictions
preds = [expert.predict(x) for expert in experts]
# get best arm
arm_max = np.nanargmax(preds)
# create arm selection probabilities
P = [(1-explore)*(arm==arm_max) + explore/n_arms for arm in range(n_arms)]
# select an arm
chosen_arm = np.random.choice(np.arange(n_arms), p=P)
pred = preds[chosen_arm]
return chosen_arm, pred
Example 10
def predict_ana( model, a, a2, b, realb2 ):
questWordIndices = [ model.word2id[x] for x in (a,a2,b) ]
# b2 is effectively iterating through the vocab. The row is all the cosine values
b2a2 = model.sim_row(a2)
b2a = model.sim_row(a)
b2b = model.sim_row(b)
addsims = b2a2 - b2a + b2b
addsims[questWordIndices] = -10000
iadd = np.nanargmax(addsims)
b2add = model.vocab[iadd]
# For debugging purposes
ia = model.word2id[a]
ia2 = model.word2id[a2]
ib = model.word2id[b]
ib2 = model.word2id[realb2]
realaddsim = addsims[ib2]
mulsims = ( b2a2 + 1 ) * ( b2b + 1 ) / ( b2a + 1.001 )
mulsims[questWordIndices] = -10000
imul = np.nanargmax(mulsims)
b2mul = model.vocab[imul]
return b2add, b2mul
Example 11
def test_nanargmax(self):
tgt = np.argmax(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmax(mat), tgt)
Example 12
def test_nanargmax(self):
tgt = np.argmax(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmax(mat), tgt)
Example 13
def decode_location(likelihood, pos_centers, time_centers):
"""Finds the decoded location based on the centers of the position bins.
Parameters
----------
likelihood : np.array
With shape(n_timebins, n_positionbins)
pos_centers : np.array
time_centers : np.array
Returns
-------
decoded : nept.Position
Estimate of decoded position.
"""
prob_rows = np.sum(np.isnan(likelihood), axis=1) < likelihood.shape[1]
max_decoded_idx = np.nanargmax(likelihood[prob_rows], axis=1)
prob_decoded = pos_centers[max_decoded_idx]
decoded_pos = np.empty((likelihood.shape[0], pos_centers.shape[1])) * np.nan
decoded_pos[prob_rows] = prob_decoded
decoded_pos = np.squeeze(decoded_pos)
return nept.Position(decoded_pos, time_centers)
Example 14
def maxabs(trace, starttime=None, endtime=None):
"""Returns the maximum of the absolute values of `trace`, and its occurrence time.
In other words, returns the point `(time, value)` where `value = max(abs(trace.data))`
and time (`UTCDateTime`) is the time occurrence of `value`
:param trace: the input obspy.core.Trace
:param starttime: (`obspy.UTCDateTime`) the start time (None or missing defaults to the trace
end): the maximum of the trace `abs` will be searched *from* this time. This argument,
if provided, does not affect the
returned `time` which will be always relative to the trace passed as argument
:param endtime: an obspy UTCDateTime object (or any value
`UTCDateTime` accepts, e.g. integer / `datetime` object) denoting
the end time (None or missing defaults to the trace end): the maximum of the trace `abs`
will be searched *until* this time
:return: the tuple (time, value) where `value = max(abs(trace.data))`, and time is
the value occurrence (`UTCDateTime`)
:return: the tuple `(time_of_max_abs, max_abs)`
"""
original_stime = None if starttime is None else trace.stats.starttime
if starttime is not None or endtime is not None:
# from the docs: "this returns a New Trace object
# Does not copy data but just passes a reference to it"
trace = trace.slice(starttime, endtime)
if trace.stats.npts < 1:
return np.nan
idx = np.nanargmax(np.abs(trace.data))
val = trace.data[idx]
tdelta = 0 if original_stime is None else trace.stats.starttime - original_stime
time = timeof(trace, idx) + tdelta
return (time, val)
Example 15
def optimize_threshold_with_roc(roc, thresholds, criterion='dist'):
if roc.shape[1] > roc.shape[0]:
roc = roc.T
assert(roc.shape[0] == thresholds.shape[0])
if criterion == 'margin':
scores = roc[:,1]-roc[:,0]
else:
scores = -cdist(np.array([[0,1]]), roc)
ti = np.nanargmax(scores)
return thresholds[ti], ti
Example 16
def optimize_threshold_with_prc(prc, thresholds, criterion='min'):
prc[np.isnan(prc)] = 0
if prc.shape[1] > prc.shape[0]:
prc = prc.T
assert(prc.shape[0] == threshold