Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def harmonic_mean(args):
if "harmonic_mean" not in dir(statistics):
return builtins.len(args) / sum([1 / x for x in args])
return statistics.harmonic_mean(args)
def get(self):
return statistics.harmonic_mean((self.precision.get(), self.recall.get()))
# Macro level calculation
macroPrecision = 0
macroRecall = 0
f1_list = []
# We ignore the "Others" class during the calculation of Precision, Recall and F1
for c in range(NUM_EMO-1):
precision = truePositives[c] / (truePositives[c] + falsePositives[c])
macroPrecision += precision
recall = truePositives[c] / (truePositives[c] + falseNegatives[c])
macroRecall += recall
f1 = (2 * recall * precision) / (precision + recall) if (precision + recall) > 0 else 0
f1_list.append(f1)
print("Class %s : Precision : %.3f, Recall : %.3f, F1 : %.3f" % (EMOS[c], precision, recall, f1))
print('Harmonic Mean: ',
s.harmonic_mean(f1_list))
macroPrecision /= 3
macroRecall /= 3
macroF1 = (2 * macroRecall * macroPrecision) / (macroPrecision + macroRecall) \
if (macroPrecision + macroRecall) > 0 else 0
print("Ignoring the Others class, Macro Precision : %.4f, Macro Recall : %.4f, Macro F1 : %.4f" % (
macroPrecision, macroRecall, macroF1))
# Micro level calculation
truePositives = truePositives[1:].sum()
falsePositives = falsePositives[1:].sum()
falseNegatives = falseNegatives[1:].sum()
print("Ignoring the Others class, Micro TP : %d, FP : %d, FN : %d"
% (truePositives, falsePositives, falseNegatives))
def _within_perc(values, perc):
mean = statistics.harmonic_mean(values)
print('Values: ', values)
print('Harmonic mean: ', mean)
diffs = (_perc_diff(mean, i) for i in values)
diffs = list(diffs)
print([f'{d:.3f}' for d in diffs])
return all(d < perc for d in diffs)
:param int runs: How many samples to take.
:param float sleep: How much time to sleep in between the runs. Example
use: Maybe the board does not discharge quick enough so it can cause
troubles when the subsecuent testcase run tries to boot again the board
"""
elapsed_times = []
for n in range(runs):
elapsed_time, _ = time_testcase(testcase, *args, **kwargs)
elapsed_times.append(elapsed_time)
time.sleep(sleep)
results = TimingResults(
statistics.mean(elapsed_times),
statistics.harmonic_mean(elapsed_times),
statistics.median(elapsed_times),
statistics.pvariance(elapsed_times),
statistics.pstdev(elapsed_times),
)
tbot.log.message(
f"""\
Timing Results:
{tbot.log.c('mean').green}: {results.mean}
{tbot.log.c('harmonic mean').green}: {results.harmonic_mean}
{tbot.log.c('median').green}: {results.median}
{tbot.log.c('variance').green}: {results.pvariance}
{tbot.log.c('standard deviation').green}: {results.pstdev}
"""
harmonic_mean = float('NaN')
else:
if start is None:
start = i
end = i + 1
harmonic_mean = statistics.harmonic_mean(df[price][start:end])
harmonic_mean_list.append(harmonic_mean)
i += 1
else:
while i < len(df[price]):
if i + 1 < n:
harmonic_mean = float('NaN')
else:
start = i + 1 - n
end = i + 1
harmonic_mean = statistics.harmonic_mean(df[price][start:end])
harmonic_mean_list.append(harmonic_mean)
i += 1
return harmonic_mean_list
def mean_harmonic(self):
return statistics.harmonic_mean(self.price)