连接两个一维numpy的阵列 [英] Concatenating two one-dimensional NumPy arrays
问题描述
我在 numpy的两个简单的一维数组。我应该能够使用将它们串联<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html\">numpy.concatenate.但我得到这个错误低于code:
类型错误:仅长度-1阵列可以转换到Python标量
块引用>code
进口numpy的
一个= numpy.array([1,2,3])
B = numpy.array([5,6])
numpy.concatenate(A,B)为什么?
解决方案行应该是:
numpy.concatenate([A,B])
要连接的阵列需要传过来的顺序,而不是作为单独的参数。
从 numpy的文档:
numpy.concatenate((A1,A2,...),轴= 0)
加入阵列的序列在一起。
块引用>这是试图间preT你的
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?
解决方案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.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.这篇关于连接两个一维numpy的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!