Vector and matrix mathematics
벡터의 내적(dot product) 계산
>>> a = np.array([[0,1],[2,3]], float) >>> b = np.array([2,3], float) >>> c = np.array([[1,1],[4,0]], float) >>> a array([[ 0., 1.], [ 2., 3.]]) >>> np.dot(b,a) array([ 6., 11.]) >>> np.dot(a,b) array([ 3., 13.]) >>> np.dot(a,c) array([[ 4., 0.], [ 14., 2.]]) >>> np.dot(c,a) array([[ 2., 4.], [ 0., 4.]]) >>> |
inner product, outer product, cross product 의 계산
>>> a = np.array([1,4,2], float) >>> b = np.array([2,2,1], float) >>> np.outer(a,b) array([[ 2., 2., 1.], [ 8., 8., 4.], [ 4., 4., 2.]]) >>> np.inner(a,b) 12.0 >>> np.cross(a,b) array([ 0., 3., -6.]) >>> |
linalg 라는 서브 모듈이 선형대수연산을 위한 내장 루틴.
>>> a = np.array([[4, 2, 0], [9, 3, 7], [1, 2, 1]], float) >>> a array([[ 4., 2., 0.], [ 9., 3., 7.], [ 1., 2., 1.]]) >>> np.linalg.det(a) -48.000000000000028 >>> |
eigenvalues(고유값)과 eigenvectors(고유벡터) 조회
>>> vals, vecs = np.linalg.eig(a) >>> vals array([ 8.85591316, 1.9391628 , -2.79507597]) >>> vecs array([[-0.3663565 , -0.54736745, 0.25928158], [-0.88949768, 0.5640176 , -0.88091903], [-0.27308752, 0.61828231, 0.39592263]]) >>> |
inverse of a matrix(역행렬)
>>> b = np.linalg.inv(a) >>> b array([[ 0.22916667, 0.04166667, -0.29166667], [ 0.04166667, -0.08333333, 0.58333333], [-0.3125 , 0.125 , 0.125 ]]) >>> np.dot(a,b) array([[ 1.00000000e+00, 0.00000000e+00, -2.22044605e-16], [ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]]) >>> |
Singular value decomposition(SVD, 특이값분해)
>>> a = np.array([[1, 3, 4], [5, 2, 3]], float) >>> U, s, Vh = np.linalg.svd(a) >>> U array([[-0.6113829 , -0.79133492], [-0.79133492, 0.6113829 ]]) >>> s array([ 7.46791327, 2.86884495]) >>>Vh array([[-0.61169129, -0.45753324, -0.64536587], [ 0.78971838, -0.40129005, -0.46401635], [-0.046676 , -0.79349205, 0.60678804]])
>>> |
'프로그래밍 Programming' 카테고리의 다른 글
[django] Django 설치하기 (0) | 2015.07.11 |
---|---|
[django] Django 설치하기 (0) | 2015.06.13 |
numpy - Arrays (8) (ArrayArray item selection and manipulation) (0) | 2015.04.11 |
numpy - Arrays (7) (Comparison operators and value testing) (0) | 2015.04.11 |
Apache Worker MPM 과 Prefork MPM(Multi-Processing Module) (0) | 2015.03.14 |