コード例 #1
0
    def classification(self, k_value, length, actual_z1, actual_z2):
        predicitions = []

        for x in range(length):
            neighbors = knn_func.getNeighbors(actual_z1, actual_z2[x], k_value)
            result = knn_func.getResponse(neighbors)
            predicitions.append(result)
        accuracy = knn_func.getAccuracy(self.dataset_z2, predicitions)
        knn_func.createErrorFile(self.dataset_z2, predicitions)

        return accuracy
コード例 #2
0
def bestZ1(k_value):
    i = 1
    predictions = []
    accuracy_list = []
    while (i <= 10):
        instance = "z1n{0}.txt".format(i)
        print("Testing instance: {0}".format(instance))

        with open(instance, 'r') as content:
            lines = csv.reader(content)
            dataset_z1 = list(lines)
        length_z1 = len(dataset_z1)

        for x in range(len(dataset_z1)):
            for y in range(4):
                dataset_z1[x][y] = float(dataset_z1[x][y])

        with open('z2.txt', 'r') as content:
            lines = csv.reader(content)
            dataset_z2 = list(lines)
        length_z2 = len(dataset_z2)

        for x in range(length_z2):
            for y in range(4):
                dataset_z2[x][y] = float(dataset_z2[x][y])

        for x in range(length_z1):
            neighbors = knn_func.getNeighbors(dataset_z1, dataset_z2[x],
                                              k_value)
            result = knn_func.getResponse(neighbors)
            predictions.append(result)

        accuracy = knn_func.getAccuracy(dataset_z2, predictions)
        print("accuracy: ", accuracy)
        accuracy_list.append(accuracy)
        predictions = []

        i = i + 1

    maximum = 0
    for x in range(len(accuracy_list)):
        if accuracy_list[x] > maximum:
            maximum = accuracy_list[x]
            best_i = x + 1
    print("Best instance of Z1 is: z1n{0}, with {1}%% of approval".format(
        best_i, maximum))

    return best_i
コード例 #3
0
def bestK():
    with open('z1.txt', 'r') as content:
        lines = csv.reader(content)
        dataset_z1 = list(lines)
    length_z1 = len(dataset_z1)

    for x in range(len(dataset_z1)):
        for y in range(4):
            dataset_z1[x][y] = float(dataset_z1[x][y])

    with open('z2.txt', 'r') as content:
        lines = csv.reader(content)
        dataset_z2 = list(lines)
    length_z2 = len(dataset_z2)

    for x in range(length_z2):
        for y in range(4):
            dataset_z2[x][y] = float(dataset_z2[x][y])

    k = 1
    limit = math.trunc(length_z1 / 2)
    predictions = []
    accuracy_list = []
    k_list = []
    while (k < limit):
        print("Testing K = ", k)
        for x in range(0, length_z1):
            neighbors = knn_func.getNeighbors(dataset_z1, dataset_z2[x], k)
            result = knn_func.getResponse(neighbors)
            predictions.append(result)
        accuracy = knn_func.getAccuracy(dataset_z2, predictions)
        knn_func.createErrorFile(dataset_z2, predictions)
        accuracy_list.append(accuracy)
        predictions = []
        print('Accuracy: ' + str(accuracy) + '%')
        k_list.append(k)
        k = k + 2

    maximum = 0
    for x in range(0, len(accuracy_list)):
        if accuracy_list[x] > maximum:
            maximum = accuracy_list[x]
            best_k = k_list[x]
    print("Best K is: {0} with {1}%%".format(best_k, maximum))

    return (best_k, maximum)
コード例 #4
0
ファイル: classify_z3.py プロジェクト: handreygalon/knn
def classifyZ3(k_value, instance_z1):
    k_value = int(k_value)

    with open(instance_z1, 'r') as content:
        lines = csv.reader(content)
        dataset_z1 = list(lines)
    length_z1 = len(dataset_z1)

    for x in range(length_z1):
        for y in range(4):
            dataset_z1[x][y] = float(dataset_z1[x][y])

    with open('z3.txt', 'r') as content:
        lines = csv.reader(content)
        dataset_z3 = list(lines)
    length_z3 = len(dataset_z3)

    for x in range(length_z3):
        for y in range(4):
            dataset_z3[x][y] = float(dataset_z3[x][y])

    predictions = []
    accuracy_list = []
    for x in range(length_z3):
        neighbors = knn_func.getNeighbors(dataset_z1, dataset_z3[x], k_value)
        result = knn_func.getResponse(neighbors)
        predictions.append(result)

    accuracy = knn_func.getAccuracy(dataset_z3, predictions)
    print("accuracy: ", accuracy)
    accuracy_list.append(accuracy)
    predictions = []

    maximum = 0
    for x in range(len(accuracy_list)):
        if accuracy_list[x] > maximum:
            maximum = accuracy_list[x]
    print("Result Z3 classification with K = {0}, using {1} instance of Z1, presents {2}%% efficiency".format(k_value, instance_z1, maximum))

    return maximum
コード例 #5
0
ファイル: main.py プロジェクト: BohdanShaban/ML-knn-algorithm
def main():

	# READ IRIS && TRAIN/TEST SPLIT	
	trainingSet=[]
	testSet=[]
	split = 0.7
	loadDataset('iris.data', split, trainingSet, testSet)
	print ('Train set: ' + repr(len(trainingSet)))
	print ('Test set: ' + repr(len(testSet)))

	# NEIGHBOURS CALCULATION && RESPONSE PREDICTION
	predictions=[]
	k = 3
	for x in range(len(testSet)):
		neighbors = getNeighbors(trainingSet, testSet[x], k)
		predicted = getResponse(neighbors)
		predictions.append(predicted)
		print('> predicted=' + repr(predicted) + ', actual=' + repr(testSet[x][-1]))

	# ACCURACY ESTIMATION
	accuracy = getAccuracy(testSet, predictions)
	print('Accuracy: ' + repr(accuracy) + '%')