问题:连接两个一维NumPy数组
我在NumPy中有两个简单的一维数组。我应该能够使用numpy.concatenate将它们连接起来。但是我收到以下代码的错误:
TypeError:只有length-1数组可以转换为Python标量
码
import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([5, 6])
numpy.concatenate(a, b)
为什么?
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:
TypeError: only length-1 arrays can be converted to Python scalars
Code
import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([5, 6])
numpy.concatenate(a, b)
Why?
回答 0
该行应为:
numpy.concatenate([a,b])
要连接的数组需要作为一个序列而不是作为单独的参数传递。
从NumPy文档中:
numpy.concatenate((a1, a2, ...), axis=0)
将一系列数组连接在一起。
它试图将您解释b
为axis参数,这就是为什么它抱怨无法将其转换为标量。
The line should be:
numpy.concatenate([a,b])
The arrays you want to concatenate need to be passed in as a sequence, not as separate arguments.
From the NumPy documentation:
numpy.concatenate((a1, a2, ...), axis=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.
回答 1
连接一维数组有多种可能性,例如,
numpy.r_[a, a],
numpy.stack([a, a]).reshape(-1),
numpy.hstack([a, a]),
numpy.concatenate([a, a])
对于大型阵列,所有这些选项都同样快。对于小型的,concatenate
有一点优势:
该图是使用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)",
)
There are several possibilities for concatenating 1D arrays, e.g.,
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:
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)",
)
回答 2
的第一个参数concatenate
本身应该是要串联的数组序列:
numpy.concatenate((a,b)) # Note the extra parentheses.
The first parameter to concatenate
should itself be a sequence of arrays to concatenate:
numpy.concatenate((a,b)) # Note the extra parentheses.
回答 3
另一种方法是使用“ 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)
结果是:
[ 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.]]
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):
%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
以下是使用numpy.ravel()
,的更多方法:numpy.array()
利用一维数组可以解包为普通元素的事实:
# we'll utilize the concept of unpacking
In [15]: (*a, *b)
Out[15]: (1, 2, 3, 5, 6)
# using `numpy.ravel()`
In [14]: np.ravel((*a, *b))
Out[14]: array([1, 2, 3, 5, 6])
# wrap the unpacked elements in `numpy.array()`
In [16]: np.array((*a, *b))
Out[16]: array([1, 2, 3, 5, 6])
Here are more approaches for doing this by using numpy.ravel()
, numpy.array()
, utilizing the fact that 1D arrays can be unpacked into plain elements:
# we'll utilize the concept of unpacking
In [15]: (*a, *b)
Out[15]: (1, 2, 3, 5, 6)
# using `numpy.ravel()`
In [14]: np.ravel((*a, *b))
Out[14]: array([1, 2, 3, 5, 6])
# wrap the unpacked elements in `numpy.array()`
In [16]: np.array((*a, *b))
Out[16]: array([1, 2, 3, 5, 6])
回答 5
来自numpy 文档的更多事实:
语法为 numpy.concatenate((a1, a2, ...), axis=0, out=None)
轴= 0用于行连接轴= 1用于列连接
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
# Appending below last row
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
# Appending after last column
>>> np.concatenate((a, b.T), axis=1) # Notice the transpose
array([[1, 2, 5],
[3, 4, 6]])
# Flattening the final array
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
希望对您有所帮助!
Some more facts from the numpy docs :
With syntax as numpy.concatenate((a1, a2, ...), axis=0, out=None)
axis = 0 for row-wise concatenation axis = 1 for column-wise concatenation
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
# Appending below last row
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
# Appending after last column
>>> np.concatenate((a, b.T), axis=1) # Notice the transpose
array([[1, 2, 5],
[3, 4, 6]])
# Flattening the final array
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
I hope it helps !
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。