Machine learning-minimum gap algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.