Beispiel #1
0
    def cross_validate(self, real_graphs):
        """
        Deprecated
        :param real_graphs:
        :return:
        """
        vector = FeatureVector()
        target_vectors = list()
        classifier = SVC(probability=True)
        classes = list()
        for generator in self.generators:
            for i in range(self.class_count):
                self.logger.debug('Current class number: #{0}'.format(i))
                tmp_g = get_giant_component(generator.generate())
                self.logger.debug(tmp_g.toString())
                target_vectors.append(
                    vector.build_vector_for_graph_as_list(tmp_g))
                classes.append(generator.get_name())

        assert len(real_graphs) != self.class_count, 'Size of real_graphs list should be equal to class_count. ' \
                                                     'Now real_graphs {0} and class_count {1}'.format(len(real_graphs),
                                                                                                      self.class_count)

        for real_graph in real_graphs:
            tmp_g = get_giant_component(real_graph)
            target_vectors.append(vector.build_vector_for_graph_as_list(tmp_g))
            classes.append('real_graph')

        cross_val_predict(classifier, target_vectors, classes, cv=3)
Beispiel #2
0
def gather_real_features(sample_count, node_count, start_position):
    collection = gateway.real_features()
    loader = MongoDBLoader()
    features = FeatureVector()
    for number in range(start_position, sample_count):
        print(number)
        component = get_giant_component(
            loader.load_real_graph_part(node_count, number + 1))
        overview(component)
        collection.insert_one(features.build_vector_for_graph(component))
Beispiel #3
0
def gather_features(sample_count, start_position, generator):
    collection_features = gateway.get_collection(generator.get_name() +
                                                 feature_collection_suffix)
    collection_graph = gateway.get_collection(generator.get_name() + '_graphs')
    feature_vector = FeatureVector()
    for number in range(start_position, sample_count):
        component = get_giant_component(generator.generate())
        overview(component)
        MongoDBStorage().storeGraph(collection_graph, component)
        collection_features.insert_one(
            feature_vector.build_vector_for_graph(component))
Beispiel #4
0
def gather_br_features(sample_count, start_position, nodes_count=79999):
    features_collection = gateway.get_collection("BR_features")
    br_collection = gateway.get_collection("bollobas_riordan_30000")
    loader = MongoDBLoader()
    features = FeatureVector()
    for number in range(start_position, sample_count):
        graph = loader.load_one_from_collection(number, br_collection)
        component = get_giant_component(graph)
        component.removeSelfLoops()
        overview(component)
        #features.get_features[9].get_value(component)
        features_collection.insert_one(
            features.build_vector_for_graph(component))
Beispiel #5
0
 def build_svc_classifier(self):
     vector = FeatureVector()
     target_vectors = list()
     classifier = SVC(probability=True)
     classes = list()
     for generator in self.generators:
         for i in range(self.class_count):
             self.logger.debug('Current class number: #{0}'.format(i))
             tmp_g = get_giant_component(generator.generate())
             self.logger.debug(tmp_g.toString())
             target_vectors.append(
                 vector.build_vector_for_graph_as_list(tmp_g))
             classes.append(generator.get_name())
     classifier.fit(target_vectors, classes)
     return classifier
Beispiel #6
0
def gather_cl_features(sample_count, node_count, start_position):
    d = []
    loader = MongoDBLoader()
    for number in range(sample_count):
        graph = loader.load_real_graph_part(node_count, number + 1)
        d.append([graph.degree(v) for v in graph.nodes()])

    feature_vector = FeatureVector()
    for number in range(start_position, sample_count):
        generator = CLGenerator(d[number])
        collection = gateway.get_collection(generator.get_name() +
                                            feature_collection_suffix)
        collection_graph = gateway.get_collection(generator.get_name() +
                                                  '_graphs')
        component = get_giant_component(generator.generate())
        overview(component)
        MongoDBStorage().storeGraph(collection_graph, component)
        collection.insert_one(feature_vector.build_vector_for_graph(component))
Beispiel #7
0
def gather_specific_feature(feature_name, model, sample_count, start_position):
    vector = FeatureVector()
    feature = vector.get_feature(feature_name)
    graph_collection = gateway.get_collection(model + '_graphs')
    feature_collection = gateway.get_collection(model +
                                                feature_collection_suffix)

    loader = MongoDBLoader()
    for number in range(start_position, sample_count):
        graph = loader.load_one_from_collection(number, graph_collection)
        component = get_giant_component(graph)
        value = feature.get_value(component)

        document_id = feature_collection.find().skip(number).limit(1)[0]['_id']
        feature_collection.update({"_id": document_id},
                                  {"$set": {
                                      feature_name: value
                                  }})
Beispiel #8
0
    def classify(self):
        self.logger.info("Loading...")
        graph_set = MongoDBLoader().load_as_slice(size=self.class_count,
                                                  item_size=self.hub_count)
        features = FeatureVector()

        classifier = self.build_svc_classifier()
        res = []
        for index, graph in enumerate(graph_set):
            self.logger.info("Components")
            component = get_giant_component(graph)
            vector = [features.build_vector_for_graph_as_list(component)]
            proba = classifier.predict_proba(vector)
            self.logger.debug("Result of classification for sample {0}: {1}",
                              index, proba)
            res.append(proba)

            self.logger.info("****************************************")
        self.logger.info("Done")

        return res