Esempio n. 1
0
def run(filename, gamma, m, k, ktype, outfig, critoutfig, testfile):

    digitsdat = Data()
    digitsdat.load_svmlight(filename,
                            gamma=gamma,
                            docalkernel=False,
                            savefile=None,
                            testfile=testfile,
                            dobin=False)

    if ktype == 0:
        digitsdat.calculate_kernel()
        print "Running Kernel type : global "
    else:
        digitsdat.calculate_kernel_individual()
        print "Running Kernel type : local "

    # selected = greedy_parallel(digitsdat.kernel, m)
    # print np.sort(selected)
    selected = greedy_select_protos(
        digitsdat.kernel, np.array(range(np.shape(digitsdat.kernel)[0])), m)
    # print np.sort(selected)
    # critselected = select_criticism(digitsdat.kernel, selected, k)
    selectedy = digitsdat.y[selected]
    sortedindx = np.argsort(selectedy)
    critselected = None

    if outfig is not None:
        plotfigs2(digitsdat.X[selected[sortedindx], :], selectedy[sortedindx],
                  outfig)

    if k > 0:
        critselected = select_criticism_regularized(digitsdat.kernel,
                                                    selected,
                                                    k,
                                                    is_K_sparse=False,
                                                    reg='logdet')

        critselectedy = digitsdat.y[critselected]
        critsortedindx = np.argsort(critselectedy)

        if critoutfig is not None:
            plotfigs2(digitsdat.X[critselected[critsortedindx], :],
                      critselectedy[critsortedindx], critoutfig + reg)

    return selected, critselected, digitsdat
Esempio n. 2
0
    data_record = data_record.reset_index()
    feature_columns = [c for c in data_record if c.startswith('machine_')]
    X = data_record[feature_columns].as_matrix()
    y = data_record['sleep_stage'].as_matrix()
    data = Data()
    data.load_data(X, y, gamma=0.026, docalkernel=False, savefile=None, testfile=None, dobin=False)
    # Calculate global kernel
    data.calculate_kernel()
    # Calculate local kernel
    #data.calculate_kernel_individual()

    num_examples_to_select_per_class = 10
    num_examples_to_select = 40

    num_prototypes_to_select = num_examples_to_select
    prototypes_selected = greedy_select_protos(data.kernel, np.array(range(np.shape(data.kernel)[0])), num_prototypes_to_select)
    prototypes_selected_y = data.y[prototypes_selected]
    prototypes_selected_wake = prototypes_selected[prototypes_selected_y == 0][:num_examples_to_select_per_class]
    prototypes_selected_N1 = prototypes_selected[prototypes_selected_y == 1][:num_examples_to_select_per_class]

    num_criticisms_to_select = num_examples_to_select
    criticisms_selected = select_criticism_regularized(data.kernel, prototypes_selected, num_criticisms_to_select, is_K_sparse=False, reg='logdet')
    criticisms_selected_y = data.y[criticisms_selected]
    criticisms_selected_wake = criticisms_selected[criticisms_selected_y == 0][:num_examples_to_select_per_class]
    criticisms_selected_N1 = criticisms_selected[criticisms_selected_y == 1][:num_examples_to_select_per_class]

    examples_selected[data_record_metadata['file_name']] = {
        'prototypes': {
            'Wake': data_record['epoch_start_in_seconds'][prototypes_selected_wake].tolist(),
            'N1': data_record['epoch_start_in_seconds'][prototypes_selected_N1].tolist(),
        },
Esempio n. 3
0
def prototype_selection(X, subsample=20, kernel='rbf'):
    return greedy_select_protos(
        pairwise_kernels(X, metric=kernel), np.array(range(X.shape[0])),
        subsample) if subsample > 1 else np.array(range(X.shape[0]))