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
z0z
V2EX  ›  Python

在 Python 中,矩阵或向量可以在中括号中判断的吗?

  •  
  •   z0z · 2018-08-21 09:56:35 +08:00 · 2255 次点击
    这是一个创建于 2047 天前的主题,其中的信息可能已经有所发展或是发生改变。

    preds 的原值是

    preds= [[0.46720526]

    [0.99414194]

    [0.26025357]

    [0.99096197]

    [0.99137177]]

    经过下面的操作之后

    preds[preds <= 0.5] = 0
    
    preds[preds > 0] = 1
    

    就把小于 0.5 的设置为 0,大于 0 的设置为 1 了

    [[0.]

    [1.]

    [0.]

    [1.]

    [1.]]

    请问这样的神操作学名叫什么呢?是什么原理呢?

    5 条回复    2018-08-21 10:34:27 +08:00
    ipwx
        1
    ipwx  
       2018-08-21 10:02:03 +08:00
    preds 的原值八成不是 list of list,是 numpy.array([[..]])。

    numpy.array 重写了 __getitem__。
    ipwx
        2
    ipwx  
       2018-08-21 10:02:23 +08:00
    重写了 __getitem__ 和 __setitem__
    princelai
        3
    princelai  
       2018-08-21 10:08:01 +08:00
    justou
        4
    justou  
       2018-08-21 10:15:29 +08:00
    numpy 里面叫 fancy indexing, 翻译成花式索引
    z0z
        5
    z0z  
    OP
       2018-08-21 10:34:27 +08:00
    @ipwx
    @princelai
    @justou 仰慕三位大神,表示感谢,我去学习学习
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5843 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:59 · PVG 09:59 · LAX 18:59 · JFK 21:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.