The KNN of machine learning

Source: Internet
Author: User

KNN is mainly used for text classification, cluster analysis, predictive analysis, dimensionality reduction, etc.
The central idea is to classify by measuring the distance between different eigenvalue values.
The algorithm is very simple, but this is a supervisory algorithm, training data need to be manually marked. The main idea of the algorithm is to calculate the distance between the candidate sample and all training samples, and select the most frequently occurring classification in the K nearest distance data as the category of the new sample.

From numpy Import *
import operator
def classify0 (sample,dataset,labels,k):
    samplearray=tile (Sample, ( dataset.shape[0],1))
    distance=sum ((samplearray-dataset) **2,1)
    s=np.argsort (distance)
    counter={} For
    I in Arange (k):
        key=labels[s[i] "
        if key in counter:
            counter[key]=counter[key]+1
        Else:
            counter[key]=1
    c=sorted (Counter.items (), Key=operator.itemgetter (1), reverse=true)
    return c[0][0] ;
Normalization

When calculating distances, the data ranges for different features are not the same. Example: Sample features include height, weight, and mileage. The number of miles in the flight range is very high relative to height and weight. When calculating the distance between samples, the difference in mileage will be decisive, and it is generally assumed that all features should be equally important and that the values should be normalized to between 0 and 1:
Vn=vovmax−vmin V_{n}=\frac{v_o}{v_{max}-v_{min}}

def autonorm (dataSet):
    Datarange=dataset.max (axis=0)-dataset.min (axis=0)
    Dataset=dataset/tile (datarange , (dataset.shape[0],1))
    return DataSet
Optimized

KNN algorithm is simple and effective, but there are disadvantages: large computational capacity, each time the need to calculate the distance between the sample to be classified and all the known samples can be obtained k nearest field points, the computational capacity is large. Therefore, it is generally necessary to edit the known sample points (only for large capacity, and to remove samples that do not have much effect on the classification). There is also the use of K decision Trees to reduce computational capacity and storage space. The number of samples in some classes in the training sample set is large and the number of other classes is small. Therefore, a weighted approach is required.

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.