c/c++语言 knn 算法,Knn算法C++实现

相对简单的模拟。C++11

/************************************************

Author :guanjun

Created Time :2016/6/20 18:22:32

File Name :1.cpp

*************************************************/#include#include#include#include#include#include#include#include#include#include

#define maxn 10010

using namespacestd;intk;structnode{doublex1,x2,x3,x4,x5;strings;

}nod[maxn];

pairp[maxn];

mapmp;

ifstream fin;int input(strings){

fin.open(s);if(!fin){

cout<

exit(1);

}int i=1;while(fin>>nod[i].x1>>nod[i].x2>>nod[i].x3>>nod[i].x4>>nod[i].x5>>nod[i].s){

nod[i].x1*=100.0;nod[i].x2*=100.0;nod[i].x3*=100.0;nod[i].x4*=100.0;nod[i].x5*=100.0;

i++;

}returni;

}double mul(double x,doubley){return (x-y)*(x-y);

}doubledis(node a,node b){double tmp=mul(a.x1,b.x1)+mul(a.x2,b.x2)+mul(a.x3,b.x3)+mul(a.x4,b.x4)+mul(a.x5,b.x5);returnsqrt(tmp);

}string knn(intn,node x){for(int i=1;i<=n;i++){

p[i]={dis(x,nod[i]),nod[i].s};

}//for(int i=1;i<=n;i++)cout<

sort(p+1,p+1+n);//for(int i=1;i<=n;i++)cout<

mp.clear();for(int i=1;i<=k;i++)mp[p[i].second]++;int Max=0;stringans;for(auto x:mp){if(x.second>Max){

Max=x.second;

ans=x.first;

}

}returnans;

}intmain()

{intn,m;

puts("input k");

cin>>k;

n=input("in.txt");

puts("input the test case");

cin>>m;

node tes;for(int i=1;i<=m;i++){

cin>>tes.x1>>tes.x2>>tes.x3>>tes.x4>>tes.x5;

tes.x1*=100.0;tes.x2*=100.0;tes.x3*=100.0;tes.x4*=100.0;tes.x5*=100.0;

cout<

}return 0;

}

训练数据:

0 0 0 0 0 very_low

0.080.080.10.240.9High

0.060.060.050.250.33Low

0.10.10.150.650.3Middle

0.080.080.080.980.24Low

0.090.150.40.10.66Middle

0.10.10.430.290.56Middle

0.150.020.340.40.01very_low

0.20.140.350.720.25Low

000.50.20.85High

0.180.180.550.30.81High

0.060.060.510.410.3Low

0.10.10.520.780.34Middle

0.10.10.70.150.9High

0.20.20.70.30.6Middle

0.120.120.750.350.8High

0.050.070.70.010.05very_low

0.10.250.10.080.33Low

0.150.320.050.270.29Low

0.20.290.250.490.56Middle

0.120.280.20.780.2Low

0.180.30.370.120.66Middle

0.10.270.310.290.65Middle

0.180.310.320.420.28Low

0.060.290.350.760.25Low

0.090.30.680.180.85High

0.040.280.550.250.1very_low

0.090.2550.60.450.25Low

0.080.3250.620.940.56High

0.150.2750.80.210.81High

0.120.2450.750.310.59Middle

0.150.2950.750.650.24Low

0.10.2560.70.760.16Low

0.180.320.040.190.82High

0.20.450.280.310.78High

0.060.350.120.430.29Low

0.10.420.220.720.26Low

0.180.40.320.080.33Low

0.090.330.310.260very_low

0.190.380.380.490.45Middle

0.020.330.360.760.1Low

0.20.490.60.20.78High

0.140.490.550.290.6Middle

0.180.330.610.640.25Middle

0.1150.350.650.270.04very_low

0.170.360.80.140.66Middle

0.10.390.750.310.62Middle

0.130.390.850.380.77High

0.180.340.710.710.9High

0.090.510.020.180.67Middle

0.060.50.090.280.25Low

0.230.70.190.510.45Middle

0.090.550.120.780.05Low

0.240.750.320.180.86High

0.180.720.370.290.55Middle

0.10.60.330.420.26Low

0.20.520.360.840.25Middle

0.090.60.660.190.59Middle

0.180.510.580.330.82High

0.080.580.60.640.1Low

0.090.610.530.750.01Low

0.060.770.720.190.56Middle

0.150.790.780.30.51Middle

0.20.680.730.480.28Low

0.240.580.760.80.28Middle

0.250.10.030.090.15very_low

0.320.20.060.260.24very_low

0.290.060.190.550.51Middle

0.280.10.120.280.32Low

0.30.080.40.020.67Middle

0.270.120.370.290.58Middle

0.310.10.410.420.75High

0.290.150.330.660.08very_low

0.30.20.520.30.53Middle

0.280.160.690.330.78High

0.2550.180.50.40.1very_low

0.2650.060.570.750.1Low

0.2750.10.720.10.3Low

0.2450.10.710.260.2very_low

0.2950.20.860.440.28Low

0.320.120.790.760.24Low

0.2950.250.260.120.67Middle

0.3150.320.290.290.62Middle

0.250.290.150.480.26Low

0.270.10.10.70.25Low

0.2480.30.310.20.03very_low

0.3250.250.380.310.79High

0.270.310.320.410.28Low

0.290.290.40.780.18Low

0.290.30.520.090.67Middle

0.2580.280.640.290.56Middle

0.320.2550.550.780.34Middle

0.2510.2650.570.60.09very_low

0.2880.310.790.230.24Low

0.3230.320.890.320.8High

0.2550.3050.860.620.15Low

0.2950.250.730.770.19Low

0.2580.250.2950.330.77High

0.290.250.290.290.57Middle

0.2430.270.080.420.29Low

0.270.280.180.480.26Low

0.2990.320.310.330.87High

0.30.270.310.310.54Middle

0.2450.260.380.490.27Low

0.2950.290.310.760.1Low

0.290.30.560.250.67Middle

0.260.280.60.290.59Middle

0.3050.2550.630.40.54Middle

0.320.270.520.810.3Middle

0.2990.2950.80.370.84High

0.2760.2550.810.270.33Low

0.2580.310.880.40.3Low

0.320.280.720.890.58High

0.3290.550.020.40.79High

0.2950.590.290.310.55Middle

0.2850.640.180.610.45Middle

0.2650.60.280.660.07very_low

0.3150.690.280.80.7High

0.280.780.440.170.66Middle

0.3250.610.460.320.81High

0.280.650.40.650.13Low

0.2550.750.350.720.25Low

0.3050.550.50.110.333Low

0.30.850.540.250.83Middle

0.3250.90.520.490.76High

0.3120.80.670.920.5High

0.2990.70.950.220.66High

0.2650.760.80.280.28Low

0.2550.720.720.630.14Low

0.2950.60.720.880.28Middle

0.390.050.020.060.34Low

0.40.180.260.260.67Middle

0.450.040.180.550.07very_low

0.480.120.280.70.71High

0.40.120.410.10.65Middle

0.410.180.330.310.5Middle

0.380.10.40.480.26Low

0.370.060.320.780.1Low

0.410.090.580.180.58Middle

0.380.010.530.270.3Low

0.330.040.50.550.1very_low

0.420.150.660.780.4Middle

0.440.080.80.220.56Middle

0.390.150.810.220.29Low

0.420.210.870.560.48Middle

0.460.20.760.950.65High

0.3650.2430.190.240.35Low

0.330.270.20.330.1very_low

0.3450.2990.10.640.13Low

0.480.30.150.650.77High

0.490.2450.380.140.86High

0.3340.2950.330.320.3Low

0.360.290.370.480.13very_low

0.390.260.390.770.14Low

0.430.3050.510.090.64Middle

0.440.320.550.330.52Middle

0.450.2990.630.360.51Middle

0.4950.2760.580.770.83High

0.4650.2580.730.180.59Middle

0.4750.320.790.310.54Middle

0.3480.3290.830.610.18Low

0.3850.260.760.840.3Middle

0.4450.390.020.240.88High

0.430.450.270.270.89High

0.330.340.10.490.12very_low

0.40.330.120.30.9High

0.340.40.380.20.61Middle

0.380.360.460.490.78High

0.350.380.320.60.16Low

0.410.490.340.210.92High

0.420.360.630.040.25Low

0.430.380.620.330.49Middle

0.440.330.590.530.85High

0.40.420.580.750.16Low

0.460.440.890.120.66Middle

0.380.390.790.330.3Low

0.390.420.830.650.19Low

0.490.340.880.750.71High

0.460.640.220.220.6Middle

0.440.550.110.260.83High

0.3650.680.10.630.18Low

0.450.650.190.990.55High

0.460.780.380.240.89High

0.370.550.410.290.3Low

0.380.590.310.620.2Low

0.490.640.340.780.21Low

0.4950.820.670.010.93High

0.440.690.610.290.57Middle

0.3650.570.590.550.25Low

0.490.90.520.90.47High

0.4450.70.820.160.64Middle

0.420.70.720.30.8High

0.370.60.770.40.5Middle

0.40.610.710.880.67High

0.60.140.220.110.66Middle

0.550.10.270.250.29Low

0.680.190.190.480.1very_low

0.730.20.070.720.26Low

0.780.150.380.180.63Middle

0.550.10.340.30.1very_low

0.590.180.310.550.09very_low

测试数据

0.64 0.09 0.33 0.65 0.5 Middle

0.60.190.550.080.1very_low

0.690.020.620.30.29Low

0.780.210.680.650.75High

0.620.140.520.810.15Low

0.70.180.880.090.66Middle

0.750.0150.780.310.53Middle

0.550.170.710.480.11very_low

0.850.050.910.80.68High

0.780.270.130.140.62Middle

0.80.290.060.310.51Middle

0.90.260.190.580.79High

0.760.2580.070.830.34Middle

0.720.320.480.20.6Middle

0.60.2510.390.290.3Low

0.520.2880.320.50.3Low

0.60.310.310.870.58High

0.510.2550.550.170.64Middle

0.580.2950.620.280.3Low

0.610.2580.560.620.24Low

0.770.2670.590.780.28Middle

0.790.280.880.20.66Middle

0.680.270.780.310.57Middle

0.580.2990.730.630.21Low

0.770.290.740.820.68High

0.710.4750.130.230.59Middle

0.580.3480.060.290.31Low

0.880.3350.190.550.78High

0.990.490.070.70.69High

0.730.430.320.120.65Middle

0.610.330.360.280.28Low

0.510.40.40.590.23Low

0.830.440.490.910.66High

0.660.380.550.150.62Middle

0.580.350.510.270.3Low

0.5230.410.550.60.22Low

0.660.360.560.40.83High

0.620.370.810.130.64Middle

0.520.440.820.30.52Middle

0.50.40.730.620.2Low

0.710.460.950.780.86High

0.640.550.150.180.63Middle

0.520.850.060.270.25Low

0.620.620.240.650.25Middle

0.910.580.260.890.88High

0.620.670.390.10.66Middle

0.580.580.310.290.29Low

0.890.680.490.650.9High

0.720.60.450.790.45Middle

0.680.630.650.090.66Middle

0.560.60.60.310.5Middle

0.540.510.550.640.19Low

0.610.780.690.920.58High

0.780.610.710.190.6Middle

0.540.820.710.290.77High

0.50.750.810.610.26Middle

0.660.90.760.870.74High

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值