4.2 Nearest Neighbor Rule classification (k-nearest Neighbor) KNN algorithm application

Source: Internet
Author: User

1 Data Set Description: Iris

150 instances sepals length, sepals width, petal length, petal width(sepal length, sepal width, petal length and petal width) Category:Iris Setosa, Iris versicolor, Iris virginica.  

  2. Use Python's machine learning library sklearn: sklearnexample.py from sklearn import neighborsfrom sklearn import datasets  KNN = neighbors. Kneighborsclassifier ()   iris = Datasets.load_iris ()   print Iris knn.fit (Iris.data, Iris.target)  predictedlabel = Knn.predict ([[0.1, 0.2, 0.3, 0.4]])  print predictedlabel     3. KNN implementation implementation:  # Example of KNN implemented from Scratch in Python import csvimport randomimport ma Thimport operator def loaddataset (filename, split, trainingset=[], testset=[]):    with open (filename, ' RB ') as csvfile:        lines = Csv.reader (csvfile)         DataSet = list (lines) &N Bsp       for x in range (len (DataSet)-1):            for y in range (4):  &NB Sp             dataset[x][y] = float (dataset[x][y])             IF Random. Random () < split:                trainingset.append (Dataset[x])     & nbsp       else:                testset.append (dataset[x])   d EF euclideandistance (Instance1, Instance2, length):    distance = 0    for x in range (length):  & nbsp     Distance + = POW ((instance1[x]-instance2[x]), 2)     return math.sqrt (distance)  def Getneigh Bors (Trainingset, Testinstance, K):    distances = []    length = Len (testinstance) -1    for X in range (len (trainingset)):        dist = euclideandistance (testinstance, trainingset[x], length) & nbsp       Distances.append ((trainingset[x], Dist)     Distances.sort (Key=operator.itemgetter (1) )     Neighbors = []    for x in range (k):        neighbors.append (distances[x][0])     REturn neighbors def getResponse (neighbors):    classvotes = {}    for x in range (len (neighbors)) :        response = neighbors[x][-1]        If response in classvotes:  &nbsp ;         Classvotes[response] + + 1        else:          & nbsp Classvotes[response] = 1    Sortedvotes = sorted (Classvotes.iteritems (), Key=operator.itemgetter (1), reverse =true)     return sortedvotes[0][0] def getaccuracy (testset, predictions):    correct = 0    for x in range (len (testset)):        if testset[x][-1] = = predictions[x]:      & nbsp     Correct + = 1    return (Correct/float (Len (testset))) * 100.0    def Main ():  & nbsp # Prepare data    trainingset=[]    testset=[]    split = 0.67    Loaddataset (R ' D: \maiziedu\dEeplearningbasics_machinelearning\datasets\iris.data.txt ', Split, Trainingset, Testset)     print ' Train set : ' + repr (len (trainingset))     print ' Test set: ' + repr (len (testset))     # Generate PREDICTIONS&NBSP ;   predictions=[]    k = 3    for x in range (len (testset)):        Neighbors = Getneighbors (Trainingset, testset[x], K)         result = GetResponse (neighbors)       & nbsp Predictions.append (Result)         print (' > predicted= ' + repr (Result) + ', actual= ' + repr (testset[ X][-1])     accuracy = getaccuracy (Testset, predictions)     print (' accuracy: ' + repr (accuracy) + '% ')     main ()

4.2 Nearest Neighbor Rule classification (k-nearest Neighbor) KNN algorithm application

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.