均匀性代码问题绘图的随机数生成器测试

Random Number Generator Test of Uniformity Code problem plotting

请告诉我我的代码有什么问题。谢谢你。

它在 运行 时给我这个错误:

Traceback (most recent call last):

    plt.plot(N, Test_Uniform(N))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2813, in plot
    is not None else {}), **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/__init__.py", line 1810, in inner
    return func(ax, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 1611, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 393, in _grab_next_args
    yield from self._plot_args(this, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 370, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 231, in _xy_from_xy
    "have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (1000,) and (3,)

它需要这样做:

       import matplotlib.pyplot as plt
       import numpy as np

       N=np.random.rand(1000)
       k_array=np.array([1,3,7]) 

       def Test_Uniform(N):
          test_uni=np.array([])  
            for w in k_array:
              test_uni_random=0
                  for i in N:
                    test_uni_random += (i**w)/(len(N))
                  test_uni=np.append(test_uni,test_uni_random)
          return test_uni

      def Test_uniform_Deviation(N):
          new_sum=np.array([])
            for z in k_array:
              test_uni_rand=0
                 for q in N:
                   test_uni_rand += (((q**z)/len(N))-(1/(1+q)))
                   new_sum=np.append(new_sum,test_uni_rand)
                 mean_sum=new_sum/len(N)
         return mean_sum

     plt.plot(N, Test_Uniform(N))
     plt.xlabel('N')
     plt.xscale('log')
     plt.ylabel('series')
     plt.show() 

     plt.plot(N, Test_uniform_Deviation(N))
     plt.xlabel('N')
     plt.xscale('log')
     plt.ylabel('series')
     plt.show()

对于每个 k,绘制找到的 r^k 的平均值与 log(N) 沿着预期限制 1/(1+k) 的一条线。并绘制平均偏差(第二个函数)与 log(N)

问题是您要针对 Test_Uniform(N) 绘制 N(1000 个数字),其中 returns 三个数字的数组,每个元素对应 k_array:

test_uni=np.array([])

for w in k_array:
    test_uni_random = 0

    for i in N:
        test_uni_random += i**w / len(N)
    test_uni = np.append(test_uni, test_uni_random)

return test_uni

test_uni 数组仅包含 k_array 中每个元素的一个元素。如果我们要反转循环:

import matplotlib.pyplot as plt
import numpy as np

N = np.random.rand(1000)
k_array = np.array([1, 3, 7]) 

def Test_Uniform(N):
    test_uni = np.array([])  

    for i in N:
        test_uni_random = 0

        for w in k_array:
            test_uni_random += i ** w / len(N)

        test_uni = np.append(test_uni, test_uni_random)

    return test_uni

plt.plot(N, Test_Uniform(N))
plt.xlabel('N')
plt.xscale('log')
plt.ylabel('series')
plt.show() 

它可能没有任何数学意义,但至少 NTest_Uniform(N) 现在具有相同数量的元素并且可以绘制: