推荐学习书目
› 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
love642285
V2EX  ›  Python

遇到一个对应计算的问题

  •  
  •   love642285 · Aug 13, 2019 · 2089 views
    This topic created in 2600 days ago, the information mentioned may be changed or developed.
    我想生成一串随机矩阵,用了 for 循环生成了 s0,s1,...sN 和 e0,e1,...,eN
    然后想对其进行计算,使得 bi=si*ei,但是用 for 循环写不出来
    B = np.random.randint(0,q,(m,n))
    for i in range(0,N):
    exec("s%s=np.random.randint(0,q,(n,1))"%i)
    for i in range(0,N):
    exec("e%s=np.random.randint(0,q,(m,1))"%i)
    for i in range(0,N):
    exec("b%s=np.dot(B,s%s)+e%s"%i)

    我已经生成了 s0 到 sN 和 e0 到 eN 了,也可以 print,但是调用他们计算 bi 的时候会报错
    exec("b%s=np.dot(B,s%s)+e%s"%i)
    TypeError: not enough arguments for format string
    3 replies  •  2019-08-13 21:16:54 +08:00
    cherbim
        1
    cherbim  
       Aug 13, 2019
    not enough arguments for format string
    这个圈起来,期末考试要考,话说你知道这个报错啥意思么
    cherbim
        2
    cherbim  
       Aug 13, 2019
    最后一行你要传递三个参数,虽然三个参数相同,那也不能省略
    necomancer
        3
    necomancer  
       Aug 13, 2019
    B = np.random.randint(0,q,(m, n))
    s = np.random.randint(0,q,(N, n))
    e = np.random.randint(0,q,(N, m))
    b = np.einsum('ij,...j->...i', B, s) + e

    b -> (N, m)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2603 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 14:59 · PVG 22:59 · LAX 07:59 · JFK 10:59
    ♥ Do have faith in what you're doing.