连接两个一维NumPy数组

时间:2022-12-01 12:33:45

I have two simple one-dimensional arrays in NumPy. I should be able to concatenate them using numpy.concatenate. But I get this error for the code below:

我有两个简单的一维数组。我应该能够使用numpi .concatenate将它们连接起来。但是我得到下面代码的错误:

TypeError: only length-1 arrays can be converted to Python scalars

TypeError:只能将长度为1的数组转换为Python标量

Code

import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([5, 6])
numpy.concatenate(a, b)

Why?

为什么?

4 个解决方案

#1


219  

The line should be:

线应该是:

numpy.concatenate([a,b])

The arrays you want to concatenate need to passed in as a sequence, not as separate arguments.

需要连接的数组需要作为序列传入,而不是作为单独的参数传入。

From the NumPy documentation:

从NumPy文档:

numpy.concatenate((a1, a2, ...), axis=0)

numpy。连接((a1,a2,…),轴= 0)

Join a sequence of arrays together.

一起加入一系列阵列。

It was trying to interpret your b as the axis parameter, which is why it complained it couldn't convert it into a scalar.

它试图把b解释为轴参数,这就是为什么它抱怨不能把它转换成标量的原因。

#2


20  

The first parameter to concatenate should itself be a sequence of arrays to concatenate:

要连接的第一个参数本身应该是要连接的数组序列:

numpy.concatenate((a,b)) # Note the extra parentheses.

#3


7  

An alternative ist to use the short form of "concatenate" which is either "r_[...]" or "c_[...]" as shown in the example code beneath (see http://wiki.scipy.org/NumPy_for_Matlab_Users for additional information):

另一种方法是使用缩写形式“concatenate”,即“r_[…]”。]”或“c_(…]“如下面的示例代码所示(参见http://wiki.scipy.org/NumPy_for_Matlab_Users获取其他信息):

%pylab
vector_a = r_[0.:10.] #short form of "arange"
vector_b = array([1,1,1,1])
vector_c = r_[vector_a,vector_b]
print vector_a
print vector_b
print vector_c, '\n\n'

a = ones((3,4))*4
print a, '\n'
c = array([1,1,1])
b = c_[a,c]
print b, '\n\n'

a = ones((4,3))*4
print a, '\n'
c = array([[1,1,1]])
b = r_[a,c]
print b

print type(vector_b)

Which results in:

结果:

[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
[1 1 1 1]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.  1.  1.  1.  1.] 


[[ 4.  4.  4.  4.]
 [ 4.  4.  4.  4.]
 [ 4.  4.  4.  4.]] 

[[ 4.  4.  4.  4.  1.]
 [ 4.  4.  4.  4.  1.]
 [ 4.  4.  4.  4.  1.]] 


[[ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]] 

[[ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 1.  1.  1.]]

#4


7  

There are several possibilities for concatenating 1D arrays, e.g.,

连接1D数组有几种可能性,例如,

numpy.r_[a, a],
numpy.stack([a, a]).reshape(-1),
numpy.hstack([a, a]),
numpy.concatenate([a, a])

All those options are equally fast for large arrays; for small ones, concatenate has a slight edge:

对于大型数组,所有这些选项都同样快速;对于小的,连接有一个小的边缘:

连接两个一维NumPy数组

The plot was created with perfplot:

这个情节是用完美的情节创作的:

import numpy
import perfplot

perfplot.show(
    setup=lambda n: numpy.random.rand(n),
    kernels=[
        lambda a: numpy.r_[a, a],
        lambda a: numpy.stack([a, a]).reshape(-1),
        lambda a: numpy.hstack([a, a]),
        lambda a: numpy.concatenate([a, a])
        ],
    labels=['r_', 'stack+reshape', 'hstack', 'concatenate'],
    n_range=[2**k for k in range(19)],
    xlabel='len(a)',
    logx=True,
    logy=True,
    )

#1


219  

The line should be:

线应该是:

numpy.concatenate([a,b])

The arrays you want to concatenate need to passed in as a sequence, not as separate arguments.

需要连接的数组需要作为序列传入,而不是作为单独的参数传入。

From the NumPy documentation:

从NumPy文档:

numpy.concatenate((a1, a2, ...), axis=0)

numpy。连接((a1,a2,…),轴= 0)

Join a sequence of arrays together.

一起加入一系列阵列。

It was trying to interpret your b as the axis parameter, which is why it complained it couldn't convert it into a scalar.

它试图把b解释为轴参数,这就是为什么它抱怨不能把它转换成标量的原因。

#2


20  

The first parameter to concatenate should itself be a sequence of arrays to concatenate:

要连接的第一个参数本身应该是要连接的数组序列:

numpy.concatenate((a,b)) # Note the extra parentheses.

#3


7  

An alternative ist to use the short form of "concatenate" which is either "r_[...]" or "c_[...]" as shown in the example code beneath (see http://wiki.scipy.org/NumPy_for_Matlab_Users for additional information):

另一种方法是使用缩写形式“concatenate”,即“r_[…]”。]”或“c_(…]“如下面的示例代码所示(参见http://wiki.scipy.org/NumPy_for_Matlab_Users获取其他信息):

%pylab
vector_a = r_[0.:10.] #short form of "arange"
vector_b = array([1,1,1,1])
vector_c = r_[vector_a,vector_b]
print vector_a
print vector_b
print vector_c, '\n\n'

a = ones((3,4))*4
print a, '\n'
c = array([1,1,1])
b = c_[a,c]
print b, '\n\n'

a = ones((4,3))*4
print a, '\n'
c = array([[1,1,1]])
b = r_[a,c]
print b

print type(vector_b)

Which results in:

结果:

[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
[1 1 1 1]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.  1.  1.  1.  1.] 


[[ 4.  4.  4.  4.]
 [ 4.  4.  4.  4.]
 [ 4.  4.  4.  4.]] 

[[ 4.  4.  4.  4.  1.]
 [ 4.  4.  4.  4.  1.]
 [ 4.  4.  4.  4.  1.]] 


[[ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]] 

[[ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 4.  4.  4.]
 [ 1.  1.  1.]]

#4


7  

There are several possibilities for concatenating 1D arrays, e.g.,

连接1D数组有几种可能性,例如,

numpy.r_[a, a],
numpy.stack([a, a]).reshape(-1),
numpy.hstack([a, a]),
numpy.concatenate([a, a])

All those options are equally fast for large arrays; for small ones, concatenate has a slight edge:

对于大型数组,所有这些选项都同样快速;对于小的,连接有一个小的边缘:

连接两个一维NumPy数组

The plot was created with perfplot:

这个情节是用完美的情节创作的:

import numpy
import perfplot

perfplot.show(
    setup=lambda n: numpy.random.rand(n),
    kernels=[
        lambda a: numpy.r_[a, a],
        lambda a: numpy.stack([a, a]).reshape(-1),
        lambda a: numpy.hstack([a, a]),
        lambda a: numpy.concatenate([a, a])
        ],
    labels=['r_', 'stack+reshape', 'hstack', 'concatenate'],
    n_range=[2**k for k in range(19)],
    xlabel='len(a)',
    logx=True,
    logy=True,
    )