代码之家  ›  专栏  ›  技术社区  ›  User12547645

为什么我的随机变量代码会抛出类型错误?

  •  0
  • User12547645  · 技术社区  · 7 年前

    def uniform_generator(a, b):
        while True:
            b = uniform(loc=a, scale=b)
            yield b.rvs(1)
    
    num_gen = uniform_generator(1, 5)
        numbers = [float(next(num_gen)[0]) for _ in range(1000)]
    

    据我所知,我所做的就是向空数组添加1000个浮点。问题是什么?我用的是python 3

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-133-42a9353c6a00> in <module>()
          4     num_gen = uniform_generator(1, 5)
          5     print(type(float(next(num_gen)[0])))
    ----> 6     numbers = [float(next(num_gen)[0]) for _ in range(1000)]
          7 
          8     assert([1 <= num and num <= 5 for num in numbers])
    
    <ipython-input-133-42a9353c6a00> in <listcomp>(.0)
          4     num_gen = uniform_generator(1, 5)
          5     print(type(float(next(num_gen)[0])))
    ----> 6     numbers = [float(next(num_gen)[0]) for _ in range(1000)]
          7 
          8     assert([1 <= num and num <= 5 for num in numbers])
    
    <ipython-input-107-8956bb825458> in uniform_generator(a, b)
          2     while True:
          3         b = uniform(loc=a, scale=b)
    ----> 4         yield b.rvs(1)
          5 
          6 def normal_generator(mean, std):
    
    c:\users\user\anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py in rvs(self, size, random_state)
        468         kwds = self.kwds.copy()
        469         kwds.update({'size': size, 'random_state': random_state})
    --> 470         return self.dist.rvs(*self.args, **kwds)
        471 
        472     def sf(self, x):
    
    c:\users\user\anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py in rvs(self, *args, **kwds)
        936         rndm = kwds.pop('random_state', None)
        937         args, loc, scale, size = self._parse_args_rvs(*args, **kwds)
    --> 938         cond = logical_and(self._argcheck(*args), (scale >= 0))
        939         if not np.all(cond):
        940             raise ValueError("Domain error in arguments.")
    
    TypeError: '>=' not supported between instances of 'rv_frozen' and 'int'
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   hootnot    7 年前

    它不应该是发电机:

    def uniform_generator(a, b):
        # you want to rename it now, since it isn't a generator anymore
        #while True:
            b = uniform(loc=a, scale=b)
            #yield b.rvs(1)
            return b.rvs(1)
    
    #num_gen = uniform_generator(1, 5)
    # numbers = [float(next(num_gen)[0]) for _ in range(1000)]
    numbers = [uniform_generator(1,5) for _ in range(1000)]
    

    def uniform_generator(a, b, wanted):
        for _ in range(wanted):
            b = uniform(loc=a, scale=b)
            yield b.rvs(1)
    
    numbers = [n for n in uniform_generator(1,5, 1000)]
    
        2
  •  1
  •   Diana    7 年前

    def uniform_generator(a, b):
    while True:
        c = uniform(loc=a, scale=b)
        yield c.rvs(1)