第一题,信息增益代码 #include <bits/stdc++.h> #include <map> #include <math.h> using namespace std; typedef struct data{     int clas[2]; }data; int n; map<int,data> mp; int classify[2] = {0}; double cpt_entroy(int m[]){     int nu = m[0] + m[1];     double first = 0;     double second = 0;     if(m[0] != 0){         double tmp = 1.0 * m[0] / nu;         first = -1 * tmp * log(tmp) / log(2);     }     if(m[1] != 0){         double tmp = 1.0 * m[1] / nu;         second = -1 * tmp * log(tmp) / log(2);     }     double entroy = 1.0 * nu / n * (first + second);     return entroy; } int main() {     int a, b;     char indep;     cin >> n;     for(int i=0; i<n; ++i){         cin>>a>>indep>>b;         classify[b]++;         if(mp.find(a) == mp.end())             mp[a].clas[b] = 0;         mp[a].clas[b] += 1;     }     double gain_all = cpt_entroy(classify);     double ans = gain_all;     map<int, data>::iterator it;     for(it = mp.begin(); it != mp.end(); ++it) {         double pro = cpt_entroy(it->second.clas);         ans -= pro;     }     int dans = (ans + 0.005) * 100;     double res = double(dans) / 100;     printf("%.2f\n",res);     return 0; }
点赞 2

相关推荐

弦五Strings:他之所以会举报你代课是因为在这种人眼里正常上课就是正义代课就是邪恶,典型二极管思维,处理方法就是私下沟通,你就说你自己家里经济困难或者家里父母生病什么之类的,需要去打工挣钱,用尽孝的正义对冲他认为的上课的正义,他可能就妥协了。
我的实习日记
点赞 评论 收藏
分享
06-25 16:25
梧州学院 Java
愿汐_:项目介绍那么长,然而你做了啥就一句话?
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务