0% found this document useful (0 votes)
4 views2 pages

2.numpy_arrays_2

The document provides a tutorial on various NumPy array operations including joining, stacking, splitting, searching, and sorting arrays. It demonstrates how to concatenate and stack arrays both row-wise and column-wise, as well as how to split arrays into multiple parts. Additionally, it covers searching for elements within arrays and sorting them in ascending order.

Uploaded by

pardhapradeep824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

2.numpy_arrays_2

The document provides a tutorial on various NumPy array operations including joining, stacking, splitting, searching, and sorting arrays. It demonstrates how to concatenate and stack arrays both row-wise and column-wise, as well as how to split arrays into multiple parts. Additionally, it covers searching for elements within arrays and sorting them in ascending order.

Uploaded by

pardhapradeep824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

02/02/2025, 16:10 numpy_arrays_2

In [7]: import numpy as np


# JOINING ARRAY NumPy :
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
arr3 = np.concatenate((arr1,arr2))
arr4 = np.array([[1,2],[3,4]])
arr5 = np.array([[5,6],[7,8]])
arr6 = np.concatenate((arr4,arr5))
arr7 = np.concatenate((arr4,arr5),axis=1)
print(arr3);print(arr6);print(arr7)

[1 2 3 4 5 6]
[[1 2]
[3 4]
[5 6]
[7 8]]
[[1 2 5 6]
[3 4 7 8]]

In [10]: # JOINING ARRAYS USING STACK FUNCTIONS :


# stacking is same as concatinating , the only difference is that stacking is do
# we can concatinate two 1d arrays along the second axis which would result in p
# axis: Along this axis, in the new array, input arrays are stacked. Possible va
# for n-dimensional output array. For example, in the case of a resultant 2-D ar
# options :0 and 1. axis=0 means 1D input arrays will be stacked row-wise. axis=
# stacked column-wise.We shall see the example later in detail. -1 means last di
# 1 and -1 are same. (optional)
arr1 = np.array([1,2,3])
arr2=np.array([4,5,6])
arr = np.stack((arr1,arr2),axis=1)
print(arr)
# stacking along rows :
arrh = np.hstack((arr1,arr2))
print(arrh)
# stacking along columns :
arrv = np.vstack((arr1,arr2))
print(arrv)
#stacking along height (depth) :
arrd = np.dstack((arr1,arr2))
print(arrd)

[[1 4]
[2 5]
[3 6]]
[1 2 3 4 5 6]
[[1 2 3]
[4 5 6]]
[[[1 4]
[2 5]
[3 6]]]

In [14]: # spliiting of arrays :


# there will be np.split,np.hsplit and np.vsplit for each of this we can pass a
x = np.array([1,2,3,4,5,6,7,8,9])
x1,x2,x3 = np.split(x,[3,5])
print(x1,x2,x3)
# vsplit :
grid = np.arange(16).reshape((4,4))
print(grid)

localhost:8891/doc/workspaces/auto-J/tree/numpy_arrays_2.ipynb? 1/2
02/02/2025, 16:10 numpy_arrays_2

u,l = np.vsplit(grid,[2])
print(u);print(l)
lf,rt = np.hsplit(grid,[2])
print(lf);print(rt)

[1 2 3] [4 5] [6 7 8 9]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[0 1 2 3]
[4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]

In [19]: # SEARCHING ARRAYS :


# where - return index
arr = np.array([1,2,3,4,5,6,6,7])
arr1 = np.where(arr == 4)
arr2 = np.where(arr%2 == 0)
print(arr1);print(arr2)
# searchsorted : this method performs a binarysearch in the array , and returns
# would be inserted to maitain the search order
x = np.searchsorted(arr,4)
print(x)
# search from right by default it is left
y = np.searchsorted(arr,4,side="right")
print(y)
# for multiple values
z = np.searchsorted(arr,[8,9])
print(z);print(arr)

(array([3], dtype=int64),)
(array([1, 3, 5, 6], dtype=int64),)
3
4
[8 8]
[1 2 3 4 5 6 6 7]

In [20]: # SORTING ARRAYS : sort()


arr = np.array([[8,3,5],[9,6,4]])
print(np.sort(arr))

[[3 5 8]
[4 6 9]]

localhost:8891/doc/workspaces/auto-J/tree/numpy_arrays_2.ipynb? 2/2

You might also like