Code of Vector

매일 공부(ML)·2021년 11월 14일
0
import numpy as np

# column vector
c = np.array([1,2,3]) 
print(c.shape)

# obtaining a particular entry
print (c[0])

# row vector
r = np.array([ [1,2,3] ])
print (r.shape) # 2-dimenison

# obtaining a particular entry
print (r[0,1])

# creating a matrix with all zeros , 디폴트 행렬
a = np.zeros((2,2))
print (a)
# creating a matrix with all ones
b = np.ones((2,2))
print (b)
             
# creating a matrix filled with the same constant
c = np.full((2,2), 7)
print (c)
             
# creating a matrix with random values
d = np.random.random((2,2))
print (d)

# creating a matrix
A=np.array([[1,2],[3,4],[5,6]])
print (A)


# creating another matrix
B=np.array([[11,12,13,14],[15,16,17,18]])
B

# transpose a matrix
A.T


# matrix-matrix multiplication
np.dot(A,B)

# coefficient matrix A and a vector b
A=np.array([[60, 5.5, 1],[65, 5.0, 0],[55, 6.0, 1]])
b=np.array([66, 70, 78])

# identity matrix = eye
eye3 = np.eye(3)
eye3


# computing an inverse
from numpy.linalg import inv # function of inverse matrix
A_inv = inv(A)
A_inv


# wrong matrix multiplication
A*A_inv

# correct matrix multiplication
A.dot(A_inv)

# solution of a linear system
# 대부분의 경우 이렇게 하지 않는다. 계산상의 오류가 커질 수 있기 때문입니다.
x=A_inv.dot(b)
x

Numerical stability

역수 취할 때 숫자가 맞아 떨어지면 좋으나 그렇지 못한 무한소수가 되는 경우 우리는 근사치를 구해야합니다. 그때 주는 미묘한 차이가 우리가 보기엔 작아서 영향을 크지 않다고 생각할 수도 있으나 막상 알고리즘에겐 그렇지 않습니다

즉,역수-> 근사치로 인해서 생기는 문제입니다.

# a better way to solve the same linear system
# solve를 사용하면 dot를 한 줄을 안 써도 됨
from numpy.linalg import solve
x = solve(A,b)
x
profile
성장을 도울 아카이빙 블로그

0개의 댓글