Skip to content

Instantly share code, notes, and snippets.

@halfak
Last active June 5, 2018 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfak/b925a2d45a3903a3e10dc5d6cd7c01b1 to your computer and use it in GitHub Desktop.
Save halfak/b925a2d45a3903a3e10dc5d6cd7c01b1 to your computer and use it in GitHub Desktop.
def weighted_sum(score, weights):
return sum(weights[cls] * proba
for cls, proba in score['probability'].items())
def normalized_weighted_sum(score, weights):
return weighted_sum(score, weights) / len(weights)
enwiki_weights = {
'Stub': 1,
'Start': 2,
'C': 3,
'B': 4,
'GA': 5,
'FA': 6
}
normalized_weighted_sum(
{'probability': {'Stub': 0.5,
'Start': 0.3,
'C': 0.1,
'B': 0.05,
'GA': 0.05,
'FA': 0}},
enwiki_weights)
0.308 * max(enwiki_weights.values())
>>> def weighted_sum(score, weights):
... return sum(weights[cls] * proba
... for cls, proba in score['probability'].items())
...
>>> def normalized_weighted_sum(score, weights):
... return weighted_sum(score, weights) / len(weights)
...
>>> enwiki_weights = {
... 'Stub': 1,
... 'Start': 2,
... 'C': 3,
... 'B': 4,
... 'GA': 5,
... 'FA': 6
... }
>>>
>>> normalized_weighted_sum(
... {'probability': {'Stub': 0.5,
... 'Start': 0.3,
... 'C': 0.1,
... 'B': 0.05,
... 'GA': 0.05,
... 'FA': 0}},
... enwiki_weights)
0.30833333333333335
>>>
>>> 0.308 * max(enwiki_weights.values())
1.8479999999999999
@adamwight
Copy link

I have questions about this formula. Are the per-class probabilities guaranteed to sum to 1? That would be strange, since these predictions aren't mutually exclusive. If they don't sum to 1, then our normalization doesn't work as we expect, for example [0, 0, 0, 0, 1.0, 1.0] would sum to 11 and normalize to 11/6. Shouldn't the denominator be the sum of all weights?

@adamwight
Copy link

Chatting now, Aaron pointed out my mistake: this is a one-vs-rest classifier, unlike drafttopic. The sum of probabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment