用熵来评价整个随机变量
平均的信息量,而平均最好的量度就是随机变量的期望
,即熵的定义如下:
计算字符的信息熵,参考代码如下:
package sk.ann;
import java.util.Scanner;
public class InfoEntropy {
public static double Entropy(String str) {
double H = .0;
int sum = 0;
int[] letter = new int[26];//26个字符
str = str.toUpperCase(); // 将小写字母转换成大写
for (int i = 0; i < str.length(); i++) { // 统计字母个数
char c = str.charAt(i);
if (c >= 'A' && c <= 'Z') {
letter[c - 'A']++;
sum++;
}
}
//计算信息熵,将字母出现的频率作为离散概率值
for (int i = 0; i < 26; i++) {
double p = 1.0 * letter[i] / sum;//单个字母的频率
if (p > 0)
H += -(p * Math.log(p) / Math.log(2));// H = -∑Pi*log2(Pi)
}
return H;
}
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner scan = new Scanner(System.in);
String str = scan.next();
double H = Entropy(str);
System.out.printf("%4.2f\n", H);
}
}
执行结果:
请输入字符串:
are you ok!yes i am ok!
1.58