ImportNumPy asNpImportMatplotlib.pyplot asPlt#用最小距离法 (minimum distance algorithm) to detect which set the target point belongs to#the Distance of point x and Point ydefDist (x, y):returnNP.SQRT (NP.sum((x-Y** 2))#Well-known points to trainX_train=Np.array ([[1,1],[2,2.5],[3,1.2],[5.5,6.3],[6,9],[7,6]])#Colors of each pointY_train=[' Red ',' Red ',' Red ',' Blue ',' Blue ',' Blue ']#Testing Point, to find this point is red or blueX_test=Np.array ([3,4]) num= Len(X_train)# number of points in X_trainDistance=Np.zeros (num)# Numpy Arrays of zeros forIinch Range(num): Distance[i]=Dist (x_train[i],x_test)Print(distance) Min_index=Np.argmin (distance)# Index with smallest distancePrint(the color of point x_test is%s" %(Y_train[min_index]))Print("Point X_test are close to point%s" %(X_train[min_index]))#scatter: Scatter chartPlt.figure () Plt.scatter (x_train[:,0],x_train[:,1],s= the, color=y_train[:])#s的意思是 If maker is a dot R*r = s (here is a)Plt.scatter (x_test[0],x_test[1],s= the, color= ' Green ') Plt.show ()
Machine learning-minimum gap algorithm