# How to merge NumPy array into a single array in Python

Let us learn **how to merge a NumPy array into a single in Python**.

Skills required :

- Python basics.
- Arrays.

Finally, if you have to or more NumPy array and you want to join it into a single array so, Python provides more options to do this task.

Because two 2-dimensional arrays are included in operations, you can join them either **row-wise** or **column-wise**.

Mainly** NumPy()** allows you to join the given two arrays either by** rows** or **columns**.

Let us see some examples to understand the concatenation of NumPy.

## Merging NumPy array into Single array in Python

Firstly, import **N****umPy package **:

import numpy as np

Creating a NumPy array using arrange(), one-dimensional array eventually starts at **0** and ends at **8**.

array = np.arrange(7)

In this you can even join two exhibits in NumPy, it is practiced utilizing np.concatenate, np.hstack.np.np.concatenate it takes tuples as the primary contention.

The code is like this:

a = np.array([3,4,5]) b = np.array([5,6,7]) np.concatenate([a,b])

Output:

array([3, 4, 5, 5, 6, 7]) More than two arrays can be concatenated at once :

c = [20,20,20] print(np.concatenate([a,b,c]))

Output :

[3 4 5 5 6 7 20 20 20]

Now it can also be used for two-dimensional array also:

grid = np.array([[1,2,3], [4,5,6]]) #concatenate with first axis np.concatenate([grid],[grid])

Output: array([[1, 2, 3], [4,5,6], [1,2,3], [4,5,6]])

Normally NumPy data types are:

**bool**-Boolean(True or False) stored as a byte.**int**-default integer type.-
**i****nt c**-identical to C int. -
**int**-integer used for the index.

#### NumPy Environment:-

To test whether the NumPy module is properly installed, import from Python prompt

import numpy

If it is not installed this **error message** will be displayed:

Traceback(most recent call last): File"<pyshell #0>", line1, in<module> import numpy ImportError : No module named 'numpy

//Program for joining NumPy array//

import numpy as np a = np.array([[1,2],[3,4]]) print'first array:' print a print'\n' b = np.array([[5,6],[7,8]]) print'second array:' print b print'\n' #array of same dimensions. print'joining the two arrays along axis 0: print np.cocatenate((a,b)) print'\n' print'joining the two arrays along axis1: print np.cocatenate((a,b),axis=1)

Output:

First array: [[1 2] [3 4]] Second array: [[5 6] [7 8]] Joining the two array along axis 0: [[1 2] [3 4] [5 6] [7 8]] Joining the two array along axis 1: [[1 2 3 4 5 6] [3 4 7 8]]

**The NumPy array:**

Data manipulation in Python is nearly synonymous with NumPy array manipulation and new tools like pandas are built around NumPy array.

Be that as it may, this area will show a few instances of utilizing NumPy, initially exhibit control to get to information and subarrays and to part and join the array.

Practically these are the operations performed on NumPy:

- Attributes of the array.
- Indexing of array.
- Joining and parting of an array.

Finally subarrays as no-duplicate perspectives:

The most significant thing in array slicing is that they return sees as opposed to duplicates of the exhibit information.

Now let us go through one more example for merging NumPy array:

umpyimport numpy import time width=320 height=320 n_matrices=80 secondmatrices=list() for i in range(n_matrices): temp=numpy.random.rand(height,width).astype(numpy.float32) secondmatrices.append(numpy.round(temp*9)) firstmatrices=list() for i in range(n_matrices): temp=numpy.random.rand(height,width).astype(numpy.float32) firstmatrices.append(numpy.round(temp*9)) firstmatrices=list() for i in range(n_matrices): temp=numpy.random.rand(height,width).astype(numpy.float32) firstmatrices.append(numpy.round(temp*9)) t1=time.time() first1=numpy.array(firstmatrices) print time.time()-t1,"s merged_array=array(first_list_of_arrays)" temp=numpy.random.rand(height,width).astype(numpy.float32) firstmatrices.append(numpy.round(temp*9)) t1=time.time() first1=numpy.array(firstmatrices) print time.time()-t1,"s merged_array=array(first_list_of_arrays)" t1=time.time() second1=numpy.array(secondmatrices) print time.time()-t1,"s merged_array=array(second_list_of_arrays)" t1=time.time() first2=firstmatrices.pop() for i in range(len(firstmatrices)): first2=numpy.vstack((firstmatrices.pop(),first2)) print time.time()-t1,"s vstack first" t1=time.time() second2=secondmatrices.pop() for i in range(len(secondmatrices)): second2=numpy.vstack((secondmatrices.pop(),second2)) print time.time()-t1,"s vstack second"

Output:

## Leave a Reply