【问题标题】:Combine multiple numpy arrays into one of different shapes将多个 numpy 数组组合成不同形状之一
【发布时间】:2020-08-23 12:23:26
【问题描述】:

所以我有四个不同形状的 numpy 数组:

(2580, 100)
(2580, 237)
(2580, 8)
(2580, 37)

如何将所有数组组合成一个 numpy 数组?

给我以下错误:

ValueError: could not broadcast input array from shape (2580,237) into shape (2580)

【问题讨论】:

    标签: python python-3.x numpy


    【解决方案1】:
    >>> import numpy as np
    >>> a = np.zeros((2580, 100))
    >>> b = np.zeros((2580, 237))
    >>> c = np.zeros((2580, 8))
    >>> d = np.zeros((2580, 37))
    >>> e = np.concatenate((a, b, c, d), axis=1)
    >>> e.shape
    (2580, 382)
    

    【讨论】:

      【解决方案2】:

      您可以使用np.c_ 沿轴连接

      import numpy as np
      f = np.zeros(shape=(5,4))
      s = np.zeros(shape=(5,6))
      t = np.zeros(shape=(5,16))
      res=np.c_[f,s,t]
      res.shape
      (5,26)
      

      【讨论】:

        【解决方案3】:

        只有我们 np.concatenate

        import numpy as np
        a = np.random.rand(2580, 100)
        b = np.random.rand(2580, 237)
        c = np.random.rand(2580, 8)
        d = np.random.rand(2580, 37)
        
        e = np.concatenate((a, b, c, d), axis = 1)
        print(e.shape) 
        # (2580, 382)
        

        【讨论】:

          猜你喜欢
          • 2020-04-04
          • 2014-02-15
          • 2020-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-10
          • 2023-03-21
          • 2014-11-12
          相关资源
          最近更新 更多