Numpy:将1D数组作为2D数组而不重新形成

时间:2022-12-29 18:39:57

I have need for hstacking multple arrays with with the same number of rows (although the number of rows is variable between uses) but different number of columns. However some of the arrays only have one column, eg.

我需要hstacking具有相同行数的多个数组(尽管行数在使用之间是可变的)但是列数不同。然而,一些阵列仅具有一列,例如。

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

which gives

#array.shape = (5,)

but I'd like to have the shape recognized as a 2d array, eg.

但我想将形状识别为二维数组,例如。

#array.shape = (5,1)

So that hstack can actually combine them. My current solution is:

所以hstack实际上可以将它们组合起来。我目前的解决方案是:

array = np.atleast_2d([1,2,3,4,5]).T
#array.shape = (5,1)

So I was wondering, is there a better way to do this? Would

所以我想知道,有更好的方法吗?将

array = np.array([1,2,3,4,5]).reshape(len([1,2,3,4,5]), 1)

be better? Note that my use of [1,2,3,4,5] is just a toy list to make the example concrete. In practice it will be a much larger list passed into a function as an argument. Thanks!

会更好?请注意,我使用[1,2,3,4,5]只是一个玩具列表,以使示例具体。在实践中,它将是一个更大的列表,作为参数传递给函数。谢谢!

3 个解决方案

#1


5  

Check the code of hstack and vstack. One, or both of those, pass the arguments through atleast_nd. That is a perfectly acceptable way of reshaping an array.

检查hstack和vstack的代码。其中一个或两个都通过atleast_nd传递参数。这是重塑数组的完全可接受的方式。

Some other ways:

其他一些方法:

arr = np.array([1,2,3,4,5]).reshape(-1,1)  # saves the use of len()
arr = np.array([1,2,3,4,5])[:,None]  # adds a new dim at end
np.array([1,2,3],ndmin=2).T  # used by column_stack

hstack and vstack transform their inputs with:

hstack和vstack转换他们的输入:

arrs = [atleast_1d(_m) for _m in tup]
[atleast_2d(_m) for _m in tup]

test data:

a1=np.arange(2)
a2=np.arange(10).reshape(2,5)
a3=np.arange(8).reshape(2,4)

np.hstack([a1.reshape(-1,1),a2,a3])
np.hstack([a1[:,None],a2,a3])
np.column_stack([a1,a2,a3])

result:

array([[0, 0, 1, 2, 3, 4, 0, 1, 2, 3],
       [1, 5, 6, 7, 8, 9, 4, 5, 6, 7]])

If you don't know ahead of time which arrays are 1d, then column_stack is easiest to use. The others require a little function that tests for dimensionality before applying the reshaping.

如果您事先不知道哪个数组是1d,那么column_stack最容易使用。其他需要一些功能,在应用重塑之前测试维度。

Numpy: use reshape or newaxis to add dimensions

Numpy:使用reshape或newaxis添加尺寸

#2


1  

If I understand your intent correctly, you wish to convert an array of shape (N,) to an array of shape (N,1) so that you can apply np.hstack:

如果我理解你的意图正确,你希望将形状数组(N,)转换为形状数组(N,1),以便您可以应用np.hstack:

In [147]: np.hstack([np.atleast_2d([1,2,3,4,5]).T, np.atleast_2d([1,2,3,4,5]).T])
Out[147]: 
array([[1, 1],
       [2, 2],
       [3, 3],
       [4, 4],
       [5, 5]])

In that case, you could use avoid reshaping the arrays and use np.column_stack instead:

在这种情况下,您可以使用避免重新整形数组并使用np.column_stack代替:

In [151]: np.column_stack([[1,2,3,4,5], [1,2,3,4,5]])
Out[151]: 
array([[1, 1],
       [2, 2],
       [3, 3],
       [4, 4],
       [5, 5]])

#3


0  

Just to add info on hpaulj's answer. I was curious about how fast were the four methods described. The winner is the method adding a column at the end of the 1d array.

只是为了添加关于hpaulj答案的信息。我很好奇四种方法描述的速度有多快。获胜者是在1d数组末尾添加列的方法。

Here is what I ran:

这是我跑的:

import numpy as np
import timeit

v = [1,2,3,4,5]

print('atleast2d:',timeit.timeit(lambda:np.atleast_2d(v).T))
print('reshape:',timeit.timeit(lambda:np.array(v).reshape(-1,1)))  # saves the use of len()
print('v[:,None]:', timeit.timeit(lambda:np.array(v)[:,None]))  # adds a new dim at end
print('np.array(v,ndmin=2).T:', timeit.timeit(lambda:np.array(v,ndmin=2).T))  # used by column_stack

And the results:

结果如下:

atleast2d: 4.455070924214851
reshape: 2.0535152913971615
v[:,None]: 1.8387219828073285
np.array(v,ndmin=2).T: 3.1735243063353664

#1


5  

Check the code of hstack and vstack. One, or both of those, pass the arguments through atleast_nd. That is a perfectly acceptable way of reshaping an array.

检查hstack和vstack的代码。其中一个或两个都通过atleast_nd传递参数。这是重塑数组的完全可接受的方式。

Some other ways:

其他一些方法:

arr = np.array([1,2,3,4,5]).reshape(-1,1)  # saves the use of len()
arr = np.array([1,2,3,4,5])[:,None]  # adds a new dim at end
np.array([1,2,3],ndmin=2).T  # used by column_stack

hstack and vstack transform their inputs with:

hstack和vstack转换他们的输入:

arrs = [atleast_1d(_m) for _m in tup]
[atleast_2d(_m) for _m in tup]

test data:

a1=np.arange(2)
a2=np.arange(10).reshape(2,5)
a3=np.arange(8).reshape(2,4)

np.hstack([a1.reshape(-1,1),a2,a3])
np.hstack([a1[:,None],a2,a3])
np.column_stack([a1,a2,a3])

result:

array([[0, 0, 1, 2, 3, 4, 0, 1, 2, 3],
       [1, 5, 6, 7, 8, 9, 4, 5, 6, 7]])

If you don't know ahead of time which arrays are 1d, then column_stack is easiest to use. The others require a little function that tests for dimensionality before applying the reshaping.

如果您事先不知道哪个数组是1d,那么column_stack最容易使用。其他需要一些功能,在应用重塑之前测试维度。

Numpy: use reshape or newaxis to add dimensions

Numpy:使用reshape或newaxis添加尺寸

#2


1  

If I understand your intent correctly, you wish to convert an array of shape (N,) to an array of shape (N,1) so that you can apply np.hstack:

如果我理解你的意图正确,你希望将形状数组(N,)转换为形状数组(N,1),以便您可以应用np.hstack:

In [147]: np.hstack([np.atleast_2d([1,2,3,4,5]).T, np.atleast_2d([1,2,3,4,5]).T])
Out[147]: 
array([[1, 1],
       [2, 2],
       [3, 3],
       [4, 4],
       [5, 5]])

In that case, you could use avoid reshaping the arrays and use np.column_stack instead:

在这种情况下,您可以使用避免重新整形数组并使用np.column_stack代替:

In [151]: np.column_stack([[1,2,3,4,5], [1,2,3,4,5]])
Out[151]: 
array([[1, 1],
       [2, 2],
       [3, 3],
       [4, 4],
       [5, 5]])

#3


0  

Just to add info on hpaulj's answer. I was curious about how fast were the four methods described. The winner is the method adding a column at the end of the 1d array.

只是为了添加关于hpaulj答案的信息。我很好奇四种方法描述的速度有多快。获胜者是在1d数组末尾添加列的方法。

Here is what I ran:

这是我跑的:

import numpy as np
import timeit

v = [1,2,3,4,5]

print('atleast2d:',timeit.timeit(lambda:np.atleast_2d(v).T))
print('reshape:',timeit.timeit(lambda:np.array(v).reshape(-1,1)))  # saves the use of len()
print('v[:,None]:', timeit.timeit(lambda:np.array(v)[:,None]))  # adds a new dim at end
print('np.array(v,ndmin=2).T:', timeit.timeit(lambda:np.array(v,ndmin=2).T))  # used by column_stack

And the results:

结果如下:

atleast2d: 4.455070924214851
reshape: 2.0535152913971615
v[:,None]: 1.8387219828073285
np.array(v,ndmin=2).T: 3.1735243063353664