V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
oldbird
V2EX  ›  Python

关于 numpy 的 reshape,请教

  •  
  •   oldbird · May 16, 2020 · 2907 views
    This topic created in 2182 days ago, the information mentioned may be changed or developed.

    a=[1,2,3,4,5,6] 怎样可以读取为这样的 numpy 数组: array([[1, 3, 5], [2, 4, 6]]) ?

    10 replies    2020-05-17 22:24:41 +08:00
    Merlini
        1
    Merlini  
       May 16, 2020
    np.array(a).reshape(2,3)
    有空多看文档
    oldbird
        2
    oldbird  
    OP
       May 16, 2020
    @Merlini 这样的结果是

    [[1 2 3]
    [4 5 6]]

    和想要的结果不一样。
    Merlini
        3
    Merlini  
       May 16, 2020
    @oldbird #2 噢噢 不好意思看错了 这样的话就更简单了 不用 numpy
    a[::2] 和 a[1::2]就行, 前者是[1,3,5],后者是[2,4,6]
    oldbird
        4
    oldbird  
    OP
       May 16, 2020
    @Merlini 谢谢,用 numpy 有方法吗? 实际情况是将图像读取为 numpy 数组类型,然后按第一列、第二列……的顺序将所有像素读取并处理,结果为一维数组,想还原成原影像的尺寸。
    yiningyang1228
        5
    yiningyang1228  
       May 16, 2020
    接楼上,然后再 stack 起来就行了,np.vstack((a[::2], a[1::2]))
    Xs0ul
        6
    Xs0ul  
       May 16, 2020
    np.array(a).reshape(3,2).T
    Merlini
        7
    Merlini  
       May 16, 2020
    @oldbird #4
    那我们假设是个二维数组 test = numpy.array([[1, 2], [3, 4], [5, 6]])

    >>> test[:,0]
    array([1, 3, 5])

    这样就读取了第一列的值。

    这是你需要的吗?
    c0xt30a
        8
    c0xt30a  
       May 16, 2020
    transpose 一下就可以了

    ```
    np.array( a ).reshape( (3, 2) ).T
    ```
    necomancer
        9
    necomancer  
       May 16, 2020
    np.arange(1,7).reshape(3,2).T
    dongxiao
        10
    dongxiao  
       May 17, 2020
    np.array(a).reshape((2, 3), order="F")
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3069 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 15:08 · PVG 23:08 · LAX 08:08 · JFK 11:08
    ♥ Do have faith in what you're doing.