Skip to content

Instantly share code, notes, and snippets.

@fredcallaway
Last active November 14, 2016 06:01
Show Gist options
  • Select an option

  • Save fredcallaway/8a1eacb6e3baf642d6063de9f998c536 to your computer and use it in GitHub Desktop.

Select an option

Save fredcallaway/8a1eacb6e3baf642d6063de9f998c536 to your computer and use it in GitHub Desktop.
from itertools import chain, combinations
def powerset(iterable):
xs = list(iterable)
return chain.from_iterable( combinations(xs,n) for n in range(len(xs)+1) )
# Features are binary. An item either has or does not have every feature.
features = 'abcde'
hypotheses_sets = {
'multiple features': list(powerset(features)),
'single feature': list(features),
}
# Without loss of generality, the target item is assumed
# to be have all features. We consider one item
# for each possible number of shared features. The item is
# represented as a string of features the item has.
items = [features[:i] for i in range(len(features)+1)]
def compatible(hypothesis, x):
return all(feature in x for feature in hypothesis)
for name, hypotheses in hypotheses_sets.items():
print('\n----- {} -----'.format(name))
print('shared features p(generalize)')
for item in items:
num_compatible = sum(1 for h in hypotheses if compatible(h, item))
p_gen = num_compatible / len(hypotheses)
print(len(item), ' ', p_gen)
"""
Output:
----- single feature -----
shared features p(generalize)
0 0.0
1 0.2
2 0.4
3 0.6
4 0.8
5 1.0
----- multiple features -----
shared features p(generalize)
0 0.03125
1 0.0625
2 0.125
3 0.25
4 0.5
5 1.0
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment