예제 #1
0
def plot_nmi_max(labels, pos, savepath):
    """
    plot nmi_max distribution of given library

    Parameters
    ----------
    :param labels:
    :param pos:
    :param savepath:
    """
    texts = []
    colors = []
    plot_labels = [None] * (len(labels) - _ADDITIONAL_RANGE)
    markers = ['o'] * (len(labels) - _ADDITIONAL_RANGE)
    for label in labels[0:-1]:
        cons = Metrics.normalized_max_mutual_info_score(label, labels[-1])
        texts.append(cons)
    cNorm = colors2.Normalize(vmin=min(texts[0:-_ADDITIONAL_RANGE+1]), vmax=max(texts[0:-_ADDITIONAL_RANGE+1]))
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=plt.get_cmap('CMRmap'))
    plot_labels.extend(_ADDITIONAL_NAMES)
    for text in texts[0:-_ADDITIONAL_RANGE+1]:
        colors.append(scalarMap.to_rgba(text))
    texts = map(_round_digits, texts)
    texts.extend(_ADDITIONAL_NAMES[-1:])
    colors.extend(_ADDITIONAL_COLORS)
    markers.extend(_ADDITIONAL_MARKERS)
    title = 'NMI distribution, ' + 'Max val = ' + str(max(texts[0:-_ADDITIONAL_RANGE])) +\
            ' ,Min k = ' + str(min(texts[0:-_ADDITIONAL_RANGE]))
    _plot_generalized_scatter(pos, colors, texts, markers, plot_labels, savepath, title=title)
    return
예제 #2
0
def comparison_ensemble_methods(dataset_name, library_name, eval_method=None):
    """
    get the performance of comparison methods (ensemble)

    :param dataset_name:
    :param library_name:
    :param eval_method:
    :return:
    """
    filename = _default_eval_path + dataset_name + '_ensemble_eval_' + time.strftime(
        '%Y-%m-%d_%H_%M_%S', time.localtime(time.time())) + '.csv'
    lib = np.loadtxt(_default_library_path + dataset_name + '/' +
                     library_name + '.res',
                     delimiter=',')
    with open(filename, 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(library_name)
        data, targets = exd.dataset[dataset_name]['data']()
        k = exd.dataset[dataset_name]['k']
        eval_methods = _default_ensemble_eval_methods if eval_method is None else eval_method
        print '[Ensemble Comparison]: Dataset: ' + str(dataset_name)
        print '[Ensemble Comparison]: Library: ' + str(library_name)
        print '[Ensemble Comparison]: Comparison Methods: ' + str(eval_methods)
        print '[Ensemble Comparison]: Real k is ' + str(k)
        for method in eval_methods:
            ensemble_label = _ensemble_methods[method](lib, N_clusters_max=k)
            performance = metrics.normalized_max_mutual_info_score(
                targets, ensemble_label)
            writer.writerow([method, str(performance)])
    return
def expected_consistency_selection_ensemble(
        labels,
        class_num,
        target,
        mlset,
        nlset,
        cons_type='must',
        ensemble_methods=_default_ensemble_methods,
        ease_factor=1):
    selected_labels = _expected_consistency_selection(labels,
                                                      mlset,
                                                      nlset,
                                                      cons_type=cons_type,
                                                      ease_factor=ease_factor)
    retVals = []
    retVals.append(ease_factor)
    retVals.append(selected_labels.shape[0])
    print('[INFO] Selected Solutions:' + str(selected_labels.shape[0]))
    for method in ensemble_methods:
        ensemble_labels = _ensemble_method[method](selected_labels,
                                                   N_clusters_max=class_num)
        ensemble_nmi = Metrics.normalized_max_mutual_info_score(
            ensemble_labels, target)
        retVals.append(ensemble_nmi)
        print('[INFO] Ensemble Method:' + method)
        print('[INFO] Performance:' + str(ensemble_nmi))
    return retVals
예제 #4
0
def evaluate_library(name,
                     path,
                     class_num,
                     target,
                     evaluate_methods=_default_evaluate_methods):
    """
    do evaluation for a given library

    :param name: name of the library
    :param path: path where the library is
    :param class_num: #real_classes
    :param target: real class label
    :param evaluate_methods: consensus functions used for evaluation

    Return
    ------
    :return: score of all consensus functions in a list
    """
    labels = np.loadtxt(path + name, delimiter=',')
    if not name.endswith('_pure.res'):
        labels = labels[0:-5]
    scores = []
    for method in evaluate_methods:
        ensemble_label = _ensemble_method[method](labels,
                                                  N_clusters_max=class_num)
        scores.append(
            Metrics.normalized_max_mutual_info_score(target, ensemble_label))
    return scores
def do_propagation_ensemble(library_folder,
                            library_name,
                            class_num,
                            target,
                            constraint_file,
                            logger,
                            alphas,
                            have_zero=True,
                            ensemble_method=_default_ensemble_method):
    logger.debug(
        '==========================================================================================='
    )
    logger.debug('-----------------Propagation Ensemble for library:' +
                 str(library_name) + '----------------')
    logger.debug('-----------------Have zero type = ' + str(have_zero) +
                 '-----------------------------------')
    logger.debug('-----------------Constraint File name = ' + constraint_file +
                 '----------------------------')

    labels = np.loadtxt(library_folder + library_name + '.res', delimiter=',')
    labels = labels.astype(int)

    ml, cl = io_func.read_constraints(constraint_file)

    hyperedges = ce.build_hypergraph_adjacency(labels)
    hyperedges = hyperedges.transpose()

    coas_matrix = hyperedges.dot(hyperedges.transpose())
    coas_matrix = np.squeeze(np.asarray(coas_matrix.todense()))
    coas_matrix = coas_matrix.astype(np.float32)
    coas_matrix /= np.max(coas_matrix)

    print coas_matrix

    nmis = []
    for alpha in alphas:
        logger.debug(
            '-------------------------->>>>>> PARAM START <<<<<<<---------------------------------'
        )
        propagated_coas_matrix = propagation_on_coassociation_matrix(
            coas_matrix, ml, cl, alpha)
        cur_nmis = []
        for method in ensemble_method:
            ensemble_label = _ensemble_method[method](propagated_coas_matrix,
                                                      labels.shape[0],
                                                      class_num)
            ensemble_nmi = Metrics.normalized_max_mutual_info_score(
                ensemble_label, target)
            logger.debug(method + ' alpha=' + str(alpha) + ', NMI=' +
                         str(ensemble_nmi))
            cur_nmis.append(ensemble_nmi)
        nmis.append(cur_nmis)
        logger.debug(
            '------------------------->>>>>> END OF THIS PARAM <<<<<<------------------------------'
        )
    logger.debug(
        '==========================================================================================='
    )
    return nmis
def k_selection_ensemble(labels,
                         k_threshold,
                         logger,
                         weighted=False,
                         alpha=0,
                         mlset=None,
                         nlset=None,
                         ctype='both'):
    """
    do selection ensemble using k as criteria
    clusteing with k smaller than k_threshold will be removed

    :param labels:
    :param k_threshold:
    :param logger:
    :param weighted: weighted version or not
    :param alpha: balance factor that control the importance of clustering/cluster
                  consistency in weights (weighted version only)
    :param mlset: cannot-link set (weighted version only)
    :param nlset: must-link set (weighted version only)
    :param ctype: type of consistency (weighted version only)
    :return:
    """
    k_value = []
    class_num = len(np.unique(labels[-1]))
    # select those clusterings that k larger than the threshold.
    for label in labels[0:-5]:
        k_value.append(len(np.unique(label)))
    k_value = np.array(k_value)
    idx = k_value.ravel() >= k_threshold
    selected_labels = labels[0:-5][idx]

    # weights
    con_per_cluster = []
    con_clustering = []
    if weighted:
        for label in selected_labels:
            con_per_cluster.append(
                Metrics.consistency_per_cluster(label,
                                                mlset,
                                                nlset,
                                                cons_type=ctype))
        for label in selected_labels:
            con_clustering.append(
                Metrics.consistency(label, mlset, nlset, cons_type=ctype))

    logger.debug('[K] Start consensus...shape=' + str(selected_labels.shape))
    logger.debug('[K] Average k is ' + str(np.mean(k_value[idx])))
    if weighted:
        logger.debug('[K] weighted consensus, alpha=' + str(alpha))

    label_CSPA = ce.cluster_ensembles_CSPAONLY(
        selected_labels,
        N_clusters_max=class_num,
        weighted=weighted,
        clustering_weights=con_clustering,
        cluster_level_weights=con_per_cluster,
        alpha=alpha)
    label_HGPA = ce.cluster_ensembles_HGPAONLY(
        selected_labels,
        N_clusters_max=class_num,
        weighted=weighted,
        clustering_weights=con_clustering,
        cluster_level_weights=con_per_cluster,
        alpha=alpha)
    label_MCLA = ce.cluster_ensembles_MCLAONLY(
        selected_labels,
        N_clusters_max=class_num,
        weighted=weighted,
        clustering_weights=con_clustering,
        cluster_level_weights=con_per_cluster,
        alpha=alpha)

    nmi_CSPA = Metrics.normalized_max_mutual_info_score(label_CSPA, labels[-1])
    nmi_HGPA = Metrics.normalized_max_mutual_info_score(label_HGPA, labels[-1])
    nmi_MCLA = Metrics.normalized_max_mutual_info_score(label_MCLA, labels[-1])
    logger.debug('CSPA performance:' + str(nmi_CSPA))
    logger.debug('HGPA performance:' + str(nmi_HGPA))
    logger.debug('MCLA performance:' + str(nmi_MCLA))
    logger.debug('--------------------------------------------')
    return
def consistency_selection_ensemble(labels,
                                   mlset,
                                   nlset,
                                   logger,
                                   must_threshold,
                                   cannot_threshold,
                                   normalized=True,
                                   weighted=False,
                                   weighted_type='both',
                                   alpha=1):
    """
    do selection ensemble using must/cannot consistency as criteria
    clusteing with k smaller than k_threshold will be removed

    :param labels:
    :param mlset:
    :param nlset:
    :param logger:
    :param must_threshold:
    :param cannot_threshold:
    :param normalized:
    :param weighted:
    :param weighted_type:
    :param alpha:
    :return:
    """
    class_num = len(np.unique(labels[-1]))
    must_consistencies = []
    cannot_consistencies = []
    clustering_weights = []
    cluster_level_weights = []
    k_value = []
    for label in labels[0:-5]:
        must_cons = Metrics.consistency(label, mlset, nlset, cons_type='must')
        cannot_cons = Metrics.consistency(label,
                                          mlset,
                                          nlset,
                                          cons_type='cannot')
        if weighted:
            clustering_weights.append(
                Metrics.consistency(label,
                                    mlset,
                                    nlset,
                                    cons_type=weighted_type))
            cluster_level_weights.append(
                Metrics.consistency_per_cluster(label,
                                                mlset,
                                                nlset,
                                                cons_type=weighted_type))
        must_consistencies.append(must_cons)
        cannot_consistencies.append(cannot_cons)
        k_value.append(len(np.unique(label)))
    if normalized:
        scaler = preprocessing.MinMaxScaler()
        must_consistencies = scaler.fit_transform(
            np.array(must_consistencies).reshape(-1, 1)).ravel()
        cannot_consistencies = scaler.fit_transform(
            np.array(cannot_consistencies).reshape(-1, 1)).ravel()
    idx = np.logical_and(must_consistencies >= must_threshold,
                         cannot_consistencies >= cannot_threshold)
    selected_labels = labels[0:-5][idx]
    k_value = np.array(k_value)[idx]
    logger.debug('[Consistency] Start consensus...shape=' +
                 str(selected_labels.shape))
    if selected_labels.shape[0] == 0:
        logger.debug('[Consistency] No clusterings are selected. Out.')
        return
    logger.debug('[Consistency] Average k is ' + str(np.mean(k_value)))
    label_CSPA = ce.cluster_ensembles_CSPAONLY(
        selected_labels,
        N_clusters_max=class_num,
        weighted=weighted,
        clustering_weights=clustering_weights,
        cluster_level_weights=cluster_level_weights,
        alpha=alpha)
    label_HGPA = ce.cluster_ensembles_HGPAONLY(
        selected_labels,
        N_clusters_max=class_num,
        weighted=weighted,
        clustering_weights=clustering_weights,
        cluster_level_weights=cluster_level_weights,
        alpha=alpha)
    label_MCLA = ce.cluster_ensembles_MCLAONLY(selected_labels,
                                               N_clusters_max=class_num)
    nmi_CSPA = Metrics.normalized_max_mutual_info_score(label_CSPA, labels[-1])
    nmi_HGPA = Metrics.normalized_max_mutual_info_score(label_HGPA, labels[-1])
    nmi_MCLA = Metrics.normalized_max_mutual_info_score(label_MCLA, labels[-1])
    logger.debug('CSPA performance:' + str(nmi_CSPA))
    logger.debug('HGPA performance:' + str(nmi_HGPA))
    logger.debug('MCLA performance:' + str(nmi_MCLA))
    return
예제 #8
0
import member_generation.library_generation as lg
import utils.io_func as io
import utils.settings as settings
import ensemble.Cluster_Ensembles as ce
import evaluation.Metrics as metrics

# 导入数据集,目前我有使用过的数据集都在utils.load_dataset模块中封装成函数了
# 调用时返回两个量,其一是特征矩阵,尺寸是(#Instances * #Features)
# 部分数据集内置了一些参数,如进行0-1规范化等,自行查看
name = 'Iris'
d, t = ld.load_iris()

# 生成聚类成员集的函数我都包装在member_generation.library_generation中
# 主要函数是generate_library,它可以提供random-subspace的聚类成员集生成
# 以及半监督的聚类成员集(目前有E2CP和COP_KMEANS两种半监督聚类方法)的生成
# 该函数返回的是library的名字,实际library保存在Results/[数据集名字]/
# 具体参数,请见函数注释说明,可能写的比较乱,如果不太明白再问
# p.s. 如果使用random-subspace的方式生成聚类成员,其生成方法主要是对样本or特征的随机采样
# 具体函数封装在member_generation.subspace中,library_generation进行调用
lib_name = lg.generate_library(d, t, name, 10, 3)

# 根据名字读入,这里读进来的是一个(#members * #instances)的矩阵
lib = io.read_matrix(settings.default_library_path + name + '/' + lib_name)

# 进行ensemble,这里ensemble返回的是集成之后的簇标签
ensemble_result = ce.cluster_ensembles_CSPAONLY(lib, N_clusters_max=3)

# print出来看看
print ensemble_result
print metrics.normalized_max_mutual_info_score(t, ensemble_result)
예제 #9
0
# print '--------------------'
# for doo in data_selected.as_matrix():
#     print doo

d, t = ed.dataset['waveform']['data']()
# d, t = ed.dataset['Wap']['data'](sparse_type='csr')
# d, t = ed.dataset['k1b']['data']()
# d, t = ed.dataset['hitech']['data']()
# d, t = ed.dataset['re0']['data']()
print d.shape
print np.unique(t)
km = cluster.KMeans(n_clusters=3)
t1 = time.clock()
km.fit(d)
t2 = time.clock()
print metrics.normalized_max_mutual_info_score(t, km.labels_)
# metrics
print t2 - t1
# import member_generation.subspace as sub
# subd = sub.feature_sampling(d, 2000)
# print d.shape
# print subd.shape
# data_selected, data_unselected, \
# target_selected, target_unselected = train_test_split(d, t,
#                                                       train_size=500,
#                                                       random_state=154)
# print data_selected
# print data_unselected
# print target_selected
# print target_unselected
# print d
예제 #10
0
import constrained_methods.constrained_clustering as cc
import utils.load_dataset as ld
import utils.io_func as io
import time
import evaluation.Metrics as Metrics

data, target = ld.load_mnist_4000()
print data.shape
data = data.astype(float)
ml, cl = io.read_constraints('Constraints/MNIST4000_diff_n_1.txt')
t1 = time.clock()
e2cp = cc.E2CP(data=data, ml=ml, cl=cl, n_clusters=10)
t2 = time.clock()
e2cp.fit_constrained()
print e2cp.labels
print Metrics.normalized_max_mutual_info_score(target, e2cp.labels)
print t2 - t1
예제 #11
0
def comparison_methods(dataset_name,
                       constraints_files=None,
                       additional_postfix='',
                       eval_method=None):
    """
    get the performance of comparison methods.

    Parameters
    ----------
    :param dataset_name:
    :param constraints_files:
    :param additional_postfix:
    :param eval_method:
    """
    filename = _default_eval_path + dataset_name + '_' + time.strftime(
        '%Y-%m-%d_%H_%M_%S', time.localtime(time.time())) + '.csv'
    with open(filename, 'wb') as f:
        writer = csv.writer(f)
        data, targets = exd.dataset[dataset_name]['data']()
        data = data.astype(np.double)
        k = exd.dataset[dataset_name]['k']
        km = cluster.KMeans(n_clusters=k)
        km.fit(data)
        writer.writerow([
            'KMeans',
            str(metrics.normalized_max_mutual_info_score(targets, km.labels_))
        ])
        eval_methods = _default_eval_methods if eval_method is None else eval_method
        if constraints_files is None:
            filenames = _get_default_constraints_files(
                dataset_name, _default_constraints_postfix, additional_postfix)
        else:
            filenames = _get_default_constraints_files(dataset_name,
                                                       constraints_files,
                                                       additional_postfix)
        for filename in filenames:
            ml, cl = io_func.read_constraints(_default_constraints_folder +
                                              filename + '.txt')
            for method in eval_methods:
                if method == 'Cop_KMeans':
                    result = _constrained_methods[method](data, k, ml, cl)
                    writer.writerow([
                        filename + '_Cop_KMeans',
                        str(
                            metrics.normalized_max_mutual_info_score(
                                targets, result))
                    ])
                elif method == 'E2CP':
                    e2cp = _constrained_methods[method](data=data,
                                                        ml=ml,
                                                        cl=cl,
                                                        n_clusters=k)
                    e2cp.fit_constrained()
                    result = e2cp.labels
                    writer.writerow([
                        filename + '_E2CP',
                        str(
                            metrics.normalized_max_mutual_info_score(
                                targets, result))
                    ])
    return
예제 #12
0
def do_new_weighted_ensemble_for_library(
        library_folder,
        library_name,
        class_num,
        target,
        constraint_file,
        logger,
        gammas,
        internals=None,
        cons_type='both',
        ensemble_method=_default_ensemble_method,
        scale=False):
    """

    :param library_folder:
    :param library_name:
    :param class_num:
    :param target:
    :param constraint_file:
    :param logger:
    :param alphas:
    :param cons_type:
    :param ensemble_method
    :return:
    """
    logger.debug(
        '==========================================================================================='
    )
    logger.debug('-----------------New ver Weighted Ensemble for library:' +
                 str(library_name) + '---------------')
    logger.debug('-----------------Weight type = ' + cons_type +
                 '-------------------------------------------')
    logger.debug('-----------------Scale type = ' + str(scale) +
                 '-------------------------------------------')
    logger.debug('-----------------Constraint File name = ' + constraint_file +
                 '----------------------------')

    labels = np.loadtxt(library_folder + library_name + '.res', delimiter=',')
    labels = labels.astype(int)

    # if the library is not pure, i.e, ensemble results and targets are also included.
    # then, last 5 rows should be removed (single kmeans, cspa, hgpa, mcla, real labels)
    if 'pure' not in library_name:
        labels = labels[0:-5]
    mlset, nlset = io_func.read_constraints(constraint_file)
    n_instances = labels.shape[1]
    if cons_type == 'both':
        n_constraints = len(mlset) + len(nlset)
    else:
        n_constraints = len(mlset)
    if internals is None:
        internals = _build_pesudo_internal(labels)

    # get cluster/clustering level weights
    # constraints in each cluster of all clusterings are also obtained to get g_gamma
    con_per_cluster = []
    constraints_num = []
    con_clustering = []
    cluster_time_sum = 0.0
    clustering_time_sum = 0.0
    for label in labels:
        t1 = time.clock()
        weight, cluster_cons_num = Metrics.consistency_per_cluster_efficient(
            label, mlset, nlset, cons_type=cons_type)
        con_per_cluster.append(weight)
        constraints_num.append(cluster_cons_num)
        t2 = time.clock()
        cluster_time_sum += (t2 - t1)
    for label in labels:
        t1 = time.clock()
        con_clustering.append(
            Metrics.consistency(label, mlset, nlset, cons_type=cons_type))
        t2 = time.clock()
        clustering_time_sum += (t2 - t1)

    print 'library size=' + str(labels.shape[0])
    print 'cluster avg=' + str(cluster_time_sum / labels.shape[0])
    print 'clustering avg=' + str(clustering_time_sum / labels.shape[0])

    if scale:
        scaler = preprocessing.MinMaxScaler()
        con_clustering = scaler.fit_transform(np.array(con_clustering))

    nmis = []
    for gamma in gammas:
        logger.debug(
            '-------------------------->>>>>> PARAM START <<<<<<<---------------------------------'
        )
        cur_g_gamma = get_g_gamma(constraints_num, labels, n_constraints,
                                  n_instances, gamma)
        cur_nmis = []
        for method in ensemble_method:
            ensemble_labels = _ensemble_method[method](
                labels,
                N_clusters_max=class_num,
                weighted=True,
                clustering_weights=con_clustering,
                cluster_level_weights=con_per_cluster,
                alpha=cur_g_gamma,
                new_formula=True,
                internal=internals)
            # ensemble_labels = _ensemble_method[method](labels, N_clusters_max=class_num,
            #                                            weighted=True, clustering_weights=con_clustering,
            #                                            cluster_level_weights=con_per_cluster, alpha=cur_g_gamma,
            #                                            new_formula=True, internal=internals, ml=mlset, cl=nlset)
            ensemble_nmi = Metrics.normalized_max_mutual_info_score(
                ensemble_labels, target)
            logger.debug(method + ' gamma=' + str(gamma) + ', NMI=' +
                         str(ensemble_nmi))
            cur_nmis.append(ensemble_nmi)
        nmis.append(cur_nmis)
        logger.debug(
            '------------------------->>>>>> END OF THIS PARAM <<<<<<-------------------------------'
        )
    logger.debug(
        '==========================================================================================='
    )
    return nmis
예제 #13
0
def do_7th_weighted_ensemble_for_library(
        library_folder,
        library_name,
        class_num,
        target,
        constraint_file,
        logger,
        alphas,
        internals,
        cons_type='both',
        ensemble_method=_default_ensemble_method,
        scale=False):
    """

    :param library_folder:
    :param library_name:
    :param class_num:
    :param target:
    :param constraint_file:
    :param logger:
    :param alphas:
    :param cons_type:
    :param ensemble_method
    :return:
    """
    logger.debug(
        '==========================================================================================='
    )
    logger.debug('-----------------New Weighted Ensemble for library:' +
                 str(library_name) + '-------------------')
    logger.debug('-----------------Weight type = ' + cons_type +
                 '-------------------------------------------')
    logger.debug('-----------------Scale type = ' + str(scale) +
                 '-------------------------------------------')
    logger.debug('-----------------Constraint File name = ' + constraint_file +
                 '----------------------------')

    labels = np.loadtxt(library_folder + library_name + '.res', delimiter=',')
    labels = labels.astype(int)
    k_values = []
    expected_cons = {}

    # if the library is not pure, i.e, ensemble results and targets are also included.
    # then, last 5 rows should be removed (single kmeans, cspa, hgpa, mcla, real labels)
    if 'pure' not in library_name:
        labels = labels[0:-5]
    mlset, nlset = io_func.read_constraints(constraint_file)

    # get cluster/clustering level weights
    con_per_cluster = []
    con_clustering = []
    for label in labels:
        con_per_cluster.append(
            Metrics.consistency_per_cluster(label,
                                            mlset,
                                            nlset,
                                            cons_type=cons_type))
    for label in labels:
        con_clustering.append(
            Metrics.consistency(label, mlset, nlset, cons_type=cons_type))
        k_values.append(len(np.unique(label)))
    k_values = np.array(k_values, dtype=int)
    possible_k = np.unique(k_values)
    cons = np.array(con_clustering)
    for k in possible_k:
        mean_value = np.mean(cons[k_values == k])
        if mean_value == 0:
            mean_value = 1
        expected_cons[k] = mean_value
    for i in range(0, labels.shape[0]):
        con_clustering[i] /= expected_cons[k_values[i]]
        con_clustering[i] *= internals[i]
    if scale:
        scaler = preprocessing.MinMaxScaler()
        con_clustering = scaler.fit_transform(np.array(con_clustering))

    nmis = []
    for alpha in alphas:
        logger.debug(
            '-------------------------->>>>>> PARAM START <<<<<<<---------------------------------'
        )
        cur_nmis = []
        for method in ensemble_method:
            ensemble_labels = _ensemble_method[method](
                labels,
                N_clusters_max=class_num,
                weighted=True,
                clustering_weights=con_clustering,
                cluster_level_weights=con_per_cluster,
                alpha=alpha)
            ensemble_nmi = Metrics.normalized_max_mutual_info_score(
                ensemble_labels, target)
            logger.debug(method + ' alpha=' + str(alpha) + ', NMI=' +
                         str(ensemble_nmi))
            cur_nmis.append(ensemble_nmi)
        nmis.append(cur_nmis)
        logger.debug(
            '------------------------->>>>>> END OF THIS PARAM <<<<<<-------------------------------'
        )
    logger.debug(
        '==========================================================================================='
    )
    return nmis
예제 #14
0
def generate_library(data, target, dataset_name, n_members, class_num,
                     n_cluster_lower_bound=0, n_cluster_upper_bound=0,
                     feature_sampling=1.0, sample_sampling=0.7,
                     feature_sampling_lower_bound=0.05, sample_sampling_lower_bound=0.1,
                     f_stable_sample=True, s_stable_sample=True,
                     constraints_file=None, sampling_method='FSRSNC', verbose=True, path=_default_result_path,
                     metric='nid', manifold_type='MDS', subfolder=True,
                     generate_only=True):
    """
    generate a single library of ensemble member.

    Parameters
    ----------
    :param data: dataset in a ndarray
    :param target: target in a ndarray or list
    :param dataset_name: name of dataset
    :param n_members: #clusters
    :param class_num: #real_class
    :param n_cluster_lower_bound: lower bound of k
    :param n_cluster_upper_bound: upper bound of k
    :param feature_sampling: fixed sampling rate of feature, or upper bound if not stable
    :param sample_sampling:  fixed sampling rate of instances, or upper bound if not stable
    :param feature_sampling_lower_bound: lower bound of sampling rate of feature, only available if not stable
    :param sample_sampling_lower_bound: lower bound of sampling rate of instance, only available if not stable
    :param f_stable_sample: stable feature sampling or not
    :param s_stable_sample: stable instance sampling or not
    :param constraints_file: name of constraint file, only available when
    :param sampling_method: 'FSRSNC' and 'FSRSNN' supported
    :param verbose: print debug info.
    :param path: path to store the library
    :param metric: used for visualization only
    :param manifold_type: used for visualization only
    :param subfolder: save library in a separated sub-folder or not.

    Return
    ------
    :return: name of the library generated (the library itself will be stored as a file)
    """
    print('start generating library for dataset:' + dataset_name)

    # make sure that path to store the library existing
    if not os.path.isdir(path):
        os.mkdir(path)
    if subfolder:
        savepath = path + dataset_name + '/'
        if not os.path.isdir(savepath):
            os.mkdir(savepath)
    else:
        savepath = path

    # we set the range of cluster number to [k, 10k] if not defined
    if n_cluster_lower_bound == 0 or n_cluster_upper_bound == 0:
        n_cluster_lower_bound = class_num
        n_cluster_upper_bound = class_num * 10

    # get sampling method, if not exist, it will raise a exception
    if sampling_method in _sampling_methods.keys():
        is_constrained = False
    elif sampling_method in _constrained_methods.keys():
        is_constrained = True
    else:
        raise ValueError('ensemble generation : Method should be set properly.')

    # read constraints file if existing
    if constraints_file is not None:
        mlset, nlset = io_func.read_constraints(constraints_file)
    else:
        if is_constrained:
            raise Exception('ensemble generation : Constrained Member must be with a constraints file.')
        constraints_file = ''
        mlset = []
        nlset = []

    # lower bound of sampling rate (use only if 'stable' set to be false)
    if feature_sampling_lower_bound > feature_sampling:
        feature_sampling_lower_bound = feature_sampling / 2
    if sample_sampling_lower_bound > sample_sampling:
        sample_sampling_lower_bound = sample_sampling / 2

    # there should be at least 2 clusters in the clustering
    if n_cluster_lower_bound < 2:
        n_cluster_lower_bound = 2
    if n_cluster_upper_bound < n_cluster_lower_bound:
        n_cluster_upper_bound = n_cluster_lower_bound

    # path and filename to write the file
    filename = _get_file_name(dataset_name, n_cluster_lower_bound, n_cluster_upper_bound, feature_sampling,
                              feature_sampling_lower_bound, sample_sampling, sample_sampling_lower_bound, n_members,
                              f_stable_sample, s_stable_sample, sampling_method, is_constraint_method=is_constrained,
                              constraint_file=constraints_file)

    # we won't generate the library with same sampling rate and size if existing
    if os.path.isfile(savepath + filename + '.res'):
        print ('[Library Generation] : library already exists.')
        return filename+'.res'
    elif os.path.isfile(savepath + filename + '_pure.res'):
        print ('[Library Generation] : corresponding pure library already exists.')
        return filename+'_pure.res'

    tag = True

    # matrix to store clustering results
    mat = np.empty(data.shape[0])

    # generate ensemble members
    for i in range(0, n_members):
        # determine k randomly
        cluster_num = np.random.randint(n_cluster_lower_bound, n_cluster_upper_bound + 1)
        random_state = np.random.randint(0, _INT_MAX - 1)

        cur_feature_sampling = feature_sampling
        cur_sample_sampling = sample_sampling
        if not f_stable_sample:
            cur_feature_sampling = rand.uniform(feature_sampling_lower_bound, feature_sampling)
        if not s_stable_sample:
            cur_sample_sampling = rand.uniform(sample_sampling_lower_bound, sample_sampling)

        print('For this base clustering, cluster number is ' + str(cluster_num))
        # generate ensemble member by given method
        if sampling_method == 'Cop_KMeans':
            result = _constrained_methods[sampling_method](data, cluster_num, mlset, nlset)
        elif sampling_method == 'E2CP':
            e2cp = _constrained_methods[sampling_method](data=data, ml=mlset, cl=nlset, n_clusters=cluster_num)
            e2cp.fit_constrained()
            result = e2cp.labels
        else:
            result = _sampling_methods[sampling_method](data, target, r_clusters=cluster_num,
                                                        r_state=random_state, fsr=cur_feature_sampling,
                                                        ssr=cur_sample_sampling)
        # print diversity
        diver = Metrics.normalized_max_mutual_info_score(result, target)
        if verbose:
            print ('Base clustering' + str(i) + ' nmi_max between real labels = ' + str(diver))
        # stack the result into the matrix
        if tag:
            mat = np.array(result)
            mat = np.reshape(mat, (1, data.shape[0]))
            tag = False
        else:
            temp = np.array(result)
            temp = np.reshape(temp, (1, data.shape[0]))
            mat = np.vstack([mat, np.array(temp)])

    # change element type to int for consensus
    mat = mat.astype(int)

    if generate_only or is_constrained:
        np.savetxt(savepath + filename + '_pure' + '.res', mat, fmt='%d', delimiter=',')
        return filename+'_pure.res'

    # single k-means model, for comparison
    clf = cluster.KMeans(n_clusters=class_num)
    clf.fit(data)
    kmlabels = clf.labels_

    # do consensus
    labels_CSPA = ce.cluster_ensembles_CSPAONLY(mat, N_clusters_max=class_num)
    labels_HGPA = ce.cluster_ensembles_HGPAONLY(mat, N_clusters_max=class_num)
    labels_MCLA = ce.cluster_ensembles_MCLAONLY(mat, N_clusters_max=class_num)

    # put consensus results into the matrix
    mat = np.vstack([mat, np.reshape(kmlabels, (1, data.shape[0]))])
    mat = np.vstack([mat, np.reshape(labels_CSPA, (1, data.shape[0]))])
    mat = np.vstack([mat, np.reshape(labels_HGPA, (1, data.shape[0]))])
    mat = np.vstack([mat, np.reshape(labels_MCLA, (1, data.shape[0]))])

    # put real labels into the matrix
    temp = np.reshape(target, (1, data.shape[0]))
    mat = np.vstack([mat, np.array(temp)])

    print ('Dataset ' + dataset_name + ', consensus finished, saving...')

    # write results to external file, use %d to keep integer part only
    np.savetxt(savepath + filename + '.res', mat, fmt='%d', delimiter=',')

    # print labels and diversities (between the real labels)
    nmi_CSPA = Metrics.normalized_max_mutual_info_score(labels_CSPA, target)
    nmi_HGPA = Metrics.normalized_max_mutual_info_score(labels_HGPA, target)
    nmi_MCLA = Metrics.normalized_max_mutual_info_score(labels_MCLA, target)
    print ('consensus NMI (CSPA) =' + str(nmi_CSPA))
    print ('consensus NMI (HGPA) =' + str(nmi_HGPA))
    print ('consensus NMI (MCLA) =' + str(nmi_MCLA))

    kmnmi = Metrics.normalized_max_mutual_info_score(kmlabels, target)
    print ('single-model diversity (K-means) =' + str(kmnmi))
    # save performances
    perf = np.array([nmi_CSPA, nmi_HGPA, nmi_MCLA, kmnmi])
    np.savetxt(savepath + filename + '_performance.txt', perf, fmt='%.6f', delimiter=',')

    if metric == 'diversity':
        distance_matrix = Metrics.diversityMatrix(mat)
        np.savetxt(savepath + filename + '_diversity.txt', distance_matrix, delimiter=',')
    else:
        distance_matrix = Metrics.NIDMatrix(mat)
        np.savetxt(savepath + filename + '_nid.txt', distance_matrix, delimiter=',')

    if manifold_type == 'MDS':
        # transform distance matrix into 2-d or 3-d coordinates to visualize
        mds2d = manifold.MDS(n_components=2, max_iter=10000, eps=1e-12, dissimilarity='precomputed')
        mds3d = manifold.MDS(n_components=3, max_iter=10000, eps=1e-12, dissimilarity='precomputed')
        pos2d = mds2d.fit(distance_matrix).embedding_
        pos3d = mds3d.fit(distance_matrix).embedding_
        np.savetxt(savepath + filename + '_mds2d.txt', pos2d, fmt="%.6f", delimiter=',')
        np.savetxt(savepath + filename + '_mds3d.txt', pos3d, fmt="%.6f", delimiter=',')

        # draw odm, k distribution and nmi distribution
        cv.plot_ordered_distance_matrix(distance_matrix, savepath + filename + '_original_distance.png',
                                        savepath + filename + '_odm.png')
        cv.plot_k_distribution(mat, pos2d, savepath + filename+'_k_distribution.png')
        cv.plot_nmi_max(mat, pos2d, savepath + filename + '_nmimax_distribution.png')

        # consistencies are calculated while constraints file exists.
        if constraints_file != '':
            cv.plot_consistency(mat, pos2d, mlset, nlset, savepath + filename+'_consistency_both.png',
                                consistency_type='both')
            cv.plot_consistency(mat, pos2d, mlset, nlset, savepath + filename+'_consistency_must.png',
                                consistency_type='must')
            cv.plot_consistency(mat, pos2d, mlset, nlset, savepath + filename+'_consistency_cannot.png',
                                consistency_type='cannot')
            cv.plt_consistency_corelation_with_k(mat, mlset, nlset, savepath + filename+'_normalized.png')
    return