Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
>>> w = np.array([0.5, 0.1, 0.3, 0.8])
We specify the number of groups for which the weighted mean is computed.
>>> n = 2
Applying sum_by_n function
>>> sum_by_n(d, w, n)
array([ 5.9, 30. ])
"""
t = len(d)
h = t // n #must be floor!
d = d * w
return np.array([sum(d[i: i + h]) for i in range(0, t, h)])
def __getCardinalities(self):
ido = self.w.id_order
self.wc = np.array([self.w.cardinalities[ido[i]] for i in range(self.n)])
return self.wc
extra[id][-1] * 1.0 / extra[id][1])
trp_r = sorted(trp_r)
if not weighted:
return r[id], trp_r[0], trp_r[-1]
else:
trp_aw = self.aw[triples[0]]
extra_w = trp_aw[0] + (trp_aw[0] - trp_aw[-
1]) * (extra[id][-1] * 1.0 / extra[id][1])
return r[id], trp_r[0], trp_r[-1], self.aw[id], trp_aw[0] + extra_w
if not weighted:
lowest, highest = [], []
for trp in triples:
trp_r = np.sort(r[list(trp)])
lowest.append(trp_r[0])
highest.append(trp_r[-1])
return r[id], np.median(np.array(lowest)), np.median(np.array(highest))
if weighted:
lowest, highest = [], []
lowest_aw, highest_aw = [], []
for trp in triples:
trp_r = r[list(trp)]
dtype = [('r', '%s' % trp_r.dtype), ('w',
'%s' % self.aw.dtype)]
trp_r = np.array(list(zip(trp_r, list(trp))), dtype=dtype)
trp_r.sort(order='r')
lowest.append(trp_r['r'][0])
highest.append(trp_r['r'][-1])
lowest_aw.append(self.aw[int(round(trp_r['w'][0]))])
highest_aw.append(self.aw[int(round(trp_r['w'][-1]))])
wm_lowest = weighted_median(np.array(lowest), np.array(lowest_aw))
wm_highest = weighted_median(
np.array(highest), np.array(highest_aw))
>>> d = np.array([5,4,3,1,2])
Creating another array including weight values for the above integers.
The median of d will be decided with a consideration to these weight
values.
>>> w = np.array([10, 22, 9, 2, 5])
Applying weighted_median function
>>> weighted_median(d, w)
4
"""
dtype = [('w', '%s' % w.dtype), ('v', '%s' % d.dtype)]
d_w = np.array(list(zip(w, d)), dtype=dtype)
d_w.sort(order='v')
reordered_w = d_w['w'].cumsum()
cumsum_threshold = reordered_w[-1] * 1.0 / 2
median_inx = (reordered_w >= cumsum_threshold).nonzero()[0][0]
if reordered_w[median_inx] == cumsum_threshold and len(d) - 1 > median_inx:
return np.sort(d)[median_inx:median_inx + 2].mean()
return np.sort(d)[median_inx]
def __crand(self):
y = self.y
rGs = np.zeros((self.n, self.permutations))
n_1 = self.n - 1
rid = list(range(n_1))
prange = list(range(self.permutations))
k = self.w.max_neighbors + 1
rids = np.array([np.random.permutation(rid)[0:k] for i in prange])
ids = np.arange(self.w.n)
ido = self.w.id_order
wc = self.__getCardinalities()
if self.w_transform == "r":
den = np.array(wc) + self.star
else:
den = np.ones(self.w.n)
for i in range(self.w.n):
idsi = ids[ids != i]
np.random.shuffle(idsi)
yi_star = y[i] * self.star
wci = wc[i]
rGs[i] = (y[idsi[rids[:, 0:wci]]]).sum(1) + yi_star
rGs[i] = (np.array(rGs[i]) / den[i]) / (self.y_sum - (1 - self.star) * y[i])
self.rGs = rGs
def __search_headbanging_median(self):
r, tr = self.r, self.tr
new_r = []
for k in list(tr.keys()):
screens = self.__get_screens(
k, tr[k], weighted=(self.aw is not None))
new_r.append(self.__get_median_from_screens(screens))
self.r = np.array(new_r)
return r[id], trp_r[0], trp_r[-1], self.aw[id], trp_aw[0] + extra_w
if not weighted:
lowest, highest = [], []
for trp in triples:
trp_r = np.sort(r[list(trp)])
lowest.append(trp_r[0])
highest.append(trp_r[-1])
return r[id], np.median(np.array(lowest)), np.median(np.array(highest))
if weighted:
lowest, highest = [], []
lowest_aw, highest_aw = [], []
for trp in triples:
trp_r = r[list(trp)]
dtype = [('r', '%s' % trp_r.dtype), ('w',
'%s' % self.aw.dtype)]
trp_r = np.array(list(zip(trp_r, list(trp))), dtype=dtype)
trp_r.sort(order='r')
lowest.append(trp_r['r'][0])
highest.append(trp_r['r'][-1])
lowest_aw.append(self.aw[int(round(trp_r['w'][0]))])
highest_aw.append(self.aw[int(round(trp_r['w'][-1]))])
wm_lowest = weighted_median(np.array(lowest), np.array(lowest_aw))
wm_highest = weighted_median(
np.array(highest), np.array(highest_aw))
triple_members = flatten(triples, unique=False)
return r[id], wm_lowest, wm_highest, self.aw[id] * len(triples), self.aw[triple_members].sum()
def __crand(self):
y = self.y
rGs = np.zeros((self.n, self.permutations))
n_1 = self.n - 1
rid = list(range(n_1))
prange = list(range(self.permutations))
k = self.w.max_neighbors + 1
rids = np.array([np.random.permutation(rid)[0:k] for i in prange])
ids = np.arange(self.w.n)
ido = self.w.id_order
wc = self.__getCardinalities()
if self.w_transform == "r":
den = np.array(wc) + self.star
else:
den = np.ones(self.w.n)
for i in range(self.w.n):
idsi = ids[ids != i]
np.random.shuffle(idsi)
yi_star = y[i] * self.star
wci = wc[i]
rGs[i] = (y[idsi[rids[:, 0:wci]]]).sum(1) + yi_star
rGs[i] = (np.array(rGs[i]) / den[i]) / (self.y_sum - (1 - self.star) * y[i])
self.rGs = rGs