推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
stdout
V2EX  ›  Python

概率随机分布

  •  
  •   stdout · Dec 30, 2020 · 2224 views
    This topic created in 1983 days ago, the information mentioned may be changed or developed.

    np.random.choice()可以对固定数组设置概率随机。限制是固定的数组。 如果我要区间概率随机,比如 0-100 取数 0-10:1% 10-20:9% 20-30: 20% 30-80: 30% 80-99: 39.99% 100: 0.01% 有什么库能直接处理这个问题呢?先谢谢各位先。

    NaVient
        1
    NaVient  
       Dec 30, 2020   ❤️ 1
    提供一个思路,用真随机来做概率随机,按照比例扩充样本数量 :)
    NaVient
        2
    NaVient  
       Dec 30, 2020   ❤️ 1
    然后附上代码
    """
    p = np.array([0.2,0.4,0.3,0.1])
    print(np.random.choice([0,1,2,3], p = p.ravel()))
    """
    stdout
        3
    stdout  
    OP
       Dec 30, 2020
    @NaVient 这个是固定的数组随机,需要实现范围内概率随机。
    NaVient
        4
    NaVient  
       Dec 30, 2020   ❤️ 1
    @stdout #3 想法灵活一点,用两次随机,第一次随机拿到按概率随机的范围,第二次随机从范围中真随机一个对象
    stdout
        6
    stdout  
    OP
       Dec 30, 2020
    """
    import random
    n = 1000
    # 分布参数
    params = {
    (0, 10): 0.01,
    (10, 20): 0.1,
    (20, 50): 0.2,
    (50, 60): 0.68,
    (90, 100): 0.01,
    }
    p = np.array(list(params.values()))
    c = np.array(range(len(params)))
    x = np.arange(n)
    y = []
    for _ in range(n):
    _c = np.random.choice(c, p=p.ravel())
    _yc = list(params.keys())[_c]
    _y = random.randrange(*_yc)
    y.append(_y)

    plt.scatter(x, y)
    """
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3986 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 00:15 · PVG 08:15 · LAX 17:15 · JFK 20:15
    ♥ Do have faith in what you're doing.