计算信息熵的简单做法
来源:互联网 发布:js 判断ie9以下版本 编辑:程序博客网 时间:2024/04/27 15:16
信息熵的简单做法
p*log(p)其中log函数是以2为底的函数,在C语言中,计算概率是可以应用换底公式来计算上面的公式。
直接贴代码了:
#include <map>#include <cmath>#include <iostream>#include <string>#include <stdio.h>int GetSumCnt(std::map<std::string, int> &smap){ int cnt_sum = 0; std::map<std::string, int>::iterator iter; for(iter = smap.begin(); iter != smap.end(); ++ iter) cnt_sum += iter->second; return cnt_sum;}int main(){ float score = 0.0, p_tmp = 0.0; int cnt = 0; int cnt_sum = 0; std::string str; std::map<std::string, int> smap; std::map<std::string, int>::iterator iter; str = "baidu"; cnt = 3; smap[str] += cnt; score = 0.0; cnt_sum = GetSumCnt(smap); for(iter = smap.begin(); iter != smap.end(); ++ iter) { p_tmp = ((float)iter->second) / ((float)cnt_sum); printf("The p_tmp is %f\n", p_tmp); score -= p_tmp * (log(p_tmp) / log(2)); } printf("The score is %f\n", score); str = "beijing"; cnt = 3; smap[str] += cnt; score = 0.0; cnt_sum = GetSumCnt(smap); for(iter = smap.begin(); iter != smap.end(); ++ iter) { p_tmp = ((float)iter->second) / ((float)cnt_sum); printf("The p_tmp is %f\n", p_tmp); score -= p_tmp * (log(p_tmp) / log(2)); } printf("The score is %f\n", score); str = "beijing"; cnt = 3; smap[str] += cnt; score = 0.0; cnt_sum = GetSumCnt(smap); for(iter = smap.begin(); iter != smap.end(); ++ iter) { p_tmp = ((float)iter->second) / ((float)cnt_sum); printf("The p_tmp is %f\n", p_tmp); score -= p_tmp * (log(p_tmp) / log(2)); } printf("The score is %f\n", score); return 0;}
在此,主要想记录的是换底公式,在写代码的时候,可以让代码更加方便简洁一些。
0 0
- 计算信息熵的简单做法
- 酸菜鱼的简单做法
- 信息熵的计算
- 简单的时钟的做法
- 最简单的豆浆做法
- 简单的文本编辑器做法
- 聊天气泡的简单做法
- LigerUI表格的简单做法
- 信息熵的计算心得
- 信息与熵的计算
- 获取windows版本信息的做法
- 几种简单的粥的做法
- 简单的贪吃蛇的另一种做法
- 新闻内容页分页的简单做法
- 两栏对齐的最简单做法
- 数字时钟最简单的一种做法
- 提高seo的几点简单做法
- C# 跨线程赋值的简单做法
- 浅谈C++中指针和引用的区别
- 字符串
- 【C】【笔记】《C和指针》第15章 输入/输出函数
- hdu1176 免费馅饼 (动态规划)
- LBP(局部二值模式)
- 计算信息熵的简单做法
- mac 下 mysql
- ibatis中iterate的用法
- TCP打洞技术
- 【鸟哥的linux私房菜-学习笔记】磁盘配额 (Quota)、磁盘阵列 (RAID)、逻辑卷轴管理员 (LVM) 初步了解
- 九度OJ-1019简单计算器
- 指针的基本操作
- 【C】【笔记】《C和指针》第16章 标准函数库
- [笔记-架构探险]框架优化与功能扩展3.1.优化Action参数、提供文件上传功能、与ServletApi解耦.