【问题标题】:Function numpy.reshape函数 numpy.reshape
【发布时间】:2017-10-07 10:10:06
【问题描述】:

我在matlab中有这个功能

cn = reshape (repmat (sn, n_rep, 1), 1, []);

没有带关键码的python:

import numpy like np
from numpy.random import randint

M = 2
N = 2 * 10 ** 8 ### data value
n_rep = 3 ## number of repetitions
sn = randint (0, M, size = N) ### integers 0 and 1
print ("sn =", sn)
cn_repmat = np.tile (sn, n_rep)
print ("cn_repmat =", cn_repmat)
cn = np.reshape (cn_repmat, 1, [])
print (cn)

我不确定是否不知道复古血缘关系

File "C: / Users / Sergio Malhao / .spyder-py3 / Desktop / untitled6.py", line 17, under <module>
cn = np.reshape (cn_repmat, 1, [])

File "E: \ Anaconda3 \ lib \ site-packages \ numpy \ core \ fromnumeric.py", line 232, in reshape
return _wrapfunc (a, 'reshape', newshape, order = order)

File "E: \ Anaconda3 \ lib \ site-packages \ numpy \ core \ fromnumeric.py", line 57, in _wrapfunc
return getattr (obj, method) (* args, ** kwds)

ValueError: Can not reshape the array of size 600000000 in shape (1,)

【问题讨论】:

    标签: python numpy reshape


    【解决方案1】:

    Numpy 不应该是 1:1 matlab。它的工作方式相似,但方式不同。 我假设您想将矩阵转换为一维数组。

    尝试:

    np.reshape (cn_repmat, (1, -1))
    

    其中 (1, -1) 是定义新数组大小的元组。

    一个形状维度可以是-1。在这种情况下,推断值 从数组的长度和剩余的维度。

    【讨论】:

    • 我得到了这个:sn [0 1 0 ..., 1 1 1], cn_repmat [[0 1 0 ..., 1 1 1]], cn= [[0 1 0 。 .., 1 1 1]] Mikaelblomkvissson
    • 恭喜。它是正确的还是什么?如果你想让它完全是一维的,写: cn = np.squeeze(np.reshape (cn_repmat, (1, -1)))
    【解决方案2】:

    八度:

    >> sn = [0,1,2,3,4]
    sn =
       0   1   2   3   4
    >> repmat(sn,4,1)
    ans =
       0   1   2   3   4
       0   1   2   3   4
       0   1   2   3   4
       0   1   2   3   4
    >> reshape(repmat(sn,4,1),1,[])
    ans =
       0   0   0   0   1   1   1   1   2   2   2   2   3   3   3   3   4   4   4   4
    

    numpy:

    In [595]: sn=np.array([0,1,2,3,4])
    In [596]: np.repeat(sn,4)
    Out[596]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
    In [597]: np.tile(sn,4)
    Out[597]: array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
    

    在 MATLAB 中,矩阵至少为 2d;在 numpy 中,它们可能是 1d。 Out[596] 是 1 天。

    我们可以通过使sn 2d 更接近 MATLAB:

    In [608]: sn2 = sn[None,:]    # = sn.reshape((1,-1))
    In [609]: sn2
    Out[609]: array([[0, 1, 2, 3, 4]])
    In [610]: np.repeat(sn2,4,1)
    Out[610]: array([[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]])
    

    使用tile,我们必须转置或玩顺序游戏(MATLAB 是 F 阶):

    In [613]: np.tile(sn,[4,1])
    Out[613]: 
    array([[0, 1, 2, 3, 4],
           [0, 1, 2, 3, 4],
           [0, 1, 2, 3, 4],
           [0, 1, 2, 3, 4]])
    In [614]: np.tile(sn,[4,1]).T.ravel()
    Out[614]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
    In [615]: np.tile(sn,[4,1]).ravel(order='F')
    Out[615]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
    

    ravel 等同于reshape(...., -1)-1 类似 [] 在 MATLAB 中重塑时的函数。

    numpyrepeat是基本功能; tile 使用具有不同用户界面的repeat(更像repmat)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 2018-02-08
      • 2017-06-10
      相关资源
      最近更新 更多