How do I concatenate two one-dimensional arrays in NumPy? I tried numpy.concatenate
:
JavaScript
x
5
1
import numpy as np
2
a = np.array([1, 2, 3])
3
b = np.array([4, 5])
4
np.concatenate(a, b)
5
But I get an error:
TypeError: only length-1 arrays can be converted to Python scalars
Advertisement
Answer
Use:
JavaScript
1
2
1
np.concatenate([a, b])
2
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.