numpy-capge
May 13, 2024
1 Explain Numpy library in Python
1. NumPy is a powerful library in Python used for numerical computing.
2. It provides support for large, multi-dimensional arrays and matrices, along with a collection
of mathematical functions to operate on these arrays efficiently.
3. NumPy is extensively used in scientific computing, data analysis, and machine learning ap-
plications
2 Key operations of NumPy
1. Creating Arrays: NumPy allows you to create arrays easily using various methods like
numpy.array(), numpy.zeros(), numpy.ones(), numpy.arange(), numpy.linspace(), etc. You
can create arrays of any dimensions (1D, 2D, 3D, etc.) with different data types.
2. Array Indexing and Slicing: Like Python lists, NumPy arrays support indexing and slicing
operations. You can access individual elements of an array using square brackets [].
3. Array Manipulation: NumPy provides functions for manipulating arrays such as reshaping,
flattening, stacking, splitting, and concatenating arrays.
4. Mathematical Operations: NumPy provides a wide range of mathematical functions to per-
form element-wise operations on arrays. These include basic arithmetic operations (addition,
subtraction, multiplication, division), trigonometric functions (sin, cos, tan), exponential and
logarithmic functions, statistical functions (mean, median, standard deviation), etc.
5. Linear Algebra Operations: NumPy provides comprehensive support for linear algebra
operations such as matrix multiplication (numpy.dot() or @ operator), matrix inversion
(numpy.linalg.inv()), eigenvalue decomposition (numpy.linalg.eig()), singular value decom-
position (numpy.linalg.svd()), and many others. These operations are essential for various
scientific and engineering computations.
6. Array Comparison and Boolean Operations: NumPy supports element-wise comparison op-
erations (==, !=, <, >, etc.) between arrays, resulting in Boolean arrays. These Boolean
arrays can be used for various purposes like filtering, masking, or indexing.
[1]: import numpy as np
[2]: #creating a 1D array
arr=np.array([2,3,5,67,8,8])
1
print(arr)
[ 2 3 5 67 8 8]
[4]: #creating a 2D array
arr=np.array([2,3,5],[2,3,5])
print(arr)
[[2 3 5]
[2 3 5]]
[12]: #creating a 3D array
arr=np.array([[[2,3,5],[2,3,5],[2,4,5]]])
print(arr)
[[[2 3 5]
[2 3 5]
[2 4 5]]]
[7]: #print a zero metrix
arr=np.zeros((2,3))
print(arr)
[[0. 0. 0.]
[0. 0. 0.]]
[8]: #print a ones metrix
arr=np.ones((2,3))
print(arr)
[[1. 1. 1.]
[1. 1. 1.]]
[11]: # Addition of two metrix
arr1=np.array([[2,31],[3,2,4]])
arr2=np.array([[12,2],[1,2,4],])
result=arr1+arr2
print(result)
[[14 5]
[ 4 4]]
[14]: # multiplication of two metrix
arr1=np.array([[2,3],[3,2]])
arr2=np.array([[12,2],[1,2]])
result=arr1*arr2
print(result)
2
[[24 6]
[ 3 4]]
[17]: #access elements using indexing
arr1=np.array([2,3,3,2,5])
print("first elemnt",arr1[0])
print("first elemnt",arr1[4])
first elemnt 2
first elemnt 5
[19]: # access elements using slicing
arr1=np.array([2,3,3,2,5])
print("first three elemnts",arr1[:4])
first three elemnts [2 3 3 2]
[22]: arr1=np.array([2,3,3,2,5])
print("elemnt form 1 index to 3",arr1[0:3])
elemnt form 1 index to 3 [2 3 3]
[27]: arr1=np.array([2,3,3,2,5])
print("elemnt form 1 index to ",arr1[-3:])
elemnt form 1 index to [3 2 5]
[51]: # reshape of metrix
arr=np.arange(9).reshape(3,3)
print(arr)
[[0 1 2]
[3 4 5]
[6 7 8]]
[2]: import numpy as np
arr=np.arange(1,10)
reshape_arr=np.reshape(arr,(3,3))
print("reshaped array")
print(reshape_arr)
reshaped array
[[1 2 3]
[4 5 6]
[7 8 9]]
[3]: arr1=np.array([[2,3],[3,2]])
arr2=np.array([[12,2],[1,2]])
3
result=arr1/arr2
print(result)
[[0.16666667 1.5 ]
[3. 1. ]]
[4]: arr=np.array([1,2,5])
log_value=np.log(arr)
print(log_value)
[0. 0.69314718 1.60943791]
[5]: arr=np.array([1,2,5])
expon_value=np.exp(arr)
print(expon_value)
[ 2.71828183 7.3890561 148.4131591 ]
[30]: #slicing in 2d array
arr = np.array([[5, 6, 3, 5, 7] , [23,4, 5, 7 ,9]])
print("slicing in 2d array", arr[ 1,1:4])
slicing in 2d array [4 5 7]
[9]: import numpy as np
arr2=np.array([[[2,3,4,4,5],[6,45,4,2,65],[6,7,4,1,4],[2,2,3,5,7]])
# print(arr2)
print("slicing in 3d" ,arr2[0:4,0:2])
slicing in 3d [[[ 2 3 4 4 5]
[ 6 45 4 2 65]]]
[55]: #find mean of 1D metrix
arr=np.array([2,2,3,4,5,6])
mean=np.mean(arr)
print(mean)
3.6666666666666665
[56]: #find median of 1D metrix
arr=np.array([2,2,3,4,5,6])
median=np.median(arr)
print(median)
3.5
[57]: #find standard deviation of 1D metrix
arr=np.array([2,2,3,4,5,6])
4
stdv=np.std(arr)
print(stdv)
1.49071198499986
[65]: #find mode of 1D metrix
from scipy.stats import mode
arr=np.array([2,4,4])
# from scipy import stats
mode_arr=stats.mode(arr)
print(mode_arr)
ModeResult(mode=array([4]), count=array([2]))
[28]: #metrix multiplication
arr1=np.array([[2,3],[43,3]])
arr2=np.array([[4,3],[6,5]])
mul=np.dot(arr1,arr2)
print(mul)
# another method
arr1=np.array([[2,3],[43,3]])
arr2=np.array([[4,3],[6,5]])
arr3=np.array([[2,3],[2,5]])
mul=arr1@arr2@arr3
print(mul)
[[ 26 21]
[190 144]]
[[ 94 183]
[ 668 1290]]
[17]: arr1=np.array([[2,3],[43,3]])
inverse=np.linalg.inv(arr1)
print(inverse)
[[-0.02439024 0.02439024]
[ 0.3495935 -0.01626016]]
[19]: #metrix transpose
arr1=np.array([[2,3],[43,3]])
print(arr1)#orignal
transpose=np.transpose(arr1)
print(transpose)#transpose array
5
[[ 2 3]
[43 3]]
[[ 2 43]
[ 3 3]]
[21]: #linear equation
arr1=np.array([[2,3],[43,3]])
arr2=np.array([[4,3],[6,5]])
p=np.linalg.solve(arr1,arr2)
print(p)
[[0.04878049 0.04878049]
[1.30081301 0.96747967]]
[29]: #determinant of a metrix
arr1=np.array([[2,3],[43,3]])
print(arr1)
deter=np.linalg.det(arr1)
print(deter)
[[ 2 3]
[43 3]]
-123.00000000000006
[ ]: