ich habe folgenden Code gefunden, den ich bis auf einen Ausdruck soweit nachvollziehen kann:
Code: Alles auswählen
from collections import Counter, defaultdict
inputs = [({'label': 'mid', 'language':'python'}, True),
({'label': 'mid', 'language':'html'}, False),
({'label': 'jun', 'language':'python'}, True),
({'label': 'sen', 'language':'c'}, True),
({'label': 'jun', 'language':'c'}, False)]
def group_by(items, key_fn):
"""returns a defaultdict(list), where each input item
is in the list whose key is key_fn(item)"""
groups = defaultdict(list)
for item in items:
print(item)
key = key_fn(item)
print(key)
groups[key].append(item)
return groups
def partition_by(inputs, attribute):
"""returns a dict of inputs partitioned by the attribute
each input is a pair (attribute_dict, label)"""
return group_by(inputs, lambda x: x[0][attribute])
def partition_entropy_by(inputs,attribute):
"""computes the entropy corresponding to the given partition"""
partitions = partition_by(inputs, attribute)
print(partitions)
partition_entropy_by(inputs, 'label')
Code: Alles auswählen
lambda x: x[0][attribute]