maxtrixA.shape : 벡터의 사이즈 row,col 순서대로.
import numpy as np
A = np.array([[0,1,-2.3,0.1], [1.3, 4, -0.1, 0], [4.1,-1.0, 0, 1.7]])
A.shape
m,n= A.shape
print("row #: " + str(m) + ", cor #: " + str(n))
shape비교 by using a function
bool값 리턴한다.
wide = lambda x: x.shape[0] < x.shape[1]
wide(A)
indexing entries
A = [[0,1,-2.3,0.1], [1.3, 4, -0.1, 0], [4.1,-1.0, 0, 1.7]]
print("A[0,2]: " + str(A[0][2]) + ", A[1,2]: " + str(A[1][2]))
* shallow copy & deep copy 주의
copy()메소드로는 얕은복사만.
element-wise하게 요소 하나하나에 대한 bool값 리턴함.
A = np.array([[0,1,-2.3,0.1], [1.3, 4, -0.1, 0], [4.1,-1.0, 0, 1.7]])
B = A.copy()
A == B
B[2,1] = 2022
A == B
*reshape
A = np.array([-2.1, -3, 0])
print("A shape: " + str(A.shape))
B = np.array([[-2.1], [-3], [0]])
print("B shape: " + str(B.shape))
C = np.array([[-2.1, -3, 0]])
print("C shape: " + str(C.shape))
D = B.reshape(3)
print("D shape: "+ str(D.shape))
block matrices : numpy.block() is used to create a block matrices
B = np.array([0,2,3])
C = np.array([-1])
D = np.array([[2,2,1], [1,3,5]])
E = np.array([[4],[4]])
A = np.block([[B,C],[D,E]])
print(A)
[[ 0 2 3 -1]
[ 2 2 1 4]
[ 1 3 5 4]]
np.vstack(vertical, 세로로 쌓자!) / np.hstack(horizontal, 가로로 쌓자!)
a = [1,2]
b = [4,5]
c = [7,8]
A = np.vstack([a,b,c])
B = np.hstack([a,b,c])
print('matrix A :\n', A)
print('dimension of A :', A.shape)
print('matrix B :\n', B)
print('dimension of B: ', B.shape)
print()
a = [[1], [2]]
b = [[4], [5]]
c = [[7], [8]]
A = np.vstack([a,b,c])
B = np.hstack([a,b,c])
print('matrix A :\n', A)
print('dimension of A :', A.shape)
print('matrix B :\n', B)
print('dimension of B: ', B.shape)
matrix A :
[[1 2]
[4 5]
[7 8]]
dimension of A : (3, 2)
matrix B :
[1 2 4 5 7 8]
dimension of B: (6,)
matrix A :
[[1]
[2]
[4]
[5]
[7]
[8]]
dimension of A : (6, 1)
matrix B :
[[1 4 7]
[2 5 8]]
dimension of B: (2, 3)
numpy.c_ (column) / numpy.r_ (row)
np.c_[-np.identity(3), np.zeros(3)]
array([[-1., -0., -0., 0.],
[-0., -1., -0., 0.],
[-0., -0., -1., 0.]])
np.r_[np.identity(3), np.zeros((1,3))]
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])
diagonal matrices
x = np.array([[0,1,2],
[3,4,5],
[6,7,8]])
#applying the np.diag() function on a matrix.
#it extracts the diagonal elements as a vector(array)
print(np.diag(x))
print()
#applying the np.diag() function on a vector(array).
#it constructs a diagonal matrix with diagonal entries in the vector.
print(np.diag(np.diag(x)))
[0 4 8]
[[0 0 0]
[0 4 0]
[0 0 8]]
random : creating matrices with elements of random value
- uniform random (원하는 자료 tuple 형태로)
import numpy as np
np.random.random((2,3))
array([[0.48947279, 0.0510617 , 0.99070287],
[0.06952147, 0.93134739, 0.64226201]])
- random with standard normal(표준정규분포)
np.random.randn(3,2)
array([[ 1.39739781, 2.54093 ],
[ 3.12739034, -0.07259194],
[ 1.46701084, -0.49630077]])
sparse diagonal matrices (희소 대각 행렬)
diagonals = [[1,2,3,4], [1,2,3], [1,2]]
B = sparse.diags(diagonals, offsets=[0,-1,2])
B.todense()
matrix([[1., 0., 1., 0.],
[1., 2., 0., 2.],
[0., 2., 3., 0.],
[0., 0., 3., 4.]])
transpose
import numpy as np
A = np.array([[0,1,-2,1],[2,-1,3,0]])
A
array([[ 0, 1, -2, 1],
[ 2, -1, 3, 0]])
A.T
np.transpose(A)
array([[ 0, 2],
[ 1, -1],
[-2, 3],
[ 1, 0]])
'Major > Linear Algebra' 카테고리의 다른 글
K-means algorithm (0) | 2022.04.15 |
---|---|
Vector with python 01 (0) | 2022.03.08 |
Vector notation & operations & my proof (0) | 2022.03.03 |