Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interweaving two numpy arrays

Assume the following arrays are given:

a = array([1,3,5]) b = array([2,4,6]) 

How would one interweave them efficiently so that one gets a third array like this

c = array([1,2,3,4,5,6]) 

It can be assumed that length(a)==length(b).

like image 492
D R Avatar asked Mar 18 '11 01:03

D R


People also ask

Can you add two NumPy arrays?

To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

How do I connect two arrays in NumPy?

Use concatenate() to Join Two Arrays Use numpy. concatenate() to merge the content of two or multiple arrays into a single array. This function takes several arguments along with the NumPy arrays to concatenate and returns a Numpy array ndarray.

Can you zip two NumPy arrays?

The numpy. column_stack() function is another method that can be used to zip two 1D arrays into a single 2D array in Python.

Can NumPy arrays be multidimensional?

In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.


2 Answers

I like Josh's answer. I just wanted to add a more mundane, usual, and slightly more verbose solution. I don't know which is more efficient. I expect they will have similar performance.

import numpy as np a = np.array([1,3,5]) b = np.array([2,4,6])  c = np.empty((a.size + b.size,), dtype=a.dtype) c[0::2] = a c[1::2] = b 
like image 198
Paul Avatar answered Sep 30 '22 07:09

Paul


I thought it might be worthwhile to check how the solutions performed in terms of performance. And this is the result:

enter image description here

This clearly shows that the most upvoted and accepted answer (Pauls answer) is also the fastest option.

The code was taken from the other answers and from another Q&A:

# Setup import numpy as np  def Paul(a, b):     c = np.empty((a.size + b.size,), dtype=a.dtype)     c[0::2] = a     c[1::2] = b     return c  def JoshAdel(a, b):     return np.vstack((a,b)).reshape((-1,),order='F')  def xioxox(a, b):     return np.ravel(np.column_stack((a,b)))  def Benjamin(a, b):     return np.vstack((a,b)).ravel([-1])  def andersonvom(a, b):     return np.hstack( zip(a,b) )  def bhanukiran(a, b):     return np.dstack((a,b)).flatten()  def Tai(a, b):     return np.insert(b, obj=range(a.shape[0]), values=a)  def Will(a, b):     return np.ravel((a,b), order='F')  # Timing setup timings = {Paul: [], JoshAdel: [], xioxox: [], Benjamin: [], andersonvom: [], bhanukiran: [], Tai: [], Will: []} sizes = [2**i for i in range(1, 20, 2)]  # Timing for size in sizes:     func_input1 = np.random.random(size=size)     func_input2 = np.random.random(size=size)     for func in timings:         res = %timeit -o func(func_input1, func_input2)         timings[func].append(res)  %matplotlib notebook  import matplotlib.pyplot as plt import numpy as np  fig = plt.figure(1) ax = plt.subplot(111)  for func in timings:     ax.plot(sizes,              [time.best for time in timings[func]],              label=func.__name__)  # you could also use "func.__name__" here instead ax.set_xscale('log') ax.set_yscale('log') ax.set_xlabel('size') ax.set_ylabel('time [seconds]') ax.grid(which='both') ax.legend() plt.tight_layout() 

Just in case you have numba available you could also use that to create a function:

import numba as nb  @nb.njit def numba_interweave(arr1, arr2):     res = np.empty(arr1.size + arr2.size, dtype=arr1.dtype)     for idx, (item1, item2) in enumerate(zip(arr1, arr2)):         res[idx*2] = item1         res[idx*2+1] = item2     return res 

It could be slightly faster than the other alternatives:

enter image description here

like image 20
MSeifert Avatar answered Sep 30 '22 05:09

MSeifert