将1D数组转换为numpy中的2D数组

时间:2021-09-28 21:58:20

I want to convert a 1-dimensional array into a matrix by specifying the number of columns in the matrix. Something that would work like this:

我想通过指定矩阵中的列数将一维数组转换成矩阵。可以这样工作:

> import numpy as np
> A = np.array([1,2,3,4,5,6])
> B = vec2matrix(A,ncol=2)
> B
array([[1, 2],
   [3, 4],
   [5, 6],
   [7, 8]])

Does numpy have a function that works like my made-up function "vec2matrix"? (I understand that you can index a 1D array like a matrix, but that isn't an option in the code I have - I need to make this conversion)

numpy是否有一个像我所编的函数“vec2matrix”那样的函数?(我知道您可以将一个一维数组索引为一个矩阵,但是在我所拥有的代码中这不是一个选项—我需要进行这个转换)

3 个解决方案

#1


73  

You want to reshape the array.

你想要重塑数组。

B = np.reshape(A, (-1, 2))

#2


28  

You have two options:

你有两个选择:

  • If you no longer want the original shape, the easiest is just to assign a new shape to the array

    如果您不再需要原始的形状,最简单的方法就是为数组分配一个新的形状

    a.shape = (a.size//ncols, ncols)
    

    You can switch the a.size//ncols by -1 to compute the proper shape automatically. Make sure that a.shape[0]*a.shape[1]=a.size, else you'll run into some problem.

    你可以切换a。大小// /ncols乘以-1,自动计算合适的形状。确保a.shape[0]* a.shape[1]=。尺寸,否则你会遇到问题的。

  • You can get a new array with the np.reshape function, that works mostly like the version presented above

    你可以用np得到一个新的数组。重塑功能,它的工作原理与上面的版本差不多

    new = np.reshape(a, (-1, ncols))
    

    When it's possible, new will be just a view of the initial array a, meaning that the data are shared. In some cases, though, new array will be acopy instead. Note that np.reshape also accepts an optional keyword order that lets you switch from row-major C order to column-major Fortran order. np.reshape is the function version of the a.reshape method.

    如果可能,new将仅仅是初始数组a的视图,这意味着数据是共享的。不过,在某些情况下,新的数组将是可复制的。注意,np。transform还接受一个可选的关键字顺序,允许您从行主C顺序切换到行主Fortran顺序。np。重塑是a的功能版本。重塑的方法。

If you can't respect the requirement a.shape[0]*a.shape[1]=a.size, you're stuck with having to create a new array. You can use the np.resize function and mixing it with np.reshape, such as

如果你不能满足这个要求。大小,您不得不创建一个新的数组。你可以用np。调整函数的大小并与np混合。重塑,如

>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)

#3


3  

Try something like:

尝试:

B = np.reshape(A,(-1,ncols))

You'll need to make sure that you can divide the number of elements in your array by ncols though. You can also play with the order in which the numbers are pulled into B using the order keyword.

您需要确保您可以将数组中的元素数除以ncols。您还可以使用order关键字来处理将数字拖入B的顺序。

#1


73  

You want to reshape the array.

你想要重塑数组。

B = np.reshape(A, (-1, 2))

#2


28  

You have two options:

你有两个选择:

  • If you no longer want the original shape, the easiest is just to assign a new shape to the array

    如果您不再需要原始的形状,最简单的方法就是为数组分配一个新的形状

    a.shape = (a.size//ncols, ncols)
    

    You can switch the a.size//ncols by -1 to compute the proper shape automatically. Make sure that a.shape[0]*a.shape[1]=a.size, else you'll run into some problem.

    你可以切换a。大小// /ncols乘以-1,自动计算合适的形状。确保a.shape[0]* a.shape[1]=。尺寸,否则你会遇到问题的。

  • You can get a new array with the np.reshape function, that works mostly like the version presented above

    你可以用np得到一个新的数组。重塑功能,它的工作原理与上面的版本差不多

    new = np.reshape(a, (-1, ncols))
    

    When it's possible, new will be just a view of the initial array a, meaning that the data are shared. In some cases, though, new array will be acopy instead. Note that np.reshape also accepts an optional keyword order that lets you switch from row-major C order to column-major Fortran order. np.reshape is the function version of the a.reshape method.

    如果可能,new将仅仅是初始数组a的视图,这意味着数据是共享的。不过,在某些情况下,新的数组将是可复制的。注意,np。transform还接受一个可选的关键字顺序,允许您从行主C顺序切换到行主Fortran顺序。np。重塑是a的功能版本。重塑的方法。

If you can't respect the requirement a.shape[0]*a.shape[1]=a.size, you're stuck with having to create a new array. You can use the np.resize function and mixing it with np.reshape, such as

如果你不能满足这个要求。大小,您不得不创建一个新的数组。你可以用np。调整函数的大小并与np混合。重塑,如

>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)

#3


3  

Try something like:

尝试:

B = np.reshape(A,(-1,ncols))

You'll need to make sure that you can divide the number of elements in your array by ncols though. You can also play with the order in which the numbers are pulled into B using the order keyword.

您需要确保您可以将数组中的元素数除以ncols。您还可以使用order关键字来处理将数字拖入B的顺序。