Reputation: 17321
How do I concatenate two one-dimensional arrays in NumPy? I tried numpy.concatenate
:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5])
np.concatenate(a, b)
But I get an error:
TypeError: only length-1 arrays can be converted to Python scalars
Upvotes: 420
Views: 580538
Reputation: 2137
Slightly different problem, that is if you want two 1D array with shape (n,)
into (2,n)
, then you have the following options:
import numpy as np
np.r_[[a], [a]]
np.stack([a, a])
np.vstack([a, a])
np.concatenate([[a], [a]])
np.array([a, a])
The fastest way is to use plain numpy.array
:
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]),
lambda a: numpy.vstack([a, a]),
lambda a: numpy.concatenate([[a], [a]]),
lambda a: numpy.array([a, a]),
],
labels=["r_", "stack", "vstack", "concatenate", "array"],
n_range=[2 ** k for k in range(19)],
xlabel="len(a)",
)
Upvotes: 0
Reputation: 1890
An alternative ist to use the short form of "concatenate" which is either "r_[...]" or "c_[...]" as shown in the example code beneath (see Link 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.]]
Upvotes: 15
Reputation: 45039
Use:
np.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.
Upvotes: 556
Reputation: 58721
There are several possibilities for concatenating 1D arrays, e.g.,
import numpy as np
np.r_[a, a]
np.stack([a, a]).reshape(-1)
np.hstack([a, a])
np.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)",
)
Upvotes: 87
Reputation: 3023
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 !
Upvotes: 4
Reputation: 61305
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])
Upvotes: 2
Reputation: 86698
The first parameter to concatenate
should itself be a sequence of arrays to concatenate:
numpy.concatenate((a,b)) # Note the extra parentheses.
Upvotes: 41