Concatenate or combine two NumPy array in Python
In this tutorial, we’re going to discuss and learn how to Concatenate or combine two Numpy array in Python. The program is mainly used to merge two arrays. we’re going to do this using Numpy.
How to combine or concatenate two NumPy array in Python
At first, we have to import Numpy. Numpy is a package in python which helps us to do scientific calculations. numpy has a lot of functionalities to do many complex things.
So first we’re importing Numpy:
import numpy as np
Next, we’re creating a Numpy array. so in this stage, we first take a variable name. then we type as we’ve denoted numpy as np. After this, we use ‘.’ to access the NumPy package. Next press array then type the elements in the array. the code is:
arr1=np.array([[11,23,34],[38,46,35]]) arr2=np.array([[200,29,386],[19,20,56]])
Now when we’re going to do concatenate, then we can make this happen in two ways, this along axis 0 and along axis 1. in Numpy the default setting is axis=0. So if we want to combine along 0 axis then we need not mention axis. but when we do it along 1 axis then we need to mention axis. At last, we use the print statement to print the combined array. The code is for merging is:
print (np.concatenate(arr1,arr2)) print (np.concatenate((arr1,arr2), axis=1))
The whole code for merging array is:
import numpy as np #creation of numpy array arr1=np.array([[11,23,34],[38,46,35]]) arr2=np.array([[200,29,386],[19,20,56]]) #joining along axis 0 print np.concatenate(arr1,arr2) #joining along axis 1 print np.concatenate((arr1,arr2), axis=1)
The output for the program along axis 0 is:
[[11,23,34] [38,46,35] [200,29,386] [19,20,56]]
The output for the program along axis 1 is:
[[11 23 34 200 29 386 ] [38 46 35 19 20 56 ]]
Time complexity: O(1)
Space complexity:O(1)
It doesn’t work, it’s just bad code, thanks for wasting my time
Show us the error, one of our authors will recheck it.
Nah, stop whining twat, thanks for wasting your own time