Concatenating multiple np arrays in python
I have some bumpy arrays and I want to concatenate them. I am using np.concatenate((array1,array2),axis=1)
. My problem is that I want to make the number of parameters parameterizable, I wrote this function
x1=np.array([1,0,1])
x2=np.array([0,0,1])
x3=np.array([1,1,1])
def conc_func(*args):
xt=[]
for a in args:
xt=np.concatenate(a,axis=1)
print xt
return xt
xt=conc_func(x1,x2,x3)
this function returns ([1,1,1]), I want it to return ([1,0,1,0,0,1,1,1,1]). I tried adding a for loop inside np.concatenate
as such
xt =np.concatenate((for a in args: a),axis=1)
but I am getting syntax error. I can't use either the app or the extension, because I have to deal with numpy arrays
, not lists
. Can anyone please help?
Thank you in advance
source to share
concatenate
can take a sequence of arrays, for example args
:
In [11]: args = (x1, x2, x3)
In [12]: xt = np.concatenate(args)
In [13]: xt
Out[13]: array([1, 0, 1, 0, 0, 1, 1, 1, 1])
By the way, while axis=1
works, the inputs are all one-dimensional arrays (so they only have 0 axis). Therefore, it makes sense to use axis=0
or omit axis
entirely, as the default is axis=0
.
source to share