예제 #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)
예제 #2
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)
예제 #3
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
예제 #4
0
split = 0.67
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) + '%')






예제 #5
0
im1, tests_contours, tests_hierarchy = cv2.findContours(
    closed_tests, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

tests_feature = createFeature(tests_contours, "")

cv2.drawContours(tests_draw, tests_contours, -1, (255, 0, 0), 3)
cv2.imshow("tests - contours", tests_draw)
cv2.waitKey(0)
cv2.destroyAllWindows()

k = 5
all_feature = nuts_feature + bolts_feature

#for test tests_contours[0]
testInstance = [tests_feature[0][0], tests_feature[0][1]]
neighbors = knn.getNeighbors(all_feature, testInstance, k)

height = 500
width = 500
feature_space = np.zeros((height, width, 3), np.uint8)

for i in range(len(all_feature)):
    if all_feature[i][2] == 'nut':
        cv2.circle(
            feature_space,
            (int(all_feature[i][0] * width), int(all_feature[i][1] * height)),
            2, (0, 0, 255), -1)
    else:
        cv2.circle(
            feature_space,
            (int(all_feature[i][0] * width), int(all_feature[i][1] * height)),
예제 #6
0
    accuracy_lvq3_k3 = []
    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)