Fix Python – Concatenating two one-dimensional NumPy arrays


Asked By – highBandWidth

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

Now we will see solution for issue: Concatenating two one-dimensional NumPy arrays



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.

This question is answered By – Winston Ewert

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0