def call_knn(data): print("KNN-----------------") print("accuracy: " , knn.calculate_accuracy( data ) ) #calculate the best k in [3,15] accuracies = [] ks = [] posCounter=0 for i in range(3,16): accuracies.append(knn.calculate_accuracy( data, i)) ks.append(i) #calculate the best accuracy and print the k maxAc = accuracies[0] maxK = ks[0] for i in range(1,len(accuracies)): if (accuracies[i] > maxAc): maxAc=accuracies[i] maxK=ks[i] bestk = maxK print("The best k is: ", bestk)
def flores_data_set_run(): data_set = arff.load(open('./data/flores.arff')) test_set, train_set = list_helper.separate_list(data_set['data'], 10) results = [] for x in range(len(test_set)): result = knn.classify(train_set, test_set[x][:-1]) results.append(result) print "Real: " + test_set[x][-1] + " - Predicted: " + result accuracy = knn.calculate_accuracy(test_set, results) print "Accuracy: " + str(accuracy) + "%"
def prediccion_data_set_run(): data_set = arff.load(open('./data/prediccion.arff')) cleaned_data_set = [list_helper.unicode_to_int(x) for x in data_set['data']] test_set, train_set = list_helper.separate_list(cleaned_data_set, 1) results = [] for x in range(len(test_set)): result = knn.classify(train_set, test_set[x][:-1]) results.append(result) print "Real: " + str(test_set[x][-1]) + " - Predicted: " + str(result) accuracy = knn.calculate_accuracy(test_set, results) print "Accuracy: " + str(accuracy) + "%"