NumPy - Ds
NumPy - Ds
import numpy as np
arr=np.array([1,2,4,5])
print(arr)
[1 2 4 5]
import numpy as np
arr=np.array([[1,2,3,4,5],[6,7,8,9,10]])
print(arr)
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
import numpy as np
arr=np.array([[23,24,25,26],[34,35,36,37],[44,45,46,47]])
print(arr)
[[23 24 25 26]
[34 35 36 37]
[44 45 46 47]]
import numpy as np
a1=np.array(12)
a2=np.array([13,23,45])
a3=np.array([[14,23,45],[23,45,67]])
print(a1.ndim)
print(a2.ndim)
print(a3.ndim)
0
1
2
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[0:1])
[14]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[0:2])
[14 24]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[0:3])
[14 24 34]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[0:4])
[14 24 34 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[::1])
[14 24 34 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[0::1])
[14 24 34 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[0::2])
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 1/15
24/07/2025, 08:34 NumPy - Colab
[14 34]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[0::3])
[14 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[1::])
[24 34 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[2::])
[34 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[3::])
[44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[::1])
[14 24 34 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[::2])
[14 34]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[::3])
[14 44]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[1::3])
[24]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[2::3])
[34]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[::-1])
[44 34 24 14]
import numpy as np
arr = np.array([14, 24, 34, 44])
print(arr[::-2])
[44 24]
import numpy as np
a1=np.array(12)
a2=np.array([13,23,45])
a3=np.array([[14,23,45],[23,45,67]])
print(a1+a2)
[25 35 57]
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 2/15
24/07/2025, 08:34 NumPy - Colab
import numpy as np
arr=np.array([[14,22,43],[23,45,67]])
print(arr[0:1])
print(arr[1][1])
[[14 22 43]]
45
import numpy as np
arr=np.array([[14,22,43],[23,45,67]])
print(arr[0,2])
43
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
keyboard_arrow_down Reshape
import numpy as np
arr=np.array([[14,22,43],[23,45,67]])
print(arr.reshape(3,2))
[[14 22]
[43 23]
[45 67]]
import numpy as np
arr=np.array([[14,22,43],[23,45,67]])
arr1=arr.reshape(2,3)
print(arr1)
[[14 22 43]
[23 45 67]]
import numpy as np
arr=np.array([[14,22,43,26,78,90,87,23,45,67]])
print(arr.reshape(2,-1))
[[14 22 43 26 78]
[90 87 23 45 67]]
import numpy as np
arr=np.array([14,22,43,26,78,90,87,23,45,67])
print(arr.reshape(2, -1).base)
[14 22 43 26 78 90 87 23 45 67]
arr=np.array([14,22,43,26,78,90,87,23,45,67,43,65,76,12])
arr2= arr.reshape(2, -1, 7)
print(arr2)
[[[14 22 43 26 78 90 87]]
[[23 45 67 43 65 76 12]]]
Flattening
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
new = arr.reshape(-2)
print(new)
[1 2 3 4 5 6]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9]])
new = arr.reshape(-2)
print(new)
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 3/15
24/07/2025, 08:34 NumPy - Colab
print(new)
[1 2 3 4 5 6 7 8 9]
int64
import numpy as np
arr = np.array([['apple'], ['mango']])
print(arr.dtype)
<U5
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='S')
print(arr.dtype)
|S1
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='i')
print(arr)
print(arr.dtype)
[1 2 3 4]
int32
Change data type from float to integer by using int as parameter value:
import numpy as np
arr =np.array([1.1, 2.1, 3.1, 4.1])
new= arr.astype(int)
print(new)
print(new.dtype)
[1 2 3 4]
int64
Iterating Arrays
Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop
of python.If we iterate on a 1-D array it will go through each element one by one.
import numpy as np
arr=([12,34,56,78])
for x in arr:
print(x)
12
34
56
78
import numpy as np
arr1=([12,34,56,78])
arr2=([23,45,67,89])
for x in arr1:
for y in arr2:
print(x,y)
12 23
12 45
12 67
12 89
34 23
34 45
34 67
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 4/15
24/07/2025, 08:34 NumPy - Colab
34 89
56 23
56 45
56 67
56 89
78 23
78 45
78 67
78 89
import numpy as np
arr=([[12,34,56,78],[21,43,65,87]])
for x in arr:
print(x)
import numpy as np
arr=([[12,34,56,78],[21,43,65,87]])
for x in arr:
print(x)
import numpy as np
arr=([[12,34,56,78],[21,43,65,87]])
for x in arr:
for y in x:
print(y)
12
34
56
78
21
43
65
87
import numpy as np
arr=([12,34,45,56],[23,45,67,78],[98,76,54,32])
for i in arr:
print(i)
import numpy as np
arr=([12,34,45,56],[23,45,67,78],[98,76,54,32])
for i in arr:
for j in i:
print(j)
12
34
45
56
23
45
67
78
98
76
54
32
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 5/15
24/07/2025, 08:34 NumPy - Colab
import numpy as np
arr=([12,34,45,56],[23,45,67,78],[98,76,54,32],[43,65,36,97])
for x in arr:
for y in x:
for z in y:
print(z)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-452672ba1d23> in <cell line: 0>()
3 for x in arr:
4 for y in x:
----> 5 for z in y:
6 print(z)
7
The function nditer() is a helping function that can be used from very basic to very advanced iterations. It solves some basic issues which we
face in iteration, lets go through it with examples.
import numpy as np
arr=([12,34,45,56],[23,45,67,78])
for i in np.nditer(arr):
print(i)
(array(12), array(23))
(array(34), array(45))
(array(45), array(67))
(array(56), array(78))
We can use op_dtypes argument and pass it the expected datatype to change the datatype of elements while iterating.
NumPy does not change the data type of the element in-place (where the element is in array) so it needs some other space to perform this
action, that extra space is called buffer, and in order to enable it in nditer() we pass flags=['buffered'].
import numpy as np
arr = np.array([1, 2, 3])
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
print(x)
np.bytes_(b'1')
np.bytes_(b'2')
np.bytes_(b'3')
import numpy as np
arr = np.array([1, 2, 3])
for x in np.nditer(arr, flags=['buffered'], op_dtypes='int'):
print(x)
1
2
3
import numpy as np
arr = np.array([1, 2, 3])
for x in np.nditer(arr, flags=['buffered'], op_dtypes='float'):
print(x)
1.0
2.0
3.0
Example
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 6/15
24/07/2025, 08:34 NumPy - Colab
import numpy as np
arr = np.array([[1, 2, 3, 7],[4, 5, 6, 8]])
for x in np.nditer(arr[:, ::2]):
print(x)
1
3
4
6
import numpy as np
arr = np.array([[1, 2, 3, 7],[4, 5, 6, 8]])
for x in np.nditer(arr[:, :2]):
print(x)
1
2
4
5
Enumeration means mentioning sequence number of somethings one by one. Sometimes we require corresponding index of the element
while iterating, the ndenumerate() method can be used for those usecases.
import numpy as np
arr = np.array([1, 2, 3])
for idx, x in np.ndenumerate(arr):
print(idx, x)
(0,) 1
(1,) 2
(2,) 3
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for idx, x in np.ndenumerate(arr):
print(idx, x)
(0, 0) 1
(0, 1) 2
(0, 2) 3
(0, 3) 4
(1, 0) 5
(1, 1) 6
(1, 2) 7
(1, 3) 8
keyboard_arrow_down Transpose
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print("Original:\n",arr)
print("Transpose:\n",arr.T)
Original:
[[1 2 3 4]
[5 6 7 8]]
Transpose:
[[1 5]
[2 6]
[3 7]
[4 8]]
import numpy as np
gfg = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(gfg, end ='\n\n')
print(gfg.T)
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 4 7]
[2 5 8]
[3 6 9]]
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 7/15
24/07/2025, 08:34 NumPy - Colab
Matrix Transpostion
array([[1, 7],
[2, 8],
[3, 9]])
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
[[1 4]
[2 5]
[3 6]]
Joining means putting contents of two or more arrays in a single array. In SQL we join tables based on a key, whereas in NumPy we join
arrays by axes. We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not explicitly
passed, it is taken as 0.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
[1 2 3 4 5 6]
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=0)
print(arr)
[[1 2]
[3 4]
[5 6]
[7 8]]
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)
[[1 2 5 6]
[3 4 7 8]]
Stacking is same as concatenation, the only difference is that stacking is done along a new axis. We can concatenate two 1-D arrays along
the second axis which would result in putting them one over the other, ie. stacking. We pass a sequence of arrays that we want to join to the
stack() method along with the axis. If axis is not explicitly passed it is taken as 0.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 8/15
24/07/2025, 08:34 NumPy - Colab
[[1 4]
[2 5]
[3 6]]
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
print(arr)
[1 2 3 4 5 6]
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)
[[1 2 3]
[4 5 6]]
NumPy provides a helper function: dstack() to stack along height, which is the same as depth.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.dstack((arr1, arr2))
print(arr)
[[[1 4]
[2 5]
[3 6]]]
Splitting is reverse operation of Joining. Joining merges multiple arrays into one and Splitting breaks one array into multiple. We use
array_split() for splitting arrays, we pass it the array we want to split and the number of splits.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 2)
print(newarr)
The return value of the array_split() method is an array containing each of the split as an array. If you split an array into 3 arrays, you can
access them from the result just like any array element:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])
[1 2]
[3 4]
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 9/15
24/07/2025, 08:34 NumPy - Colab
[5 6]
Use the same syntax when splitting 2-D arrays. Use the array_split() method, pass in the array you want to split and the number of splits you
want to do.
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
newarr = np.array_split(arr, 3)
print(newarr)
[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]]), array([[ 9, 10],
[11, 12]])]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3)
print(newarr)
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]]), array([[13, 14, 15],
[16, 17, 18]])]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.hsplit(arr, 3)
print(newarr)
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
Searching Arrays
You can search an array for a certain value, and return the indexes that get a match. To search an array, use the where() method.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)
(array([3, 5, 6]),)
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 10/15
24/07/2025, 08:34 NumPy - Colab
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
x = np.where(arr%2 == 0)
print(x)
(array([1, 3, 5, 7]),)
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
x = np.where(arr%2 == 1)
print(x)
(array([0, 2, 4, 6]),)
Search Sorted
There is a method called searchsorted() which performs a binary search in the array, and returns the index where the specified value would
be inserted to maintain the search order.
import numpy as np
arr = np.array([6, 7, 8, 9])
x = np.searchsorted(arr, 7)
print(x)
import numpy as np
arr = np.array([6, 7, 8, 9])
x = np.searchsorted(arr, 7, side='right')
print(x)
import numpy as np
arr = np.array([1, 3, 5, 7])
x = np.searchsorted(arr, [2, 4, 6])
print(x)
[1 2 3]
Sorting Arrays
Sorting means putting elements in an ordered sequence. Ordered sequence is any sequence that has an order corresponding to elements,
like numeric or alphabetical, ascending or descending. The NumPy ndarray object has a function called sort(), that will sort a specified array.
import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))
[0 1 2 3]
import numpy as np
arr = np.array(['banana', 'cherry', 'apple'])
print(np.sort(arr))
import numpy as np
arr = np.array([True, False, True])
print(np.sort(arr))
import numpy as np
arr = np.array([[3, 2, 4], [5, 0, 1]])
print(np.sort(arr))
[[2 3 4]
[0 1 5]]
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 11/15
24/07/2025, 08:34 NumPy - Colab
ufuncs stands for "Universal Functions" and they are NumPy functions that operate on the ndarray object.
ufuncs are used to implement vectorization in NumPy which is way faster than iterating over elements. They also provide broadcasting and
additional methods like reduce, accumulate etc. that are very helpful for computation. ufuncs also take additional arguments, like: where
boolean array or condition defining where the operations should take place. dtype defining the return type of elements. out output array where
the return value should be copied.
x = [1, 2, 3, 4]
y = [4, 5, 6, 7]
z = []
[5, 7, 9, 11]
import numpy as np
x = [1, 2, 3, 4]
y = [4, 5, 6, 7]
z = np.add(x, y)
print(z)
[ 5 7 9 11]
Simple Arithmetic
You could use arithmetic operators + - * / directly between NumPy arrays, but this section discusses an extension of the same where we have
functions that can take any array-like objects e.g. lists, tuples etc. and perform arithmetic conditionally.
The add()
function sums the content of two arrays, and return the results in a new array.
import numpy as np
arr1 = np.array([10, 11, 12, 13, 14, 15])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.add(arr1, arr2)
print(newarr)
[30 32 34 36 38 40]
Subtraction
The subtract() function subtracts the values from one array with the values from another array, and return the results in a new array.
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.subtract(arr1, arr2)
print(newarr)
[-10 -1 8 17 26 35]
Multiplication
The multiply() function multiplies the values from one array with the values from another array, and return the results in a new array.
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.multiply(arr1, arr2)
print(newarr)
Division
The divide() function divides the values from one array with the values from another array, and return the results in a new array.
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 12/15
24/07/2025, 08:34 NumPy - Colab
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 10, 8, 2, 33])
newarr = np.divide(arr1, arr2)
print(newarr)
The power()
function rises the values from the first array to the power of the values of the second array, and return the results in a new array.
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 6, 8, 2, 33])
newarr = np.power(arr1, arr2)
print(newarr)
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.mod(arr1, arr2)
print(newarr)
[ 1 6 3 0 0 27]
The divmod() function return both the quotient and the mod. The return value is two arrays, the first array contains the quotient and second
array contains the mod.
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.divmod(arr1, arr2)
print(newarr)
Absolute Values
Both the absolute() and the abs() functions do the same absolute operation element-wise but we should use absolute() to avoid confusion
with python's inbuilt math.abs()
import numpy as np
arr = np.array([-1, -2, 1, 2, 3, -4])
newarr = np.absolute(arr)
print(newarr)
[1 2 1 2 3 4]
Trigonometric Functions
NumPy provides the ufuncs sin(), cos() and tan() that take values in radians and produce the corresponding sin, cos and tan values.
import numpy as np
x = np.sin(np.pi/2)
print(x)
1.0
import numpy as np
arr = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5])
x = np.sin(arr)
print(x)
import numpy as np
arr = np.array([90, 180, 270, 360])
x = np.deg2rad(arr)
print(x)
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 13/15
24/07/2025, 08:34 NumPy - Colab
[1.57079633 3.14159265 4.71238898 6.28318531]
import numpy as np
arr = np.array([np.pi/2, np.pi, 1.5*np.pi, 2*np.pi])
x = np.rad2deg(arr)
print(x)
Finding Angles
Finding angles from values of sine, cos, tan. E.g. sin, cos and tan inverse (arcsin, arccos, arctan). NumPy provides ufuncs arcsin(), arccos()
and arctan() that produce radian values for corresponding sin, cos and tan values given.
import numpy as np
x = np.arcsin(1.0)
print(x)
1.5707963267948966
import numpy as np
arr = np.array([1, -1, 0.1])
x = np.arcsin(arr)
print(x)
Hypotenues
Finding hypotenues using pythagoras theorem in NumPy. NumPy provides the hypot() function that takes the base and perpendicular values
and produces hypotenues based on pythagoras theorem.
import numpy as np
base = 3
perp = 4
x = np.hypot(base, perp)
print(x)
5.0
We can use NumPy's unique() method to find unique elements from any array. E.g. create a set array, but remember that the set arrays should
only be 1-D arrays.
import numpy as np
arr = np.array([1, 1, 1, 2, 3, 4, 5, 5, 6, 7])
x=np.unique(arr)
print(x)
[1 2 3 4 5 6 7]
import numpy as np
arr=([2,3,4,5,6,7,7,9,99,99,99])
print(np.unique(arr))
[ 2 3 4 5 6 7 9 99]
import numpy as np
arr1=np.array([2,3,4,5,6,7])
arr2=np.array([7,9,9,9, 9])
newarr=np.union1d(arr1,arr2)
print(newarr)
[2 3 4 5 6 7 9]
import numpy as np
arr1=np.array([2,3,4,5,6,7])
arr2=np.array([7,3,4,5,6])
newarr=np.intersect1d(arr1,arr2,assume_unique=True)
print(newarr)
[3 4 5 6 7]
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 14/15
24/07/2025, 08:34 NumPy - Colab
https://colab.research.google.com/drive/1u5gLPYVjY5VPfiQOMyzXb_PWNqFjGWae#scrollTo=8Aeg9yJqQQpq&printMode=true 15/15