Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>from __future__ import division from itertools import izip, repeat import math def weighted_mean(values, weights=None): total = 0 number = 0 if weights is None: weights = repeat(1) for weight, value in izip(weights, values): total += weight * value number += weight return number and total / number xa = [1, 2, 3, 10, 1383, 0, 12, 9229, 2, 494, 10, 49] xb = [1, 1, 4, 12, 1100, 43, 9, 4848, 2, 454, 6, 9] print "Option 1, if you want bigger numbers to have a bigger effect on the score" weights = (math.sqrt(abs(a) * abs(b)) for a, b in izip(xa, xb)) scores = (abs(a) + abs(b) and abs(a - b) / (abs(a) + abs(b)) for a, b in izip(xa, xb)) final_score = weighted_mean(scores, weights) print "%.02f%%" % (final_score * 100) print "Option 2, if you want to have all numbers have the same effect on the score" scores = (abs(a) + abs(b) and abs(a - b) / (abs(a) + abs(b)) for a, b in izip(xa, xb)) final_score = weighted_mean(scores) print "%.02f%%" % (final_score * 100) </code></pre> <p>Of course, you can also use other kinds of weights, such as <code>(abs(a) + abs(b)) / 2</code>, depending on how you want to interpret a given difference.</p> <p>Loopless version of the second one:</p> <pre><code>xan = numpy.array(xa) xbn = numpy.array(xb) error_threshold = 0.000001 final_score = numpy.mean((abs(xan - xbn) + error_threshold) / (abs(xan) + abs(xbn) + error_threshold)) </code></pre> <p>Or the first:</p> <pre><code>scores = (abs(xan - xbn) + error_threshold) / (abs(xan) + abs(xbn) + error_threshold) weights = numpy.sqrt(abs(xan) * abs(xbn)) final_score = numpy.sum(scores * weights) / numpy.sum(weights) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload