Exemple #1
0
def two_moons_hfs_hard(path,
                       dataset='/data/data_2moons_hfs',
                       l=4,
                       var=1,
                       eps=0,
                       k=10,
                       laplacian_normalization=""):
    # a skeleton function to perform HFS, needs to be completed

    # load the data
    in_data = scipy.io.loadmat(path + dataset)
    X = in_data['X']
    Y = in_data['Y'][:, 0]

    # Draw at random index of labels
    Y_masked = mask_labels(Y, l)

    labels = hard_hfs(X,
                      Y_masked,
                      var=var,
                      eps=eps,
                      k=k,
                      laplacian_normalization=laplacian_normalization)

    helper.plot_classification(X, Y, labels, var=var, eps=eps, k=k)
    accuracy = np.mean(labels == np.squeeze(Y))

    return accuracy
Exemple #2
0
def two_moons_hfs_soft(path,
                       cl,
                       cu,
                       l=4,
                       var=1,
                       eps=0,
                       k=10,
                       laplacian_normalization=""):
    # a skeleton function to perform HFS, needs to be completed
    # load the data
    in_data = scipy.io.loadmat(path + '/data/data_2moons_hfs')
    X = in_data['X']
    Y = in_data['Y'][:, 0]

    # automatically infer number of labels from samples
    num_samples = X.shape[0]
    num_classes = len(np.unique(Y))

    # Draw at random index of labels
    Y_masked = mask_labels(Y, l)

    labels = soft_hfs(X,
                      Y_masked,
                      cl,
                      cu,
                      var=var,
                      eps=eps,
                      k=k,
                      laplacian_normalization=laplacian_normalization)

    helper.plot_classification(X, Y, labels, var=var, eps=eps, k=k)
    accuracy = np.mean(labels == np.squeeze(Y))

    return accuracy
Exemple #3
0
def two_moons_hfs():
    """
    TO BE COMPLETED.

    HFS for two_moons data.
    """

    """
    Load the data. At home, try to use the larger dataset (question 1.2).    
    """
    # load the data
    in_data = loadmat(os.path.join('data', 'data_2moons_hfs.mat'))
#    in_data = loadmat(os.path.join('data', 'data_2moons_hfs_large.mat'))
    X = in_data['X']
    Y = in_data['Y'].squeeze()

    # automatically infer number of labels from samples
    num_samples = np.size(Y, 0)
    num_classes = len(np.unique(Y))

    """
    Choose the experiment parameters
    """
    var = 1
    eps = 0.820
    k = 6
    laplacian_regularization = 0.8
    laplacian_normalization = ''
    c_l = 50
    c_u = 50

    # number of labeled (unmasked) nodes provided to the hfs algorithm
    l = 4

    # mask labels
    Y_masked = mask_labels(Y, l)

    """
    compute hfs solution using either soft_hfs or hard_hfs
    """
    labels = hard_hfs(X, Y_masked, laplacian_regularization, var, eps, k, laplacian_normalization)
#    labels = soft_hfs(X, Y_masked, c_l , c_u, laplacian_regularization, var, eps, k, laplacian_normalization)

    """
    Visualize results
    """
    plot_classification(X, Y, labels,  var=var, eps=0, k=k)
    accuracy = np.mean(labels == np.squeeze(Y))
    
    print('The corresponding accuracy is : {} %'.format(accuracy * 100))
    return accuracy
Exemple #4
0
def two_moons_hfs():
    """
    TO BE COMPLETED.

    HFS for two_moons data.
    """
    """
    Load the data. At home, try to use the larger dataset (question 1.2).    
    """
    # load the data
    in_data = loadmat(os.path.join('data', 'data_2moons_hfs.mat'))
    #in_data = loadmat(os.path.join('data', 'data_2moons_hfs_large.mat'))
    X = in_data['X']
    Y = in_data['Y'].squeeze()
    """
    Choose the experiment parameters
    """
    var = 1.0
    eps = 0
    k = 6
    laplacian_regularization = 0.000000001
    laplacian_normalization = ''
    c_l = 0.96
    c_u = 0.04

    # number of labeled (unmasked) nodes provided to the hfs algorithm
    l = 4

    # mask labels
    Y_masked = mask_labels(Y, l)

    if np.size(np.where(Y_masked == 1)) == 4 or np.size(
            np.where(Y_masked == 2)) == 4:
        print(
            "WARNING : the randomness was such that the four revealed labels are in the same class"
        )
    """
    compute hfs solution using either soft_hfs or hard_hfs
    """
    #labels = hard_hfs(X, Y_masked, laplacian_regularization = laplacian_regularization, var = var, eps=eps, k=k, laplacian_normalization = laplacian_normalization)
    labels = soft_hfs(X, Y_masked, c_l, c_u, laplacian_regularization, var,
                      eps, k, laplacian_normalization)
    """
    Visualize results
    """
    plot_classification(X, Y, labels, var=var, eps=0, k=k)
    accuracy = np.mean(labels == np.squeeze(Y))

    return accuracy
Exemple #5
0
def two_moons_hfs(small_dataset=True, method='hard', plot=True):
    """
    TO BE COMPLETED.

    HFS for two_moons data.
    """
    """
    Load the data. At home, try to use the larger dataset (question 1.2).    
    """
    # load the data
    if small_dataset:  # questions 1.1 and 1.3
        in_data = loadmat(os.path.join('data', 'data_2moons_hfs.mat'))
    else:  # question 1.2
        in_data = loadmat(os.path.join('data', 'data_2moons_hfs_large.mat'))

    X = in_data['X']
    Y = in_data['Y'].squeeze()

    # automatically infer number of labels from samples
    num_samples = np.size(Y, 0)
    num_classes = len(np.unique(Y))
    """
    Choose the experiment parameters
    """
    var = 1
    eps = 0
    if small_dataset:  # questions 1.1 and 1.3 parameter
        k = 12
    else:  # question 1.2 parameter
        k = 40
    laplacian_regularization = 0
    laplacian_normalization = "rw"
    c_l = .9
    c_u = .1

    # number of labeled (unmasked) nodes provided to the hfs algorithm
    l = 4

    # mask labels
    Y_masked = mask_labels(Y, l)
    """
    compute hfs solution using either soft_hfs or hard_hfs
    """
    if method == 'hard':
        labels = hard_hfs(X, Y_masked, laplacian_regularization, var, eps, k,
                          laplacian_normalization)
    else:  # if method == 'soft'
        labels = soft_hfs(X, Y_masked, c_l, c_u, laplacian_regularization, var,
                          eps, k, laplacian_normalization)
    """
    Visualize results
    """
    if plot:
        if method == 'hard':
            method_name = 'Hard HFS'
        else:
            method_name = 'Soft HFS'

        plot_classification(X,
                            Y,
                            labels,
                            var=var,
                            eps=0,
                            k=k,
                            method=method_name)

    accuracy = np.mean(labels == np.squeeze(Y))
    return accuracy