In [1]:
import numpy as np
In [2]:
import numpy as np
A= np.arange(0,15,2)
print(A)
In [6]:
print(A.shape)
for i in range(A.size):
print("A[{}] : {}".format(i,A[i]))
In [8]:
# -1부터 -8까지 나오도록 출력
print(A.shape)
for i in range(A.size):
print("A[{}] : {}".format(-(i+1),A[-(i+1)]))
In [21]:
import numpy as np
A= np.arange(12).reshape(3,4)
print(A)
print("A.ndim:",A.ndim)
print(A.shape, A.shape[0], A.shape[1])
for i in range(A.shape[0]):
for j in range(A.shape[1]):
print("A[{0}][{1}] : {2} ".format(i,j,A[i][j]), end=" ")
print()
print()
for i in range(A.shape[0]):
print("A[{0}][{1}] : {2:2d}". format(i,j,A[i][j]), end=" ")
In [106]:
import numpy as np
A= np.arange(9).reshape(3,3)
print(A)
print()
for row in A: #A => [[0.1,2], [3,4,5], [6,7,8]]
print(row)
print()
print(A.T) #전치행렬: 행과 열의 값을 바꿈
print()
for column in A.T:
print(column)
flat¶
In [26]:
import numpy as np
A=np.arange(9).reshape(3,3)
print(A)
print()
for a in A.flat: #개별 원소로 바뀜
print(a, end=" ")
In [108]:
import numpy as np
A= np.arange(0,20,2) #1차원 배열 생성
print(A)
In [38]:
print(A[:]) #전체원소
print(A[0:3])
print(A[:3]) # 0에서 시작하면 생략하능
In [37]:
print(A[7:10])
print(A[7:]) # 끝 인덱스 생략가능
In [110]:
print(A[::2]) #첫번째 원소부터 2씩 건너뜀
In [48]:
print(A[:-2]) #마지막 2개 원소 제외
print(A[-10:-2])
print(A[-2:]) #마지막 원소 2개만 취함
In [43]:
print(A[0:3].shape)
print(A[0:3].ndim)
In [49]:
print(A)
A[0:3]=100
print(A)
In [102]:
import numpy as np
A=np.arange(1,13).reshape(3,4)
print(A)
In [104]:
for i in range(A.shape[0]):
for j in range(A.shape[1]):
print("A[{0}][{1}] : {2:2d} or A[{3}, {4}] : {5:2d}".format(i, j, A[i][j], i, j, A[i, j]))
In [55]:
for i in range(A.shape[0]):
print("A[{0}] : {1} or A[{2},:] : {3}".format(i,A[i],i,A[i,:]))
In [56]:
print(A)
In [57]:
print(A[:2,:2])
In [58]:
A[:2,:2]=0
print(A)
In [111]:
import numpy as np
A=np.arange(1,13).reshape(3,4)
print(A)
In [112]:
print(A[0])
print(A[:,1])
print(A[...,1])
In [65]:
import numpy as np
A=np.array([2,4,6,8]).reshape(2,2)
B=np.array([2,2,2,2]).reshape(2,2)
print(A)
print(B)
In [67]:
print(A+B)
print()
print(A-B)
print()
print(A*B)
print()
print(A/B)
In [68]:
print(A@B)
In [69]:
print(np.dot(A,B))
print(A.dot(B))
다른 크기의 배열간의 산술연산(브로드캐스팅)¶
A.shape= (2,3,4,5)
B.shape= (5,) => B의 차원의 개수가 같아지도록 B shape 왼쪽에 1추가
B.shape= (1,1,1,5)
- 두 배열을 비교
- 대응하는 차원이 같거나 한쪽이 1인 경우에만 브로드캐스팅 가능
참고) 행렬 곱셈 규칙 : 행렬(m x n) x 행렬(n x a) -> (m xa) 행렬
브로드캐스팅해서 행렬곱셈은 적용안됨..
1Q
A.shape : (2,1,3,1)
B.shape : (6,1,3)
=> 브로드캐스팅이 가능한가?
1A)
B의 차원의 개수가 같아지도록 함
A.shape : (2,1,3,1)
B.shape : (6,1,3)
B.shape : (1,6,1,3)
-> 가능하다.
2Q)
A.shape : (2,3,4)
B.shape : (3,1)
=> 브로드캐스팅이 가능한가?
2A)
A.shape : (2,3,4)
B.shape : (3,1)
B.shape : (1,3,1)
-> 가능하다
3Q)
A.shape : (3,)
B.shape : (4,)
=> 브로드캐스팅이 가능한가?
3A)
A.shape : (3,)
B.shape : (4,)
-> 브로드캐스팅 불가능
4Q)
A.shape : (3,3,5)
B.shape : ()
=> 브로드캐스팅이 가능한가?
4A)
A.shape : (3,3,5)
B.shape : (1,1,1)
-> 브로드캐스팅 가능
+
A.shape: (3,4)
B.shape: (3,) => (1,3)
-> 브로드캐스팅 NO
In [125]:
import numpy as np
A=np.arange(10,130,10).reshape(3,4)
print(A)
print(A.shape)
print()
B=np.arange(1,5)
print(B)
print(B.shape)
In [126]:
A+B
Out[126]:
In [138]:
import numpy as np
A=np.arange(10,130,10).reshape(3,4)
print(A)
print(A.shape)
print()
B=np.arange(1,4)
print(B)
print(B.shape)
In [139]:
A+B #브로드캐스팅 no
In [133]:
import numpy as np
A= np.arange(10,130,10).reshape(3,4)
print(A)
print()
B=5
A+B
Out[133]:
In [129]:
A*B
Out[129]:
In [134]:
import numpy as np
A = np.arange(10,130,10).reshape(3,4)
print(A)
print(A.shape)
B = np.arange(1,5)
print(B)
print(B.shape)
In [136]:
A*B
Out[136]:
In [135]:
A@B
Out[135]:
In [164]:
B= np.array([1, 2, 3])
print(B.shape)
In [165]:
A= np.array([[1], [2], [3]])
print(A.shape)
In [174]:
C= np.array([[[1, 2, 3, 4], [4, 5, 6,7],[1, 2, 3, 4]], [[1, 2, 3, 4],[1, 2, 3, 4], [4, 5, 6,7]], ])
print(C.shape)
In [179]:
A*C
Out[179]:
In [181]:
B*C
In [182]:
C*B
In [183]:
C*A
Out[183]:
'Python' 카테고리의 다른 글
Python 기초09_vectorize (0) | 2020.09.08 |
---|---|
Python 기초08_matplotlib(그래프 그리기,subplots,meshgrid) (0) | 2020.09.08 |
Python 기초06_Numpy (0) | 2020.09.07 |
Python 기초05 (0) | 2020.09.07 |
Python기초04_ 텍스트파일에서 특정단어를 원하는 단어로 바꾸기 (0) | 2020.09.06 |