V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-08-13 16:19:12 +08:00 · 1380 次点击
    这是一个创建于 1690 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想生成一串随机矩阵,用了 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 条回复    2019-08-13 21:16:54 +08:00
    cherbim
        1
    cherbim  
       2019-08-13 20:56:52 +08:00
    not enough arguments for format string
    这个圈起来,期末考试要考,话说你知道这个报错啥意思么
    cherbim
        2
    cherbim  
       2019-08-13 21:02:45 +08:00
    最后一行你要传递三个参数,虽然三个参数相同,那也不能省略
    necomancer
        3
    necomancer  
       2019-08-13 21:16:54 +08:00
    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)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4431 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 10:04 · PVG 18:04 · LAX 03:04 · JFK 06:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.