如何将2d numpy数组制作成3d数组?

时间:2021-12-09 14:58:54

I have a 2d array with shape (x, y) which I want to convert to a 3d array with shape (x, y, 1). Is there a nice Pythonic way to do this?

我有一个带有形状(x,y)的二维数组,我想将其转换为具有形状(x,y,1)的3d数组。有没有一个很好的Pythonic方法来做到这一点?

5 个解决方案

#1


38  

In addition to the other answers, you can also use slicing with numpy.newaxis:

除了其他答案,您还可以使用numpy.newaxis切片:

>>> from numpy import zeros, newaxis
>>> a = zeros((6, 8))
>>> a.shape
(6, 8)
>>> b = a[:, :, newaxis]
>>> b.shape
(6, 8, 1)

Or even this (which will work with an arbitrary number of dimensions):

甚至这个(它将使用任意数量的维度):

>>> b = a[..., newaxis]
>>> b.shape
(6, 8, 1)

#2


8  

numpy.reshape(array, array.shape + (1,))

#3


2  

import numpy as np

a= np.eye(3)
print a.shape
b = a.reshape(3,3,1)
print b.shape

#4


0  

hope this funtion helps u to convert 2D array to 3D array.

希望这个功能可以帮助你将2D数组转换为3D数组。

Args:
  x: 2darray, (n_time, n_in)
  agg_num: int, number of frames to concatenate. 
  hop: int, number of hop frames. 

Returns:
  3darray, (n_blocks, agg_num, n_in)


def d_2d_to_3d(x, agg_num, hop):

    # Pad to at least one block. 
    len_x, n_in = x.shape
    if (len_x < agg_num): #not in get_matrix_data
        x = np.concatenate((x, np.zeros((agg_num - len_x, n_in))))

    # main 2d to 3d. 
    len_x = len(x)
    i1 = 0
    x3d = []
    while (i1 + agg_num <= len_x):
        x3d.append(x[i1 : i1 + agg_num])
        i1 += hop

    return np.array(x3d)

#5


0  

import numpy as np

导入numpy为np

create a 2D array

a = np.array([[1,2,3], [4,5,6], [1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6]])

a = np.array([[1,2,3],[4,5,6],[1,2,3],[4,5,6],[1,2,3],[4, 5,6],[1,2,3],[4,5,6]])

print(a.shape)

shape of a = (8,3)

b = np.reshape(a, (8, 3, -1))

b = np.reshape(a,(8,3,-1))

changing the shape, -1 means any number which is suitable

print(b.shape)

size of b = (8,3,1)

#1


38  

In addition to the other answers, you can also use slicing with numpy.newaxis:

除了其他答案,您还可以使用numpy.newaxis切片:

>>> from numpy import zeros, newaxis
>>> a = zeros((6, 8))
>>> a.shape
(6, 8)
>>> b = a[:, :, newaxis]
>>> b.shape
(6, 8, 1)

Or even this (which will work with an arbitrary number of dimensions):

甚至这个(它将使用任意数量的维度):

>>> b = a[..., newaxis]
>>> b.shape
(6, 8, 1)

#2


8  

numpy.reshape(array, array.shape + (1,))

#3


2  

import numpy as np

a= np.eye(3)
print a.shape
b = a.reshape(3,3,1)
print b.shape

#4


0  

hope this funtion helps u to convert 2D array to 3D array.

希望这个功能可以帮助你将2D数组转换为3D数组。

Args:
  x: 2darray, (n_time, n_in)
  agg_num: int, number of frames to concatenate. 
  hop: int, number of hop frames. 

Returns:
  3darray, (n_blocks, agg_num, n_in)


def d_2d_to_3d(x, agg_num, hop):

    # Pad to at least one block. 
    len_x, n_in = x.shape
    if (len_x < agg_num): #not in get_matrix_data
        x = np.concatenate((x, np.zeros((agg_num - len_x, n_in))))

    # main 2d to 3d. 
    len_x = len(x)
    i1 = 0
    x3d = []
    while (i1 + agg_num <= len_x):
        x3d.append(x[i1 : i1 + agg_num])
        i1 += hop

    return np.array(x3d)

#5


0  

import numpy as np

导入numpy为np

create a 2D array

a = np.array([[1,2,3], [4,5,6], [1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6]])

a = np.array([[1,2,3],[4,5,6],[1,2,3],[4,5,6],[1,2,3],[4, 5,6],[1,2,3],[4,5,6]])

print(a.shape)

shape of a = (8,3)

b = np.reshape(a, (8, 3, -1))

b = np.reshape(a,(8,3,-1))

changing the shape, -1 means any number which is suitable

print(b.shape)

size of b = (8,3,1)