Lecture 강의

Undergrads 학부 Graduates 대학원 역학(Mech)/설계(Design)/FEM 인공지능(AI)/기계학습(ML)/IoT SAP/ETABS OpenSees/FeView/STKO 아바쿠스(Abaqus) 파이썬(Python)/매트랩(Matlab) 엑셀(Excel-VBA)/HTML 마이다스(MIDAS)

[03. 패키지 - SciPy] (1) sub 패키지, (2) 선형대수, (3) 최적화

작성자 : kim2kie

(2023-02-19)

조회수 : 12465

[참조]

공학자를 위한 Python, 조정래, 2022: 5. SciPy
https://wikidocs.net/15636

Dookie Kim, Python: From Beginning to Application,  2022
https://www.dropbox.com/s/oa86j9ap62esmtz/Python.pdf?dl=0 
 
SciPy는 과학기술계산을 위한 라이브러리이다.  

(1) sub 패키지
(2) 선형대수
 1) 행렬 곱셈
 2) determinant, solve, inverse
 3) 노름(norm)
 4) 선형회귀(linear least square): linalg.lstsq()
 5) 고유치(eigen) 문제: linalg.eig()
 6) 기타
(3) 최적화


 


 

 

(1) sub 패키지
Clustering package (scipy.cluster)
Constants (scipy.constants)
Discrete Fourier transforms (scipy.fftpack)
Integration and ODEs (scipy.integrate)
Interpolation (scipy.interpolate)
Input and output (scipy.io)
Linear algebra (scipy.linalg)
Miscellaneous routines (scipy.misc)
Multi-dimensional image processing (scipy.ndimage)
Orthogonal distance regression (scipy.odr)
Optimization and root finding (scipy.optimize)
Signal processing (scipy.signal)
Sparse matrices (scipy.sparse)
Sparse linear algebra (scipy.sparse.linalg)
Compressed Sparse Graph Routines (scipy.sparse.csgraph)
Spatial algorithms and data structures (scipy.spatial)
Special functions (scipy.special)
Statistical functions (scipy.stats)
Statistical functions for masked arrays (scipy.stats.mstats)
Low-level callback functions


(2) 선형대수: linalg
1) 행렬 곱셈

Ex)
import numpy as np
import scipy.linalg as linalg

A = np.array([[1,2,-1],
              [2,7,4],
              [0,4,-1]])
X = np.array([1,0,1.2])

# A*X = Y: (3x3)*(3x1)=(3x1)
Y1 = np.matmul(A,X) 
Y2 = np.dot(A,X)
Y3 = A.dot(X)

# A*X = Y: (3x3)*(3x4)=(3x4)
X = np.array([[1,2,3,4],
              [-1,2,3,1],
              [3,-2,5,9]])
Y1 = np.matmul(A,X)
Y2 = np.dot(A,X)
Y3 = A.dot(X)

 
2) determinant, solve, inverse

Ex-계속)

# 행렬식(determinant)
det = linalg.det(A)

# 솔버(solve)
Y = linalg.solve(A,X)
R = A.dot(Y) - B   # 결과 검증 확인 = 0

# 역행렬(inverse)
Ainv = linalg.inv(A)
R = A@Ainv # 결과 검증 확인 = I

 

3) 노름(norm)

Ex-계속)
# 벡터 노름
norm1 = linalg.norm(X,1) # L1(ABS)  norm == sum(np.abs(x))
norm2 = linalg.norm(X)   # L2(SRSS) norm == np.sqrt(sum(x*x))
normMax = linalg.norm(X,np.inf) # max(최댓값) norm  == np.max(abs(x))


4) 선형회귀(linear least square)

Ex)
import matplotlib.pyplot as plt
import numpy as np
from scipy import linalg

c1, c2 = 5.0, 2.0
X = np.linspace(0.1,1,100)
Y = c1*np.exp(-X) + c2*X
Z = Y + 0.05 * np.max(Y) * np.random.randn(len(Y))
A = np.vstack((np.exp(-X),X)).T  # vertical 합치기

C,resid,rank,sigma = linalg.lstsq(A,Z)
X2 = np.linspace(0.1,1,100)
Y2 = C[0]*np.exp(-X2) + C[1]*X2

plt.plot(X,Z,'X',X2,Y2)
plt.axis([0,1.1,3.0,5.5])
plt.xlabel('$X$')
plt.title('Data fitting with linalg.lstsq')
plt.show()


5) 고유치(eigen) 문제:

Ex)
D,V = linalg.eigh(A)   # for real sym or complex hermitian
D,V = linalg.eig(A)  


6) 기타

pseudo-inverse
generalized inverse
svd
lu decompositon
cholesky
QR
schur
matrix functions
special matrix


(3) 최적화

scipy.optimize 패키지는 다음과 같은 최적화 문제를 풀 수 있다.

Local optimization: 비구속 및 구속 조건하의 multivariate scalar function의 최소화 문제, 'minimize'
Global optimizaiton: bashinhopping, differential_evolution
Least-squares minimization과 curve fitting: least_squares, curve_fit
Scalar univariate function의 최소화 또는 해 찾기: minimizer_scalar, root_scalar
Multivariate equation system의 해 찾기: root
Linear Programming: linprog