Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 鼠标运动熵_C# - Fatal编程技术网

C# 鼠标运动熵

C# 鼠标运动熵,c#,C#,我试图计算鼠标运动的熵。我不是一个C程序员,但我试图把C++代码移植到它里面,所以我可能把一些事情搞得超出了实际的数学(我不理解,因此我使用了在线资源) 我为每个位置(x和y)创建一个排序字典: 然后在循环结束时,我尝试计算每个循环的熵,如下所示: double entropyx = 0.0; foreach(KeyValuePair<int, int> entry in freqx) { double currfreq = Convert.T

我试图计算鼠标运动的熵。我不是一个C程序员,但我试图把C++代码移植到它里面,所以我可能把一些事情搞得超出了实际的数学(我不理解,因此我使用了在线资源)

我为每个位置(x和y)创建一个排序字典:

然后在循环结束时,我尝试计算每个循环的熵,如下所示:

    double entropyx = 0.0;
    foreach(KeyValuePair<int, int> entry in freqx)
    {
        double currfreq = Convert.ToDouble(entry.value) / 1;
        entropyx += currfreq * Log2(currfreq);
    }
    entropyx *= 1;

    double entropyy = 0.0;
    foreach (KeyValuePair<int, int> entry in freqy)
    {
        double currfreq = Convert.ToDouble(entry.Value) / 1;
        entropyy += currfreq * Log2(currfreq);
    }
    entropyy *= 1;
编辑:

试用代码:

public void GetMouseEntropy()
        {
            RichTextBox1.Text += "Started..." + Environment.NewLine;

            SortedDictionary<int, int> freqx = new SortedDictionary<int, int>();
            SortedDictionary<int, int> freqy = new SortedDictionary<int, int>();

            int curr_x = 0;
            int curr_y = 0;

            Stopwatch sw = new Stopwatch();
            sw.Start();

            while(sw.ElapsedMilliseconds < 2000)
            {
                curr_x = Cursor.Position.X;
                curr_y = Cursor.Position.Y;

                if (freqx.ContainsKey(curr_x))
                {
                    freqx[curr_x] += 1;
                }
                else
                {
                    freqx.Add(curr_x, 1);
                }

                if (freqy.ContainsKey(curr_y))
                {
                    freqy[curr_y] += 1;
                }
                else
                {
                    freqy.Add(curr_y, 1);
                }

                Application.DoEvents();
            }

            sw.Stop();

            double entropyx = 0.0;
            foreach (KeyValuePair<int, int> entry in freqx)
            {
                double currfreq = Convert.ToDouble(entry.Value) / 1;
                entropyx += currfreq * Log2(currfreq);
            }
            entropyx *= 1;

            double entropyy = 0.0;
            foreach (KeyValuePair<int, int> entry in freqy)
            {
                double currfreq = Convert.ToDouble(entry.Value) / 1;
                entropyy += currfreq * Log2(currfreq);
            }
            entropyy *= 1;

            RichTextBox1.Text += "X: " + entropyx + ", Y: " + entropyy;
        }
public void GetMouseEntropy()
{
RichTextBox1.Text+=“已启动…”+Environment.NewLine;
SortedDictionary Frexx=新的SortedDictionary();
SortedDictionary Frequey=新的SortedDictionary();
int curr_x=0;
int curr_y=0;
秒表sw=新秒表();
sw.Start();
而(西南ElapsedMilliseconds<2000)
{
curr_x=Cursor.Position.x;
curr_y=Cursor.Position.y;
if(频率容器(电流)
{
freqx[curr_x]+=1;
}
其他的
{
增加频率(电流,1);
}
if(频率容器(当前))
{
频率[curr_y]+=1;
}
其他的
{
增加频率(当前,1);
}
Application.DoEvents();
}
sw.Stop();
双entropyx=0.0;
foreach(freqx中的KeyValuePair条目)
{
double currfreq=Convert.ToDouble(entry.Value)/1;
entropyx+=currfreq*Log2(currfreq);
}
entropyx*=1;
双熵yy=0.0;
foreach(频率中的KeyValuePair条目)
{
double currfreq=Convert.ToDouble(entry.Value)/1;
entropyy+=currfreq*Log2(currfreq);
}
entropyy*=1;
RichTextBox1.Text+=“X:+entropyx+”,Y:+entropyy;
}

香农熵的定义是(参见):

H=-和(p(i)*log2(p(i)))

式中,p(i)是某个结果i发生的概率,且总和覆盖所有可能的结果

由于您的频率集合包含发生次数,而不是概率,因此在汇总时,您必须按样本总数进行划分

此外,公式中缺少“-”号

double entropyx = 0.0;
foreach(KeyValuePair<int, int> entry in freqx)
{
    double currfreq = Convert.ToDouble(entry.value) / sampleCount;
    entropyx += currfreq * Log2(currfreq);
}
entropyx *= -1;

double entropyy = 0.0;
foreach (KeyValuePair<int, int> entry in freqy)
{
    double currfreq = Convert.ToDouble(entry.Value) / sampleCount;
    entropyy += currfreq * Log2(currfreq);
}
entropyy *= -1;

它可以被称为ShannonEntropy(freqx.Values)

好的,完成了,兄弟。现在这些值更糟糕了。你在while循环中收集了多少样本?我收集了几千个。我还用随机数测试了Shannon熵算法的多种变体,{4,4,4,45}返回了一个超过200的假设熵,所以我在那里肯定做错了什么。我不确定在“currfreq”部分我应该如何计算它。我用随机数测试了你的代码,没有发现任何偏差
entropyx=2
意味着您只有两个共线(相同的y值两次),所有其他y值都是唯一的。我一定误解了它的工作原理,该值不应该在0和8之间吗?
    double Log2(double Nr)
    {
        return Math.Log(Nr) / Math.Log(2);
    }
public void GetMouseEntropy()
        {
            RichTextBox1.Text += "Started..." + Environment.NewLine;

            SortedDictionary<int, int> freqx = new SortedDictionary<int, int>();
            SortedDictionary<int, int> freqy = new SortedDictionary<int, int>();

            int curr_x = 0;
            int curr_y = 0;

            Stopwatch sw = new Stopwatch();
            sw.Start();

            while(sw.ElapsedMilliseconds < 2000)
            {
                curr_x = Cursor.Position.X;
                curr_y = Cursor.Position.Y;

                if (freqx.ContainsKey(curr_x))
                {
                    freqx[curr_x] += 1;
                }
                else
                {
                    freqx.Add(curr_x, 1);
                }

                if (freqy.ContainsKey(curr_y))
                {
                    freqy[curr_y] += 1;
                }
                else
                {
                    freqy.Add(curr_y, 1);
                }

                Application.DoEvents();
            }

            sw.Stop();

            double entropyx = 0.0;
            foreach (KeyValuePair<int, int> entry in freqx)
            {
                double currfreq = Convert.ToDouble(entry.Value) / 1;
                entropyx += currfreq * Log2(currfreq);
            }
            entropyx *= 1;

            double entropyy = 0.0;
            foreach (KeyValuePair<int, int> entry in freqy)
            {
                double currfreq = Convert.ToDouble(entry.Value) / 1;
                entropyy += currfreq * Log2(currfreq);
            }
            entropyy *= 1;

            RichTextBox1.Text += "X: " + entropyx + ", Y: " + entropyy;
        }
double entropyx = 0.0;
foreach(KeyValuePair<int, int> entry in freqx)
{
    double currfreq = Convert.ToDouble(entry.value) / sampleCount;
    entropyx += currfreq * Log2(currfreq);
}
entropyx *= -1;

double entropyy = 0.0;
foreach (KeyValuePair<int, int> entry in freqy)
{
    double currfreq = Convert.ToDouble(entry.Value) / sampleCount;
    entropyy += currfreq * Log2(currfreq);
}
entropyy *= -1;
double ShannonEntropy(IReadOnlyCollection<int> frequencies)
{
    var samples = frequencies.Sum();

    double entropy = 0.0;
    foreach (var freq in frequencies)
    {
        if (freq > 0)
        {
            double prob = Convert.ToDouble(freq) / samples;
            entropy -= prob * Math.Log(prob) / Math.Log(2);
        }
    }
    return entropy;
}