Created
August 13, 2017 23:27
-
-
Save WoodProgrammer/eeb04dad9a9ae8986d776099a2e4452a to your computer and use it in GitHub Desktop.
Knn Algorithm Basic Extension on python 2.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| def distance(class_of_cluster,x1,x2,y1,y2): | |
| y_square = math.pow((y2-y1),2) | |
| x_square = math.pow((x2-x1),2) | |
| return class_of_cluster,math.sqrt(x_square+y_square) | |
| def knn(data_set,target_data,neighbors_count): | |
| distances = [] | |
| for cl_data in data_set: | |
| distances.append(distance(cl_data[0],example_data[0][0],cl_data[1],example_data[0][1],cl_data[2])) | |
| return distances[0][0],distances[:neighbors_count] | |
| data_arr = [('A',1,2),('A',2,3),('A',4,3),('B',5,6),('B',6,7),('B',7,8),('B',8,9)] | |
| example_data = [(1,3)] | |
| print knn(data_arr,example_data,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment