Esempio n. 1
0
 def run(self):
     g = InfoCluster(affinity='nearest_neighbors', n_neighbors=8)
     g.fit(self.pos_list)
     self.partition_list = g.partition_list
     self.critical_values = g.critical_values
     self.get_category = g.get_category
     self.g = g
Esempio n. 2
0
 def run(self):
     g = InfoCluster(gamma=self._gamma)
     g.fit(self.pos_list)
     self.partition_list = g.partition_list
     self.critical_values = g.critical_values
     self.get_category = g.get_category
     self.g = g
    def predict_with_same_ancestor(self, tree_node, node_index_i, node_index_j,
                                   weight_added):
        # rerun the info-clustering algorithm, if tree_node has many children, the speed is very slow
        child_node_list = tree_node.get_children()
        n_nodes = len(child_node_list)
        affinity_matrix = np.zeros([n_nodes, n_nodes])
        for ii in range(n_nodes):
            for jj in range(ii + 1, n_nodes):
                affinity_matrix[ii,
                                jj] = self.get_weight(child_node_list[ii],
                                                      child_node_list[jj])

        node_index_ii = -1
        node_index_jj = -1
        for ii in range(n_nodes):
            if (str(node_index_i) == child_node_list[ii].name):
                node_index_ii = ii
            if (str(node_index_j) == child_node_list[ii].name):
                node_index_jj = ii

        if (node_index_ii < node_index_jj):
            affinity_matrix[node_index_ii, node_index_jj] = weight_added
        else:
            affinity_matrix[node_index_jj, node_index_ii] = weight_added
        new_ic = InfoCluster(affinity='precomputed')
        new_ic.fit(affinity_matrix)
        is_solution_trivial = len(new_ic.critical_values) == 1
        return is_solution_trivial
 def test_cv_value(self):
     g = nx.Graph()  # undirected graph
     g.add_edge(0, 1, weight=1)
     g.add_edge(1, 2, weight=1)
     g.add_edge(0, 2, weight=5)
     ic = InfoCluster(
         affinity='precomputed')  # use precomputed graph structure
     ic.fit(g)
     self.assertAlmostEqual(ic.tree.cv, 2.0)
     self.assertAlmostEqual(ic.tree.get_children()[0].cv, 5.0)
 def test_workflow(self):
     g = nx.Graph()  # undirected graph
     g.add_edge(0, 1, weight=1)
     g.add_edge(1, 2, weight=1)
     g.add_edge(0, 2, weight=5)
     info_cluster_instance = InfoCluster(affinity='precomputed')
     info_cluster_instance.fit(g)
     info_cluster_instance.print_hierarchical_tree()
     self.assertEqual(info_cluster_instance.get_tree_depth(), 2)
 def test_4_point(self):
     g = nx.DiGraph()
     g.add_edge(0, 2, weight=2)
     g.add_edge(0, 1, weight=5)
     g.add_edge(0, 3, weight=1)
     g.add_edge(1, 2, weight=2)
     g.add_edge(1, 3, weight=1)
     info_cluster_instance = InfoCluster(affinity='precomputed')
     info_cluster_instance.fit(g)
     info_cluster_instance.print_hierarchical_tree()
     self.assertEqual(info_cluster_instance.get_tree_depth(), 3)
 def test_10_point(self):
     point_list = np.array([[0.8, 1.3], [0.9, 1.2], [0.9, -0.5],
                            [0.7, -0.6], [1.0, -0.6], [-1.3, -0.5],
                            [-1.0, -0.6], [-1.1, -0.8], [-2.7, -0.8],
                            [-2.6, -0.9]])
     info_cluster_instance = InfoCluster(gamma=1, affinity="rbf")
     info_cluster_instance.fit(point_list)
     info_cluster_instance.print_hierarchical_tree()
     # print(info_cluster_instance.critical_values)
     print(info_cluster_instance.partition_list)