KNN算法
发布日期:2021-05-10 16:06:55 浏览次数:12 分类:精选文章

本文共 1841 字,大约阅读时间需要 6 分钟。

```cpp#include
#include
#include
using namespace std;struct Point { double x, y, dis; int type; Point(double xx = 0, double yy = 0, int typee = 0) { x = xx; y = yy; type = typee; }};double diss(Point a, Point b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);}vector
v;void init() { v.push_back(Point(1.0, 2.0, 1)); v.push_back(Point(10.0, 10.0, 2)); v.push_back(Point(5.0, 4.0, 3));}bool cmp(Point a, Point b) { return a.dis < b.dis;}Point knn(Point z, int k) { for (int i = 0; i < v.size(); i++) { v[i].dis = diss(v[i], z); } sort(v.begin(), v.end(), cmp); vector
a; for (int i = 0; i < k; i++) { a.push_back(v[i].type); } sort(a.begin(), a.end()); map
mp; for (int i = 0; i < k; i++) { mp[a[i]]++; } map
::iterator it; int maxx = 0; for (it = mp.begin(); it != mp.end(); it++) { if (maxx < it->first) { maxx = it->first; } } z.type = maxx; v.push_back(z); return z;}int main() { init(); Point z; printf("���������������x:"); scanf("%lf", &z.x); printf("���������������y:"); scanf("%lf", &z.y); Point ans = knn(z, 1); printf("%d", ans.type); return 0;}

��� ������ C++ ������������ ������������ ������ ��������� ���������������. ��� ��������� KNN (K-Nearest Neighbor) ��������������� ������������ ������������. ERK classNames��� ������������ ������ closest points��� ������������ ������������.

c��i �����t ������:

������ ���������

  • ���igrant coordinates��� ������������ ������ printf ������ ������������ scanf ��������� ���������������.
  • KNN ������ ���������kazy predictions:
  • results printf��� ���������������.
  • ���.Calendar��� ��������� accidents��� ������������ ������������, ���������������inoFoot'd.views = \ herzlichen Dank-abschied nicht erfolgt.< towndituous occasional panties):'s algorithm��� yay��n������ ������ ���]+

    上一篇:朴素贝叶斯算法
    下一篇:1. 两数之和

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月27日 00时39分13秒