Esempio n. 1
0
def Test_weightened_knn():
    k_fold = 10
    trainingSet=[]
    testSet = []
    predictions = []
    dataset = misc.loadData('pc1.arff')

    trainingSet = np.asarray(trainingSet)
    testSet = np.asarray(testSet)
    predictions = []

    trainingSet,testSet = partition(dataset,k_fold)
    accuracy = []

    for k in [1,2,3,5,7,9,11,13,15]:
        print "Weightened KNN with K = "  + repr(k)
        print "K-fold = " + repr(k_fold)
        for x in range(k_fold):
            predictions = []
            for i in range(len(testSet[x])):      
                result = calculateNeighborsWeightened(trainingSet[x], testSet[x][i], k)
                predictions.append(result)
            accuracy.append(misc.calculateAccuracy(testSet[x],predictions))
            print 'iteration: '+ repr(x) + " accuracy:" + repr(accuracy[x]) + '%'
        accuracy_avg = sum(accuracy)/k_fold
        print ("Average accuracy : " + repr(accuracy_avg) + '%')
        print 
        accuracy = []
        accuracy_avg = 0
    print 
Esempio n. 2
0
def Test_vdm_weightened_knn():
    k_fold = 10
    trainingSet=[]
    testSet = []
    predictions = []
    dataset = misc.loadData('chess.csv')
    
    trainingSet = np.asarray(trainingSet)
    testSet = np.asarray(testSet)
    predictions = []

    trainingSet,testSet = partition(dataset,k_fold)
    accuracy = []

    for k in [1,2,3,5,7,9,11,13,15]:
        print "Weightened KNN-VDM with K = "  + repr(k)
        print "K-fold = " + repr(k_fold)
        for x in range(k_fold):
            predictions = []
            lookup_ = misc.buildLookUp(trainingSet[x])
            classes = misc.getClasses(trainingSet[x])
            for i in range(len(testSet[x])):      
                result = calculateNeighborsWeightenedVDM(trainingSet[x],testSet[x][i],k,lookup_,classes)
                predictions.append(result)
            accuracy.append(misc.calculateAccuracy(testSet[x],predictions))
            print 'iteration: '+ repr(x) + " accuracy:" + repr(accuracy[x]) + '%'
        accuracy_avg = sum(accuracy)/k_fold
        print ("Average accuracy : " + repr(accuracy_avg) + '%')
        print 
        accuracy = []
        accuracy_avg = 0
    print 
    def train_one_batch(crnn, criterion, optimizer, train_iter):
        data = train_iter.next()
        cpu_images, cpu_texts = data
        batch_size = cpu_images.size(0)
        misc.loadData(image, cpu_images)
        t, l = converter.encode(cpu_texts)
        misc.loadData(text, t)
        misc.loadData(length, l)

        preds = crnn(image)
        preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
        cost = criterion(preds, text, preds_size, length) / batch_size
        crnn.zero_grad()
        cost.backward()
        optimizer.step()
        return cost
Esempio n. 4
0
def Test_hvdm_unweightened_knn():
    k_fold = 10
    trainingSet=[]
    testSet = []
    predictions = []
    dataset = misc.loadData('crx.data')
    
    for i in range(len(dataset)):
        if dataset[i][13] != '?':
            dataset[i][13] = float(dataset[i][13])

    trainingSet = np.asarray(trainingSet)
    testSet = np.asarray(testSet)
    predictions = []

    trainingSet,testSet = partition(dataset,k_fold)
    accuracy = []

    for k in [1,2,3,5,7,9,11,13,15]:
        print "UnWeightened KNN-HVDM with K = "  + repr(k)
        print "K-fold = " + repr(k_fold)

        for x in range(k_fold):
            predictions = []
            maxmin = misc.maxMin(trainingSet[x])
            lookup_ = misc.buildLookUp(trainingSet[x])
            classes = misc.getClasses(trainingSet[x])
            for i in range(len(testSet[x])):      
                result = calculateNeighborsUnWeightenedHVDM(trainingSet[x],testSet[x][i],k,lookup_,classes,maxmin)
                predictions.append(result)
            accuracy.append(misc.calculateAccuracy(testSet[x],predictions))
            print 'iteration: '+ repr(x) + " accuracy:" + repr(accuracy[x]) + '%'
        accuracy_avg = sum(accuracy)/k_fold
        print ("Average accuracy : " + repr(accuracy_avg) + '%')
        print 
        accuracy = []
        accuracy_avg = 0
    print  
def validate_one_epoch(crnn,
                       val_loader,
                       criterion,
                       converter,
                       opt,
                       logger=None,
                       max_iter=100):
    if logger:
        logger.info('Start val')
    else:
        print('Start val')

    for p in crnn.parameters():
        p.requires_grad = False

    crnn.eval()

    image = torch.FloatTensor(opt.batchSize, 3, opt.imgH, opt.imgH)
    text = torch.IntTensor(opt.batchSize * 5)
    length = torch.IntTensor(opt.batchSize)

    if opt.cuda:
        image = image.cuda()

    image, text, length = Variable(image), Variable(text), Variable(length)

    val_iter = iter(val_loader)

    i = 0
    n_correct = 0
    loss_avg = misc.averager()

    max_iter = min(max_iter, len(val_loader))
    for i in range(max_iter):
        data = val_iter.next()
        i += 1
        cpu_images, cpu_texts = data
        batch_size = cpu_images.size(0)
        misc.loadData(image, cpu_images)
        t, l = converter.encode(cpu_texts)
        misc.loadData(text, t)
        misc.loadData(length, l)

        preds = crnn(image)
        preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
        cost = criterion(preds, text, preds_size, length) / batch_size
        loss_avg.add(cost)

        _, preds = preds.max(2)
        preds = preds.transpose(1, 0).contiguous().view(-1)
        sim_preds = converter.decode(preds.data, preds_size.data, raw=False)
        for pred, target in zip(sim_preds, cpu_texts):
            if pred == target.lower():
                n_correct += 1

    raw_preds = converter.decode(preds.data, preds_size.data,
                                 raw=True)[:opt.n_test_disp]
    for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts):
        if logger:
            logger.info('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt))
        else:
            print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt))

    accuracy = n_correct / float(max_iter * opt.batchSize)
    if logger:
        logger.info('Test loss: %f, accuray: %f' % (loss_avg.val(), accuracy))
    else:
        print('Test loss: %f, accuray: %f' % (loss_avg.val(), accuracy))
Esempio n. 6
0
#Bibliotecas utilizadas
import misc
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

#modulos implementados
from LVQ import lvq1
from LVQ import lvq2_1
from LVQ import lvq3

#-------------Utilizando um dataset artificial com apenas dois features para ilustrar o funcionamento do LVQ.-------------------
dataset = misc.loadData('long1.arff')
dataset = np.asarray(dataset)

plt.scatter(dataset[:, 0], dataset[:, 1], c=dataset[:, 2])
plt.title("2d Dataset", fontsize='large')
plt.show()

#---------------------------------------Escolha de um prototipo aleatorio de cada classe----------------------------------------
prototypes = misc.getRandomPrototypes(dataset, 1)

plt.scatter(dataset[:, 0], dataset[:, 1], c=dataset[:, 2])
plt.title("Prototipos: azul e verde", fontsize='large')
plt.scatter(prototypes[:, 0], prototypes[:, 1], c=['g', 'r'])
plt.show()

#-----------------------------Ajuste dos prototipos utilizado o lvq1 com 0.4 de taxa de aprendizado-----------------------------
prototypes = lvq1(prototypes, dataset, 0.4)

plt.title("Prototipos: azul e verde(ajustados)", fontsize='large')
Esempio n. 7
0
# 
# - The mean is calculated and the entire data set normalized.
# - The covariance matrix is calculated.
# - The eigenvectors and eigenvalues of the covariance matrix are then calculated.
# - The K eigenvectors with the greatest amount of associated information are chosen.
# - We assemble the projection matrix P based on previously selected eigenvectors.
# - The normalized image obtained in step 1 is projected by the projection matrix.
# 
# 
# 

# In[2]:


#---Using an artificial dataset with only two dimensions to illustrate how the PCA works.
dataset = misc.loadData('sample.csv')
dataset = np.asarray(dataset)

plt.scatter(dataset[:,0],dataset[:,1])
plt.title("2D Sample - Dataset", fontsize='large')
plt.show()


# The number of eigenvectors generated is equal to the number of dimensions of the dataset, so in the present example, two eigenvectors are gerenerated, each with the same dimension as the dataset(two dimensions).  

# In[12]:


#---The two eigenvectors are:
eigenValues, eigenVectors = PCA.pca_eigen(dataset)
np.set_printoptions(precision=3)