This is the first algorithm I came into contact with -----knn (proximity algorithm)
Self-understanding is to use the Pythagorean theorem to calculate the distance between point C and A and B. If the distance is smaller, it is considered that C tends to that value.
Python implements the knn process
1 from numpy import * 2 3 #simulate training data---array 4 with 4 rows and 6 columns train_data = array([ 5 [1, 2, 3, 4, 5, 6], # A type 6 [7, 5, 4, 1, 7, 5], # B type 7 [4, 7, 6, 5, 3, 8], # C type 8 [5, 2, 3, 4, 9, 4], # D type 9 ] ) 10 #Each line is a type, that is, 4 lines correspond to the following 4 types respectively 11 labels = [ " A " , " B " , " C" , " D " ] 12 13 #Simulate test data 14 test_data = array([1, 5, 8, 6, 4, 7 ]) 15 16 17 # Purpose: Calculate which category the test data belongs to through the knn algorithm 18 def knn(test_data, train_data, labels): 19 train_data_lines = train_data.shape[0] #Get the number of lines of training data 20 test_data = tile(test_data, (train_data_lines, 1)) #Organize the test data as much as the training data Number of rows, calculate "distance" 21 dif_value = test_data - train_data #difference 22 square_value_1 = dif_value **2 #square 23 line_num = square_value_1.sum(axis=1) #Sum on each line 24 square_value_2 = line_num**0.5 #Root 25 sortdistance = square_value_2.argsort() #sort in descending order 26 return labels[sortdistance[0]] 27 28 res = knn(test_data,train_data,labels) 29 print (res)
It's that simple... lol, hope it helps.