python_vectorize

vectorize

In [1]:
import numpy as np

matrix= np.arange(1,10).reshape(3,3)
print(matrix)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
In [2]:
add_100= lambda i:i+100
In [3]:
vectorized_add_100= np.vectorize(add_100)

vectorized_add_100(matrix) 
Out[3]:
array([[101, 102, 103],
       [104, 105, 106],
       [107, 108, 109]])
In [4]:
find_odd = lambda i:i%2*i
vectorized_find_odd= np.vectorize(find_odd)

vectorized_find_odd(matrix) 
Out[4]:
array([[1, 0, 3],
       [0, 5, 0],
       [7, 0, 9]])
In [5]:
find_odd = lambda i : i if i%2==1 else 0 
find_even= find_odd= lambda i : i if i%2==0 else 0

vectorized_find_odd= np.vectorize(find_odd)
vectorized_find_odd(matrix)
Out[5]:
array([[0, 2, 0],
       [4, 0, 6],
       [0, 8, 0]])
In [6]:
import numpy as np

matrix= np.arange(1,10).reshape(3,3)
print(matrix)

print(np.max(matrix))
print()
print(np.min(matrix))
print()
print(np.min(matrix, axis=1)) # 각 행에서 최소값 찾음
print()
print(np.min(matrix, axis=0)) # 각 열에서 최소값 찾음
[[1 2 3]
 [4 5 6]
 [7 8 9]]
9

1

[1 4 7]

[1 2 3]
In [7]:
vector_column= np.max(matrix, axis=1, keepdims=True) 
print(vector_column)
print(vector_column.shape)
[[3]
 [6]
 [9]]
(3, 1)
In [8]:
print(matrix.shape)
print(matrix-vector_column)
(3, 3)
[[-2 -1  0]
 [-2 -1  0]
 [-2 -1  0]]

평균, 분산, 표준편차

In [13]:
import numpy as np

matrix= np.arange(1,10).reshape(3,3)
print(matrix)

print("평균: ",np.mean(matrix))
print("분산: ",np.var(matrix))
print("표준편차: ",np.std(matrix))
[[1 2 3]
 [4 5 6]
 [7 8 9]]
평균:  5.0
분산:  6.666666666666667
표준편차:  2.581988897471611
In [14]:
print("평균 : ", np.mean(matrix))
print("평균 : ",np.mean(matrix,axis=0)) #각 열의 평균 
평균 :  5.0
평균 :  [4. 5. 6.]
In [15]:
print(np.std(matrix))
print(np.std(matrix, ddof=1))
2.581988897471611
2.7386127875258306

행렬의 전치(전치행렬, Transpose)

In [1]:
import numpy as np 
matrix= np.arange(1,10).reshape(3,3)
print(matrix)
print()
print(matrix.T)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

[[1 4 7]
 [2 5 8]
 [3 6 9]]
In [23]:
np.array([[1,2,3,4,5,6,7]]).T
Out[23]:
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7]])
In [24]:
matrix=([[1,1,1], [1,1,10], [1,1,15]])
print(matrix)
np.linalg.matrix_rank(matrix) 
[[1, 1, 1], [1, 1, 10], [1, 1, 15]]
Out[24]:
2

inverse matrix

In [25]:
matrix=np.array([[1,4], [2,5]])
print(matrix)
[[1 4]
 [2 5]]
In [26]:
#역행렬 
inv_matrix= np.linalg.inv(matrix)
print(inv_matrix)
[[-1.66666667  1.33333333]
 [ 0.66666667 -0.33333333]]
In [27]:
matrix @ inv_matrix
Out[27]:
array([[1., 0.],
       [0., 1.]])
In [28]:
matrix= np.arange(1,7).reshape(2,3)
print(matrix)
[[1 2 3]
 [4 5 6]]
In [29]:
inv_matrix= np.linalg.pinv(matrix) #유사역행렬
print(inv_matrix)
[[-0.94444444  0.44444444]
 [-0.11111111  0.11111111]
 [ 0.72222222 -0.22222222]]
In [30]:
matrix@inv_matrix
Out[30]:
array([[1.00000000e+00, 2.22044605e-16],
       [0.00000000e+00, 1.00000000e+00]])
In [31]:
print(np.round(matrix @ inv_matrix,0))
[[1. 0.]
 [0. 1.]]
In [35]:
import random
a= np.arange(5)
print(a)
np.random.shuffle(a)
print(a)
[0 1 2 3 4]
[3 0 1 2 4]
In [38]:
import random
a= np.arange(5)
print(a) 
b= np.random.permutation(a)
print(b)
[0 1 2 3 4]
[2 4 1 0 3]
In [39]:
print(np.random.permutation(5))
[0 4 1 2 3]

+ Recent posts