- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我尝试运行下面的代码
import math
import random
from matplotlib import pyplot as plt
from collections import Counter
def bucketize(point,bucket_size):
return bucket_size * math.floor(point/bucket_size)
def make_histogram(points, bucket_size):
return Counter(bucketize(point,bucket_size) for point in points)
def plot_histogram(points,bucket_size,title=""):
histogram = make_histogram(points,bucket_size)
plt.bar(histogram.keys(),histogram.values(),width=bucket_size)
plt.title(title)
plt.show()
def inverse_normalCDF(p,miu=0,sigma=1,tolerance=0.00001):
if miu !=0 or sigma !=1:
return miu + sigma * inverse_normalCDF(p,tolerance=tolerance)
low_z,low_p = -10.0,0
hi_z,hi_p = 10.0,1
while hi_z - low_z > tolerance:
mid_z = (low_z + hi_z)/2
mid_p = normalCDF(mid_z)
if mid_p > p:
low_z,low_p = mid_z,mid_p
elif mid_p > p:
hi_z, hi_p = mid_z,mid_p
else:
break
return mid_z
def normalCDF(x,miu=0,sigma=1):
return (1+math.erf((x-miu)/math.sqrt(2)/sigma))/2
random.seed(0)
#uniform = [200*random.random()-100 for _ in range (10000)]
#plot_histogram(uniform,10,"uniform histogram")
normal = [57 * inverse_normalCDF(random.random() for _ in range(10000))]
plot_histogram(normal,10,"normal histogram")
但程序显示错误“RecursionError:比较时超出最大递归深度”,我应该如何修复 RecursionError,因为它会影响 inverse_normalCDF 函数?
错误不再显示函数中超出的最大递归深度,而是
Traceback (most recent call last):
File "C:\Users\asus\Documents\Sublime\dataScience\normalHistogram.py", line 41, in <module>
normal = [57 * inverse_normalCDF(random.random() for _ in range(10000))]
File "C:\Users\asus\Documents\Sublime\dataScience\normalHistogram.py", line 26, in inverse_normalCDF
if mid_p > p:
TypeError: '>' not supported between instances of 'float' and 'generator'
[Finished in 0.7s]
我使用的python版本是3.7,我使用的matplotlib版本是3.0.3
*更新,改变了
def inverse_normalCDF(p,miu=0,sigma=1,tolerance=0.00001):fix some typos in the code
最佳答案
你快到了。该错误可以通过重写您的列表理解来解决:
normal = [57 * inverse_normalCDF(random.random()) for _ in range(10000)]
我只是移动了一个右括号,以便函数 inverse_normalCDF 为每次迭代传递一个值,而不是整个生成器。
关于python 绘图 RecursionError : maximum recursion depth exceeded in comparison,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55876542/
当我收到此错误时,此递归阶乘计算器一直运行良好,直到输入 994:“RecursionError:比较时超出最大递归深度”。有人可以解释一下这是什么意思吗?递归的最大数量怎么可能?提前致谢。 def
我正在尝试使用递归函数迭代嵌套字典,但是我最终收到一条消息,上面写着 RecursionError: maximum recursion depth exceeded while calling a
我正在尝试编写一个由任意数量的 lambda 函数组成的函数。 我有两个简单的 lambda 函数。 f = lambda x: x + 1 g = lambda x: x**2 我对组合函数的尝试是
import random class cards(): def __init__(self,val,suit): self.val = val self.su
我在 Python 中编写了以下用于快速排序的代码,但出现RecursionError: maximum recursion depth exceeded in comparison。在运行另一个具有
所以我尝试使用 sys.getrecursionlimit() 和 sys.setrecursionlimit() 方法。默认递归限制为 3000。 我尝试使用以下代码对其进行检查: def recu
我有一个 python 应用程序,它可以在 python 上完美运行,但在运行 cythonized 脚本时出现错误。 当我用 cython 编译脚本时,一切都编译正常,我在带有 Linux 的 Ra
代码如下所示。我只知道 RecursionError 的原因是 student.age 在 Descriptor 中以无限循环调用 __get__ 。但是,我既不知道根本原因,也不知道解决方案。 我该
简单的代码: from PyDAQmx import Task task = Task() 加薪: File "C:\Users\Adrian\Anaconda3\lib\site-packages
我一直在尝试使用 PyQt5 库开发一个多窗口应用程序。我想有一个专门用于单独功能的第二个窗口。我能够从主窗口切换到辅助窗口,没有任何问题。但是在尝试切换回来后,我得到了递归错误异常。这是相关的代码:
我使用以下代码来计算斐波那契数列的元素。代码按原样正常工作 def fib(n, lookup=dict()): if n == 0 or n == 1: lookup[n]
我正在使用谷歌colab。我已经做了很多次我想做的事情,所以我不知道为什么它现在不起作用。 我所做的就是制作一个包含大约 1000 个键的字典,每个值都是 4 个字符串的列表。我已经确认这本词典正是我
所以,我有一个函数,它的目的是为被逗号包围的单词着色。 def __init__(...something): ...something self.user_i
我正在使用 tkinter 学习,这涉及形状的流畅运动。这就是我现在陷入困境的地方。 我收到不一致的递归错误。该代码在 Windows 1O 上运行没有问题,但在我的 macOS 上它崩溃了。 此外,
我得到了以下代码来详细说明我的问题。我正在使用 python 3.6 和 pandas==0.25.3。 import pandas as pd from enum import Enum, IntE
出于单元测试的目的,我创建了一个类,其实例是一个可迭代的,它将产生特定的序列,然后引发异常: class Iter: def __init__(self, seq): self
我运行了一个用于 MNIST 分类的神经网络模型并收到了错误- RecursionError: maximum recursion depth exceeded 我检查了一些关于 stackoverf
我定义了一个名为 spam 的类: class spam(): def __str__(self): print(self) a = spam() print(a) 最后的打印
我为 Tornado RequestHandler 编写了一个辅助装饰器: def return_response(method): @wraps(method) async def
我尝试运行下面的代码 import math import random from matplotlib import pyplot as plt from collections import Co
我是一名优秀的程序员,十分优秀!