Пример #1
0
 def __init__(self, name, test, elements, n_categories):
     self.name = name
     self.test = test
     self.classifier = KMeans(self.test, 2, linspace(0,1,2).reshape(2,1))
     self.elements = elements
     self.categories = []
     self.relatives = []
Пример #2
0
class Trait(object):
    """
    Class storing information about each trait, i.e.
    the method which objects will be tested against
    and possible categories they can fit information

    Trait.test() takes an object as input and returns a float
    """

    def __init__(self, name, test, elements, n_categories):
        self.name = name
        self.test = test
        self.classifier = KMeans(self.test, 2, linspace(0,1,2).reshape(2,1))
        self.elements = elements
        self.categories = []
        self.relatives = []

    def __str__(self):
        return json.dumps(self, cls=TraitEncoder, indent=4)

    def __repr__(self):
        return str(self)

    def split(self):
        groups = self.classifier.split(self.elements)

        categories = [Category(
            self,
            "{}_{}".format(self.name, groups[label][0]),
            groups[label][0],  # mean
            groups[label][1],  # elements
            label  # label
        ) for label in groups]

        return categories

    def add_relative(self, relative):
        [self_category.add_relation(self_category.compute_relation(relative_category))
            for self_category in self.categories
            for relative_category in relative.categories]

        self.relatives.append(relative)