import numpy as np
############## 1. CREATING ARRAYS . $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#2. Creating Arrays
#Creating a 1D array:
a = np.array([1, 2, 3, 4])
print(a)
[1 2 3 4]
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
[[1 2 3]
[4 5 6]]
# Generates ..zeros as per the dimensions feed by the user.
c = np.zeros((2, 3))
print(c)
[[0. 0. 0.]
[0. 0. 0.]]
# Generates ..ones as per the dimensions feed by the user.
d = np.ones((3, 2))
print(d)
[[1. 1.]
[1. 1.]
[1. 1.]]
e = np.full((2, 2), 7)
print(e)
[[7 7]
[7 7]]
f = np.arange(20) # Equivalent to range(10)
print(f)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
g = np.linspace(0, 1, 5) # 5 values from 0 to 1
print(g)
[0. 0.25 0.5 0.75 1. ]
######################### Array Operations..............$$$$$$$$$$$$$
h = np.array([1, 2, 3])
i = np.array([4, 5, 6])
array_addition = h + i
print(array_addition)
[5 7 9]
product_array = h * i #Elementwise multiplication
print(product_array)
[ 4 10 18]
j = np.array([[1, 2], [3, 4]]) #Matrix multiplication:
k = np.array([[5, 6], [7, 8]])
k np.array([[5, 6], [7, 8]])
matrix_product = np.dot(j, k)
print(matrix_product)
[[19 22]
[43 50]]
## 4. Array Statistics
#Mean of an array:
mean_val = np.mean(f)
print(mean_val)
9.5
#Standard deviation of an array:
std_dev = np.std(f)
print(std_dev)
5.766281297335398
#Sum of array elements:
total_sum = np.sum(f)
print(total_sum)
190
### 5. Reshaping Arrays
reshaped = np.reshape(f, (4, 5)) ##Reshape a 1D array to 2D:
print(reshaped)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
#Flatten a 2D array:
flattened = reshaped.flatten()
print(flattened)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
#### 6. Array Indexing and Slicing
element = b[1, 2] # Element at row 1, column 2
print(element)
slice_array = b[0:2, 1:3] #Slicing an array:
print(slice_array)
[[2 3]
[5 6]]
#### 7. Applying Functions
# Apply a function to each element:
squared_array = np.sqrt(f)
print(squared_array)
[0. 1. 1.41421356 1.73205081 2. 2.23606798
2.44948974 2.64575131 2.82842712 3. 3.16227766 3.31662479
3.46410162 3.60555128 3.74165739 3.87298335 4. 4.12310563
4.24264069 4.35889894]
# Apply a function along an axis:
print(b)
print()
sum_axis0 = np.sum(b, axis=0) # Sum along rows
print(sum_axis0)
sum_axis1 = np.sum(b, axis=1) # Sum along columns
print(sum_axis1)
[[1 2 3]
[4 5 6]]
[5 7 9]
[ 6 15]
### 8. Random Numbers
random_ints = np.random.randint(0, 10, size=(2, 3)) # Generate random integer
print(random_ints)
[[2 6 0]
[6 2 0]]
random_floats = np.random.random((2, 2)) # Generate Random Floats
print(random_floats)
[[0.15120192 0.46644157]
[0.40234905 0.27096769]]
####### LINEAR ALGEBRA APPLICATIONS $$$$$$$$$$$$$$$
### Matrix Multiplication
#Matrix multiplication is fundamental in linear algebra. NumPy uses the np.dot() function or the @ operator for this purpose.
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Matrix multiplication
C = np.dot(A, B)
# Alternatively: C = A @ B
print(C)
[[19 22]
[43 50]]
#Solving Linear Systems
#You can solve systems of linear equations
#Ax=b using the np.linalg.solve() function.
A = np.array([[3, 2], [1, 2]])
b = np.array([5, 6])
# Solve for x
x = np.linalg.solve(A, b)
print(x)
[-0.5 3.25]
#3. Matrix Decomposition
#Matrix decompositions such as LU decomposition, QR decomposition, and Singular Value Decomposition (SVD) are crucial in numerical analy
#LU Decomposition
from scipy.linalg import lu
A = np.array([[3, 2], [1, 2]])
# LU decomposition
P, L, U = lu(A)
print("P:", P)
print("L:", L)
print("U:", U)
P: [[1. 0.]
[0. 1.]]
L: [[1. 0. ]
[0.33333333 1. ]]
U: [[3. 2. ]
[0. 1.33333333]]
#4. Eigenvalues and Eigenvectors
# Finding eigenvalues and eigenvectors is essential in various fields, including stability analysis and principal component analysis (PC
#Ax=(Lambda)x x is eigen vector, lambda is eign value
A = np.array([[4, 2], [1, 3]])
# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)
Eigenvalues: [5. 2.]
Eigenvectors: [[ 0.89442719 -0.70710678]
[ 0.4472136 0.70710678]]
#5. Matrix Determinant
#The determinant of a matrix is a scalar value that can be used to determine if a matrix is invertible and other properties.
A = np.array([[1, 2], [3, 4]])
# Compute determinant
det = np.linalg.det(A)
print("Determinant:", det)
Determinant: -2.0000000000000004
# 6. Matrix Inversion
# Inverting a matrix is necessary for solving linear systems and other applications.
A = np.array([[1, 2], [3, 4]])
# Compute the inverse
A_inv = np.linalg.inv(A)
print("Inverse of A:\n", A_inv)
Inverse of A:
[[-2. 1. ]
[ 1.5 -0.5]]
#7. Norms
#Matrix norms and vector norms help in assessing the magnitude of vectors and matrices.
v = np.array([1, 2, 3])
# Compute L2 norm (Euclidean norm)
norm_v = np.linalg.norm(v)
print("Norm of v:", norm_v)
Norm of v: 3.7416573867739413
##8. Rank of a Matrix
## Determining the rank of a matrix helps in understanding its properties.
A = np.array([[6, 8], [3, 4]])
# Compute the rank
rank_A = np.linalg.matrix_rank(A)
print("Rank of A:", rank_A)
Rank of A: 1
Start coding or generate with AI.