Esempio n. 1
0
def compute_xmax_cnet(cnet_module, depth, dataset, x_var, e_var):

    x_map_list = []  # store the map tuple
    evid_flag = np.zeros(dataset.shape[1],
                         dtype=int)  # value = 1 means evidence
    evid_flag[e_var] = 1

    cnet = CNET_dfs(cnet_module, dataset.shape[1])

    internal_list, leaf_list = cnet.get_node_list()

    for t in leaf_list:
        t.evar = np.where(evid_flag[t.ids] == 1)[0]

        t.ind_as_parent(t.evar)
        t.ind_as_child(t.evar)

    xmax_prob_arr = np.zeros(dataset.shape[0])

    for i in xrange(dataset.shape[0]):
        datapoint = dataset[i]
        xmax_prob_arr[i], x_map = cnet.instantiation(datapoint, e_var,
                                                     evid_flag, internal_list,
                                                     leaf_list)
        x_map_list.append(x_map)

    return xmax_prob_arr, np.asarray(x_map_list)
Esempio n. 2
0
def compute_xmax_bcnet(bcnet, dataset, x_var, e_var):

    x_map_list = []  # store the map tuple
    evid_flag = np.zeros(dataset.shape[1],
                         dtype=int)  # value = 1 means evidence
    evid_flag[e_var] = 1

    n_variables = dataset.shape[1]

    cm_weights = bcnet['cm_weights']
    log_weights = np.log(cm_weights)

    n_components = cm_weights.shape[0]

    bcnet_dict_list = []
    bcnet_internal_list = []
    bcnet_leaf_list = []
    for i in xrange(n_components):
        cn = bcnet['cnet_list'][i]
        cnet_component = CNET_dfs(cn, n_variables)
        bcnet_dict_list.append(cnet_component)

        internal_list, leaf_list = cnet_component.get_node_list()
        bcnet_internal_list.append(internal_list)
        bcnet_leaf_list.append(leaf_list)

    for i in xrange(len(bcnet_leaf_list)):
        for t in bcnet_leaf_list[i]:
            t.evar = np.where(evid_flag[t.ids] == 1)[0]
            t.ind_as_parent(t.evar)
            t.ind_as_child(t.evar)

    xmax_prob_arr = np.zeros(dataset.shape[0])

    for i in xrange(dataset.shape[0]):
        datapoint = dataset[i]
        xmap_temp = []
        xmax_prob_temp = np.zeros(n_components)

        for j in xrange(n_components):

            cnet = bcnet_dict_list[j]
            xmax_prob_temp[j], x_map = cnet.instantiation(
                datapoint, e_var, evid_flag, bcnet_internal_list[j],
                bcnet_leaf_list[j])
            xmap_temp.append(x_map)

        xmax_prob_temp += log_weights
        best_ind = np.argmax(xmax_prob_temp)
        # find the best one
        x_map_list.append(xmap_temp[best_ind])
        xmax_prob_arr[i] = xmax_prob_temp[best_ind]

    return xmax_prob_arr, np.asarray(x_map_list)
Esempio n. 3
0
    def structure_redefine(self, load_info):

        self.weights = load_info.mixture_weight
        self.n_components = load_info.n_components
        for cn in load_info.cnet_list:
            main_dict = {}
            utilM.save_cutset(main_dict,
                              cn.tree,
                              np.arange(self.n_variable),
                              ccpt_flag=True)
            cnet_component = CNET_dfs(main_dict, self.n_variable)
            self.cnet_dict_list.append(cnet_component)

            internal_list, leaf_list = cnet_component.get_node_list()
            self.internal_list.append(internal_list)
            self.leaf_list.append(leaf_list)
Esempio n. 4
0
def compute_xmax_bcnet(bcnet, dataset, x_var, e_var):

    #print ('In compute_xmax_bcnet')
    x_map_list = []  # store the map tuple
    evid_flag = np.zeros(dataset.shape[1],
                         dtype=int)  # value = 1 means evidence
    evid_flag[e_var] = 1

    n_variables = dataset.shape[1]
    #    cm_weights = np.loadtxt(bcnet_dir +'_component_weights.txt')
    #    log_weights = np.log(cm_weights)
    #
    #    n_components = cm_weights.shape[0]
    #
    #    bcnet_dict_list = []
    #    bcnet_internal_list =[]
    #    bcnet_leaf_list =[]
    #    for i in xrange(n_components):
    #        cn_file = bcnet_dir +'_' +str(i) + '.npz'
    #        cn = np.load(cn_file)['module'].item()
    #        cnet_component = CNET_dfs(cn,n_variables)
    #        bcnet_dict_list.append(cnet_component)
    #
    #        internal_list, leaf_list = cnet_component.get_node_list()
    #        bcnet_internal_list.append(internal_list)
    #        bcnet_leaf_list.append(leaf_list)

    cm_weights = bcnet['cm_weights']
    log_weights = np.log(cm_weights)

    n_components = cm_weights.shape[0]

    bcnet_dict_list = []
    bcnet_internal_list = []
    bcnet_leaf_list = []
    for i in xrange(n_components):
        cn = bcnet['cnet_list'][i]
        cnet_component = CNET_dfs(cn, n_variables)
        bcnet_dict_list.append(cnet_component)

        internal_list, leaf_list = cnet_component.get_node_list()
        bcnet_internal_list.append(internal_list)
        bcnet_leaf_list.append(leaf_list)

    #print ('log_weights: ', log_weights)

    for i in xrange(len(bcnet_leaf_list)):
        for t in bcnet_leaf_list[i]:
            t.evar = np.where(evid_flag[t.ids] == 1)[0]
            t.ind_as_parent(t.evar)
            t.ind_as_child(t.evar)

    xmax_prob_arr = np.zeros(dataset.shape[0])

    for i in xrange(dataset.shape[0]):
        datapoint = dataset[i]
        xmap_temp = []
        xmax_prob_temp = np.zeros(n_components)

        for j in xrange(n_components):

            cnet = bcnet_dict_list[j]
            xmax_prob_temp[j], x_map = cnet.instantiation(
                datapoint, e_var, evid_flag, bcnet_internal_list[j],
                bcnet_leaf_list[j])
            xmap_temp.append(x_map)

        #print ('xmax_prob_temp:', xmax_prob_temp)
        xmax_prob_temp += log_weights
        #print ('xmax_prob_temp:', xmax_prob_temp)
        best_ind = np.argmax(xmax_prob_temp)
        #print ('best_ind: ', best_ind)
        # find the best one
        x_map_list.append(xmap_temp[best_ind])
        xmax_prob_arr[i] = xmax_prob_temp[best_ind]

    return xmax_prob_arr, np.asarray(x_map_list)