The KNN algorithm---the K data before. __KNN algorithm principle

Source: Internet
Author: User
Brief Introduction

K Nearest neighbor algorithm is also called KNN algorithm, K nearest neighbor algorithm. K indicates the nearest K-Data sample.
The individual feels that the emphasis is on how the distance is expressed, how it is calculated, whether it is simple to use a distance formula, or a complex weighted calculation. The final output will be
a distance value. The remaining questions can be abstracted into a first k data. Code

#include <vector> #include <iostream> using namespace std;
void Adjust_heap (vector<int> & VEC, int length, int i);
void Gen_max_heap (vector<int> & VEC);

void Get_kmax_value (vector<int> &vec, vector<int> &kvec, int k);
    int main () {vector<int> Vec = {0, 4, 1, 3, 2, 16, 9, 10, 14, 8, 7};//first bit without gen_max_heap (VEC);
    Vector<int> Kvec;
    Get_kmax_value (Vec,kvec, 5);
    for (auto X:kvec) cout << x << Endl;
    Cin.get ();
return 0;
    } void Adjust_heap (Vector<int> & VEC, int length, int root) {int left = 2 * i;
    int right = 2 * i + 1;
    int largest = root;
            while (Left < length | | Right < length) {if (left < length && Vec[left] > Vec[largest])
        largest = left;
        if (right < length && vec[right] > vec[largest]) largest = right; if (I!= largest)//continue to adjust the affected subtree {SWAP (Vec[largest], vec[i]);
            root = largest;
            left = 2 * root;
        right = 2 * root + 1;
        } else {break;
    }} void Gen_max_heap (Vector<int> & VEC) {int length = vec.size ();
    for (Auto root = (Vec.size ()-1)/2; root!= 0; root--) {adjust_heap (Vec, length, root);
    } void Get_kmax_value (vector<int> &vec, vector<int> &kvec, int k) {int length = vec.size ();
        for (int i = 0; I!= k; ++i) {kvec.push_back (vec[1));
        Swap (vec[1], vec[length-1-i]);
    Adjust_heap (VEC, Length-1-I, 1); Compile with compiler that supports C++11
AnalysisThe former K data is used, and the common method is to use Max_heap (Dagen).
Introduction Dagen, a complete binary tree, each node value is greater than its child node value. I code to write the comparison of Slag, in the selection of data structure when the choice of vector, in use when subscript this point is very headache.
Subscript problem for Parent-child node:
2.1 Vec[0] element is not necessary. Subscript starting from 1 to N, child node n, parent node [N/2] corresponds to implementation.
2.2 The selection of subscript and cyclic variables. Core in this adjust_heap operation
How to build a large heap, first starting from the smallest subtree, should be the leaf node. But there is no need to compare a node.
Start with the node subtree of a root and a layer of children. Such a subtree can be easily adjusted to a root value greater than the child node.
The key is to start with a 3-tier subtree structure. A subtree consisting of root and two 2-layer subtrees that have been adjusted respectively. If Root and two children
There are adjustments that will affect the adjustment of the child's subtree, then will continue to adjust the affected subtree. No need to adjust, because two children are also adjusted well, so the entire subtree does not have to adjust, break can be.

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.