vectorize¶
In [1]:
import numpy as np
matrix= np.arange(1,10).reshape(3,3)
print(matrix)
In [2]:
add_100= lambda i:i+100
In [3]:
vectorized_add_100= np.vectorize(add_100)
vectorized_add_100(matrix)
Out[3]:
In [4]:
find_odd = lambda i:i%2*i
vectorized_find_odd= np.vectorize(find_odd)
vectorized_find_odd(matrix)
Out[4]:
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]:
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)) # 각 열에서 최소값 찾음
In [7]:
vector_column= np.max(matrix, axis=1, keepdims=True)
print(vector_column)
print(vector_column.shape)
In [8]:
print(matrix.shape)
print(matrix-vector_column)
평균, 분산, 표준편차¶
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))
In [14]:
print("평균 : ", np.mean(matrix))
print("평균 : ",np.mean(matrix,axis=0)) #각 열의 평균
In [15]:
print(np.std(matrix))
print(np.std(matrix, ddof=1))
행렬의 전치(전치행렬, Transpose)¶
In [1]:
import numpy as np
matrix= np.arange(1,10).reshape(3,3)
print(matrix)
print()
print(matrix.T)
In [23]:
np.array([[1,2,3,4,5,6,7]]).T
Out[23]:
In [24]:
matrix=([[1,1,1], [1,1,10], [1,1,15]])
print(matrix)
np.linalg.matrix_rank(matrix)
Out[24]:
inverse matrix¶
In [25]:
matrix=np.array([[1,4], [2,5]])
print(matrix)
In [26]:
#역행렬
inv_matrix= np.linalg.inv(matrix)
print(inv_matrix)
In [27]:
matrix @ inv_matrix
Out[27]:
In [28]:
matrix= np.arange(1,7).reshape(2,3)
print(matrix)
In [29]:
inv_matrix= np.linalg.pinv(matrix) #유사역행렬
print(inv_matrix)
In [30]:
matrix@inv_matrix
Out[30]:
In [31]:
print(np.round(matrix @ inv_matrix,0))
In [35]:
import random
a= np.arange(5)
print(a)
np.random.shuffle(a)
print(a)
In [38]:
import random
a= np.arange(5)
print(a)
b= np.random.permutation(a)
print(b)
In [39]:
print(np.random.permutation(5))
'Python' 카테고리의 다른 글
Python_pandas 문제 (0) | 2020.09.09 |
---|---|
Python_pandas(판다스):시리즈,데이터프레임,색인,인덱싱,sorting (0) | 2020.09.09 |
Python 기초08_matplotlib(그래프 그리기,subplots,meshgrid) (0) | 2020.09.08 |
Python 기초07_Numpy2 (0) | 2020.09.08 |
Python 기초06_Numpy (0) | 2020.09.07 |