Ejemplo n.º 1
0
def main(testfile=None):
	if testfile:
		a=rd.read(testfile)
		k=3
		predictions=[]
		for x in range(len(a)):
			neighbors = knn.getNeighbors(train, a[x], k)
			result = knn.getResponse(neighbors)
			predictions.append(result)
		return(predictions)
	else:
		knn.main(train,tests)
Ejemplo n.º 2
0
def main():

    predictions = []
    k = 3
    for x in range(len(test)):
        neighbours = getNeighbour(train, test[x], k)
        result = getResponse(neighbours)
        predictions.append(result)
        print('> predicted=' + repr(result) + ', actual=' +
              repr(testSet[x][-1]))
        accuracy = getAccuracy(test, predictions)
        print('Accuracy: ' + repr(accuracy) + '%')
Ejemplo n.º 3
0
def main(i=0, j=62, testfile=None):
    tr, ts = rt.read2(i, j)
    k = 3
    if testfile:
        tst = rd.read2(testfile)
        predictions = []
        for x in range(len(tst)):
            neighbors = knn.getNeighbors(tr, tst[x], k)
            result = knn.getResponse(neighbors)
            predictions.append(result)
        return (predictions)
    else:
        knn.main(tr, ts)
Ejemplo n.º 4
0
def getClassificationPrecision(trainingSet,testSet,k=3):
    predictions=[]
    preds=[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
    dictClasses={'carettacaretta': 0, 'cheloniamydas': 1,'dermochelyscoriacea':2,'eretmochelysimbricata':3,'lepidochelysolivacea':4}
    
    for x in range(len(testSet)):
        neighbors = knn.getNeighbors(trainingSet, testSet[x], k)
        result = knn.getResponse(neighbors)
        #print result
        predictions.append(result)
        indiceClassePred=dictClasses[result]
        indiceClasseActual=dictClasses[testSet[x][-1]]
        preds[indiceClasseActual][indiceClassePred]=preds[indiceClasseActual][indiceClassePred]+1
    
    #print('> predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1]))
    #print predictions
    precisao = knn.getPrecision(testSet, predictions)
    #print('Accuracy: ' + repr(accuracy) + '%')
    #quit()
    return precisao
Ejemplo n.º 5
0
print 'Train set: ' + repr(len(trainingSet))
print 'Test set: ' + repr(len(testSet))
# generate predictions
predictions=[]
k = 3 # valor de k igual a 3

preds=[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
dictClasses={'carettacaretta': 0, 'cheloniamydas': 1,'dermochelyscoriacea':2,'eretmochelysimbricata':3,'lepidochelysolivacea':4}

numeroImagensPorClasse=30.00;



for x in range(len(testSet)):
	neighbors = knn.getNeighbors(trainingSet, testSet[x], k)
	result = knn.getResponse(neighbors)
	predictions.append(result)
	indiceClassePred=dictClasses[result]
	indiceClasseActual=dictClasses[testSet[x][-1]]
	preds[indiceClasseActual][indiceClassePred]=preds[indiceClasseActual][indiceClassePred]+1	
	print('> predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1]))
accuracy = knn.getAccuracy(testSet, predictions)
print('Accuracy: ' + repr(accuracy) + '%')







Ejemplo n.º 6
0
            feature_space,
            (int(all_feature[i][0] * width), int(all_feature[i][1] * height)),
            2, (0, 255, 255), -1)

cv2.circle(feature_space,
           (int(testInstance[0] * width), int(testInstance[1] * height)), 5,
           (255, 255, 255), -1)

for x in range(len(neighbors)):
    cv2.circle(feature_space,
               (int(neighbors[x][0] * width), int(neighbors[x][1] * height)),
               2, (255, 255, 255), -1)
cv2.imshow('feature space tests_contours[0]', feature_space)
cv2.waitKey(0)
cv2.destroyAllWindows()

#All tests_contours
for i in range(len(tests_feature)):
    testInstance = [tests_feature[i][0], tests_feature[i][1]]
    neighbors = knn.getNeighbors(all_feature, testInstance, k)
    if knn.getResponse(neighbors) == "nut":
        print('the test instance is nuts.')
        cv2.drawContours(tests_draw, tests_contours, i, (255, 0, 0), 3)
    else:
        print('the test instance is bolt.')
        cv2.drawContours(tests_draw, tests_contours, i, (0, 255, 0), 3)

cv2.imshow("tests - contours", tests_draw)
cv2.waitKey(0)
cv2.destroyAllWindows()
Ejemplo n.º 7
0
    accuracy_knn = []
    for n_prototypes in number_prototypes:
        prototypes_lvq1 = train_prototypes_lvq1(lvq_training_set, n_prototypes,
                                                lrate, epochs)
        prototypes_lvq2 = train_prototypes_lvq2(prototypes_lvq1,
                                                lvq_training_set, lrate,
                                                epochs)
        prototypes_lvq3 = train_prototypes_lvq3(prototypes_lvq2,
                                                lvq_training_set, lrate,
                                                epochs)

        for k in kn:
            predictions = []
            for row in range(len(knn_test_set)):
                neighbors = getNeighbors(prototypes_lvq1, knn_test_set[row], k)
                results = getResponse(neighbors)
                predictions.append(results)
            accuracy = getAccuracy(knn_test_set, predictions)
            if k == 1:
                accuracy_lvq1_k1.append(accuracy)
            else:
                accuracy_lvq1_k3.append(accuracy)
            print('With LVQ1 for ' + str(n_prototypes) +
                  '  and for dataset -> ' + str(filename) +
                  ' accuracy for k= ' + str(k) + ': ' + repr(accuracy) + '%')

        for k in kn:
            predictions = []
            for row in range(len(knn_test_set)):
                neighbors = getNeighbors(prototypes_lvq2, knn_test_set[row], k)
                results = getResponse(neighbors)