Esempio n. 1
0
def gda_model_accuracy_nmf(Xraw, L, num_features, percent_train=0.5):

    # reduce dimensions of raw data using NMF
    nmf_obj = skd.NMF(n_components=num_features, random_state=17)
    X = nmf_obj.fit_transform(Xraw.T).T
    Q = nmf_obj.components_.T

    # get shape information for labels
    (nl, ) = L.shape

    # break data into training and testing datasets
    (Dtr, Dtt) = mydata.separateClassData(
        X, L.reshape(1, nl), numdata_or_percent_for_training=percent_train)

    # get the start time the training starts
    start = time.time()

    # build the set of LDA models
    dlist = mydsc.constructDistriminantSet(X=Dtr['net'], L=Dtr['nlbl'])

    # get the end time after training
    end = time.time()
    print('({0}) Time elapsed to train is: '.format(num_features), end - start)

    # test the discriminants for each label dataset in the training set
    Ltest = mydsc.evalDiscriminantSet(X=Dtt['net'], discriminant_list=dlist)

    # Compute accuracy and confusion matrix
    classes = np.array([0, 1])
    (C, accuracy) = mydsc.computeConfusionMatrix(Xtest=Dtt['net'],
                                                 Ltest=Dtt['nlbl'],
                                                 Leval=Ltest,
                                                 classes=classes)

    # plot the confusion matrix
    f0 = plot.figure()
    (f0, ax0) = myimg.plotDataset(f0, C, delta=(-1, -1))
    ax0.set_xlabel('Classified Classes')
    ax0.set_ylabel('True Classes')
    f0.savefig('{0}/confusion_mat_{1}.png'.format('../plots/GDA',
                                                  num_features))

    # print output message
    print('Accuracy for {0} features is : '.format(num_features), accuracy)

    # return the accuracy
    return accuracy / 100.0
# evaluate the discriminant functions
Labels = mydisc.evalDiscriminantSet(Test[vars], set)

# find time the training finished
end = timer()
print('Testing took {0} seconds'.format(end - start))

# compute the confusion matrix along with accuracy
(C, accuracy) = mydisc.computeConfusionMatrix(Test[vars], Test[lbls], Labels,
                                              classes)
print('The overall accuracy is {0}%'.format(accuracy))

# plot the confusion matrix
plotdir = 'plots/prob1_face'
f0 = plot.figure()
(f0, ax0) = myimg.plotDataset(f0, C, delta=(-1, -1))
ax0.set_xlabel('Classified Classes')
ax0.set_ylabel('True Classes')
f0.savefig('{0}/confusion_mat.png'.format(plotdir))

# Find the largest confusion values in the matrix and get their tuple pair
cbest = mydisc.computeMostConfused(C, num_values=4)

# For each pair of confused values, generate and save each digit's likelihood and the
# pair's log odds ratio
for (i1, i2, cval) in cbest:
    fig = plot.figure()
    l1 = set[i1].likelihood(class_value=1).reshape(35, 30)
    l2 = set[i2].likelihood(class_value=1).reshape(35, 30)
    odds = l1 - l2
Esempio n. 3
0
    # compute the confusion matrix along with accuracy
    (C,accuracy) = mydisc.computeConfusionMatrix(Test[vars],Test[lbls],Labels,classes)
    print('({0},{1})| The overall accuracy is {2}%'.format(dim[0], dim[1], accuracy))

    # generate appropriate paths
    plotdir0    = 'plots/prob12_disjoint'
    plotdir     = 'plots/prob12_disjoint/{0}_{1}'.format(dim[0],dim[1])
    try:
        os.makedirs(plotdir0)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

    try:
        os.makedirs(plotdir)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

    # plot the confusion matrix
    f0 = plot.figure()
    (f0,ax0) = myimg.plotDataset(f0,C,delta=(-1,-1))
    ax0.set_xlabel('Classified Classes')
    ax0.set_ylabel('True Classes')
    f0.savefig('{0}/confusion_mat.png'.format(plotdir))




Esempio n. 4
0
# index for independent var data and labels
vars = 0
lbls = 1

# define classes
classes = np.array([1, 2, 3, 4, 5])

# load the dataset in a proper form
(Test,Train) = myload.loadProblem2Part2('data/prob2/part2')

# create the discriminant functions
possible_values = np.array([0,1])
set = mydisc.constructDistriminantSet(X=Train[vars],
                                      L=Train[lbls],
                                      possible_values=possible_values,
                                      laplace_smoothing_val=1e-1)

# evaluate the discriminant functions
Labels = mydisc.evalDiscriminantSet(Test[vars],set,classes=classes)

# compute the confusion matrix along with accuracy
(C,accuracy) = mydisc.computeConfusionMatrix(Test[vars],Test[lbls],Labels,classes)
print('The overall accuracy is {0}%'.format(accuracy))

# plot the confusion matrix
plotdir = 'plots/prob22'
f0 = plot.figure()
(f0,ax0) = myimg.plotDataset(f0,C,delta=(0,0))
ax0.set_xlabel('Classified Classes')
ax0.set_ylabel('True Classes')
f0.savefig('{0}/confusion_mat.png'.format(plotdir))