************** written in the front **************
This article is a summary of the learning process and some personal ideas, only recorded, continuous update ...
thin only sparse, I implore you if you find any problems please be sure to exchange a positive message Oh ~ ~
Personal opinion: The supervision study in machine learning can be divided into regression and fitting, more methods, it is necessary to learn from the basic simple step by step, although not necessarily for each formula are deduced, but at least to know the principle, because the next more difficult algorithm is based on the basis of the algorithm to increase and upgrade. The patchwork study still feels not solid. Of course I'm still a little white.
Note: From the Book Machine learning Combat
The basic principle of KNN:
K-Nearest Neighbor algorithm, to achieve the classification function, where k is the choice and the target K distance nearest point to determine what kind of target. The distance uses the European distance.
Note: A. Use a dictionary; B. Using iterators
The following code from the machine learning related to the section, this is only to write their own implementation
#-*-coding:utf-8-*-"" "aiming to achive the KNN algorithm" "" Import NumPy as NP import operator de F knnclassfy (Test_x,train_x,train_y,k): M=train_x.shape[0] Diffmat=np.tile (test_x, (m,1))-train_x SqDiffMat=dif FMAT**2 # European Distance Sqdistance=sqdiffmat.sum (Axis=1) # Row Add distance=sqdistance**0.5 sorteddistanceindex=distance.
Argsort () # from small to large sort classcount={} for I in range (0,k): # Select before K Votelabel=train_y[sorteddistanceindex[i]] Classcount[votelabel]=classcount.get (votelabel,0) +1 sortedclasscount=sorted (Classcount.iteritems (), Key=operat
Or.itemgetter (1), reverse=true) # from large to small sort return sortedclasscount[0][0] if __name__== ' __main__ ': # the DataSet Train_x=np.array ([[[0,0],[0.1,0.1],[1.0,1.0],[1.1,1.4]]]) train_y=np.array ([' A ', ' a ', ' B ', ' B ']) test_x=np.array ([[0]. 2,0.1]] test_y=knnclassfy (test_x,train_x,train_y,3) print ("The classsfied result of a KNN is%s"% (test_y))