请告诉我我的代码有什么问题。谢谢。
它在运行时给我这个错误:
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)
的关系。
答案 0 :(得分:0)
问题是您要针对data['A'] = data['A'].mask(data['A'].str.contains(r'\d'), '')
data['A'] = np.where(data['A'].str.contains(r'\d'), '', data['A'])
绘制N(1000个数字),这将返回一个由三个数字组成的数组,其中每个数字对应Test_Uniform(N)
的每个元素:
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()
和N
现在具有相同数量的元素并且可以绘制: