香农熵

Jam*_*mes 0 c++ entropy

以下 C++ 代码(按原样)来自http://rosettacode.org/wiki/Entropy。有错误 - 有人可以纠正吗?

#include <string>
#include <map>
#include <iostream>
#include <algorithm>
#include <cmath>

double log2( double number ) {
    return log( number ) / log( 2 ) ;
}

int main( int argc , char *argv[ ] ) {
    std::string teststring( argv[ 1 ] ) ;
    std::map<char , int> frequencies ;

    for ( char c : teststring )
        frequencies[ c ] ++ ;

    int numlen = teststring.length( ) ;
    double infocontent = 0 ;

    for ( std::pair<char , int> p : frequencies ) {
        double freq = static_cast<double>( p.second ) / numlen ;
        infocontent += freq * log2( freq ) ;
    }

    infocontent *= -1 ;

    std::cout << "The information content of " << teststring 
      << " is " << infocontent << " !\n" ;
    return 0 ;
}
Run Code Online (Sandbox Code Playgroud)

第一个错误似乎已修复:

double log2( double n )  
{  
    // log(n)/log(2) is log2.  
    return log( n ) / log( 2. );  
}
Run Code Online (Sandbox Code Playgroud)

我不确定他们想表达什么:

for ( char c : teststring )
Run Code Online (Sandbox Code Playgroud)

use*_*387 5

这个效果不错

template <typename T> static float ShannonEntropy(T data[],int elements){
    float entropy=0;
    std::map<T,long> counts;
    typename std::map<T,long>::iterator it;
    //
    for (int dataIndex = 0; dataIndex < elements; ++dataIndex) {
        counts[data[dataIndex]]++;
    }
    //
    it = counts.begin();
    while(it != counts.end()){
        float p_x = (float)it->second/elements;
        if (p_x>0) entropy-=p_x*log(p_x)/log(2);
        it++;
    }
    return entropy;
}
Run Code Online (Sandbox Code Playgroud)