9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook
Python NumPy
NumPy
NumPy, which stands for Numerical Python, is an open-source Python library
consisting of multidimensional and single-dimensional array elements.
It's a standard that computes numerical data in Python.
NumPy is most widely used in almost every domain where numerical computation is
required, like science and engineering
In [1]: # Importing NumPy Array
import numpy as np
Creating Arrays
NumPy arrays are more efficient than Python lists for numerical operations due to their fixed
size and homogeneous data type.
1D Array (Vector):
In [2]: arr1d = [Link]([1, 2, 3, 4, 5])
print(arr1d)
[1 2 3 4 5]
In [3]: list1=[1,2,3,4]
arr1=[Link](list1) # convert the list into array
print(arr1)
[1 2 3 4]
In [5]: print(type(arr1)) #get type of the array
print(type(arr1d))
<class '[Link]'>
<class '[Link]'>
In [6]: type(arr1)
Out[6]: [Link]
In [7]: print(arr1*2)
[2 4 6 8]
localhost:8889/notebooks/Python/Python/Class 5 [Link] 1/7
9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook
In [8]: print([Link]) #give the no. of row and no . of column
(4,)
In [9]: print([Link]) #give the total no. of element in the array
In [10]: print([Link]) # give the type of element and bit size
int32
In [11]: arr2=[Link]([1,2,3,4],np.int64) #to change the data type and bit size
print([Link])
int64
In [12]: arr3=[Link]([1,2,3,4],np.float32)
print([Link])
print(arr3)
float32
[1. 2. 3. 4.]
In [13]: arr4=[Link]((5)) #give an array of given size consisting 0 only
print(arr4)
[0. 0. 0. 0. 0.]
In [14]: arr5=[Link]((5))
print(arr5)
[1. 1. 1. 1. 1.]
In [15]: print([Link]) # gives the dimension of the array
2D Array (Matrix):
In [16]: l1=[1,2,3]
l2=[4,5,6]
l3=[7,8,9]
arr6=[Link]([l1,l2,l3])
print(arr6)
[[1 2 3]
[4 5 6]
[7 8 9]]
In [17]: type(arr6)
Out[17]: [Link]
localhost:8889/notebooks/Python/Python/Class 5 [Link] 2/7
9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook
In [18]: print([Link]) #give the matrix size
(3, 3)
In [19]: print([Link]) #give the total element in the matrix
In [20]: print([Link]) # give the type of element and bit size
int32
In [21]: print(l1+l2)
[1, 2, 3, 4, 5, 6]
In [22]: np1=[Link](l1)
np2=[Link](l2)
print(np1+np2)
[5 7 9]
In [24]: a2=[Link]([[1,2,3],[4,5,6],[7,8,9]])
print(a2)
[[1 2 3]
[4 5 6]
[7 8 9]]
In [25]: print([Link])
print([Link])
(3, 3)
9
In [26]: print([Link]) # gives the dimension of the array
3D Array (Tensor):
In [27]: arr3d = [Link]([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3d)
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
localhost:8889/notebooks/Python/Python/Class 5 [Link] 3/7
9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook
In [28]: a3=[Link]([[[1,2,3],[2,3,4]],[[4,5,6],[6,5,4]],[[7,8,5],[2,4,5]]])
print(a3)
[[[1 2 3]
[2 3 4]]
[[4 5 6]
[6 5 4]]
[[7 8 5]
[2 4 5]]]
In [30]: print([Link])
print([Link])
(3, 2, 3)
18
In [31]: print([Link]) # gives the dimension of the array
NumPy - Indexing & Slicing
In [32]: a = [Link](10)
In [33]: a
Out[33]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [34]: s = slice(2,7,2)
In [35]: a[s]
Out[35]: array([2, 4, 6])
In [36]: b = a[2:9:2]
In [37]: print(b)
[2 4 6 8]
In [38]: c = a[5]
print(c)
In [39]: print(a[2:])
[2 3 4 5 6 7 8 9]
localhost:8889/notebooks/Python/Python/Class 5 [Link] 4/7
9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook
In [40]: print(a[2:5])
[2 3 4]
In [41]: a = [Link]([[1,2,3],[3,4,5],[4,5,6]])
print(a)
[[1 2 3]
[3 4 5]
[4 5 6]]
In [43]: print(a[:])
[[1 2 3]
[3 4 5]
[4 5 6]]
In [44]: print(a[:4])
[[1 2 3]
[3 4 5]
[4 5 6]]
In [45]: print(a[4:])
[]
In [46]: print(a[1:4])
[[3 4 5]
[4 5 6]]
In [47]: print(a[2:])
[[4 5 6]]
In [48]: x = [Link]([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
In [51]: print(x)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
In [52]: print(x[:,:])
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
localhost:8889/notebooks/Python/Python/Class 5 [Link] 5/7
9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook
In [53]: print(x[1:,:])
[[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
In [54]: print(x[1:3,:])
[[3 4 5]
[6 7 8]]
In [56]: print(x[:,1:3])
[[ 1 2]
[ 4 5]
[ 7 8]
[10 11]]
In [58]: print(x[1:3,1:])
[[4 5]
[7 8]]
In [59]: print(x[1:3,1:3])
[[4 5]
[7 8]]
In [61]: print(x[2:,1:2])
[[ 7]
[10]]
In [67]: a1=[Link](32)
print(a1)
[Link]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31]
Out[67]: 1
In [69]: a2=[Link](8,4)
a2
Out[69]: array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]])
localhost:8889/notebooks/Python/Python/Class 5 [Link] 6/7
9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook
In [70]: [Link]
Out[70]: 2
In [72]: a3 = [Link](2,4,4)
a3
Out[72]: array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]]])
In [73]: [Link]
Out[73]: 3
In [75]: ar4=[Link](10,20,5) # give 5 random element between 10 to 20
print(ar4)
[10 15 17 13 14]
In [76]: ar5=[Link](5,3) # gives a matrix of given size concist of random e
print(ar5)
[[-0.60554537 0.82270419 -0.68292731]
[ 1.11267969 0.57879301 0.43384949]
[ 0.09422181 2.21999736 0.35297232]
[-0.45893539 -1.28833069 -2.15839099]
[ 0.06352359 -1.27736041 0.12127969]]
In [77]: ar6=[Link].random_sample((5,3)) # gives a matrix of given size concist o
print(ar6)
[[0.51779312 0.15548194 0.31707927]
[0.54605588 0.09933863 0.67800372]
[0.77162612 0.84544265 0.56366543]
[0.02496134 0.50177791 0.21819764]
[0.57773426 0.47091659 0.39079135]]
localhost:8889/notebooks/Python/Python/Class 5 [Link] 7/7