# concatenate : numpy배열을 하나로 합치는 기능

import numpy as np
array1 = np.array([1,2,3])
array2 = np.array([4,5,6])
array3 = np.concatenate([array1, array2])
print(array3)

array([1, 2, 3, 4, 5, 6])

 

# concatenate는 보통 컬럼과 컬럼,

로우와 로우가 크기가 맞을 때 또는 맞춰서 사용한다.

array1 = np.array([1,2,3])
array2 = np.array([4,5,6])

# 1행3열의 2차원 배열로 변경
array4 = array1.reshape(1,3)
array5 = array2.reshape(1,3)

# 위 아래를 합쳐준다.
array6 = np.concatenate([array4, array5],axis=0)
print(array6.shape)
print(array6)

 

# axis=0  : vstack (위아래로 결합)

   axis=1  : hstack (옆으로 결합)

   axis=None (한 개의 리스트로 결합)

array1 = np.array([[1,2],[3,4]])
array2 = np.array([[7,8],[9,10],[11,12]])
array3 = np.concatenate([array1,array2],axis=0)
array3

array([[ 1,  2],
       [ 3,  4],
       [ 7,  8],
       [ 9, 10],
       [11, 12]])
array1 = np.array([[1,2],[3,4]])
array2 = np.array([[7,8],[9,10],[11,12]])
print(array2.T)
array3 = np.concatenate([array1,array2.T],axis=1)
array3

[[ 7  9 11]
 [ 8 10 12]]
array([[ 1,  2,  7,  9, 11],
       [ 3,  4,  8, 10, 12]])
array1 = np.array([[1,2],[3,4]])
array2 = np.array([[7,8],[9,10],[11,12]])
array3 = np.concatenate([array1,array2],axis=None)
array3

array([ 1,  2,  3,  4,  7,  8,  9, 10, 11, 12])

 

# np.where() 메소드 *****
# 특정조건을 입력받아서 해당 조건을 만족하는 요소의 인덱스를 반환
# 오라클의 decode 함수에 해당 (MySQL 에는 없다.)

a = np.array(['남','여','남','여','여'])
res = np.where(a == '남',1,2)
print(res)

array([1, 2, 1, 2, 2])
arr = np.array([11,12,13,14,15,16,17,15,11,12,14,15,16,17])
result = np.where((arr > 12) & (arr <16))
print(result)

(array([ 2,  3,  4,  7, 10, 11]),)

 

# np.select() 메서드 *****
# 조건 목록과 선택목록을 입력받아 조건에 따라 선택목록의 요소로 구성된 배열을 반환한다.
# 특정조건을 리스트로 만들 수 있다. ----- 핵심

a = np.array(['AClass','BClass','Class','AClass','BClass','AClass','Class'])
clist = [(a == 'AClass'), (a == 'BClass'),(a == 'Class')]
achoice = [1,2,3] # 해당값이 없으면 0 으로 처리한다.
res = np.select(clist, achoice)
print(res)

array([1, 2, 3, 1, 2, 1, 3])
a = np.array(['AClass','BClass','Class','AClass','BClass','AClass','Class'])
clist = [(a == 'AClass'), (a == 'Blass'),(a == 'Class')]
achoice = [1,2,3] # 해당값이 없으면 0 으로 처리한다.
res = np.select(clist, achoice)
print(res)

array([1, 0, 3, 1, 0, 1, 3])

 

# 행렬연산

 

# 더하기
x = np.array([10,11,12])
y = np.array([0,1,2])
z = x+y
z

array([10, 12, 14])
# 곱하기
np.array([1,2,3,4,5]) *10

array([10, 20, 30, 40, 50])
np.ones((3,3)) + 10

array([[11., 11., 11.],
       [11., 11., 11.],
       [11., 11., 11.]])

# 일반적으로 Numpy에서 모양이 다른 배열끼리는 연산이 불가능하다.

 

# CASE1 : 1차원배열, 멤버가 하나인 배열은 어떤 배열에서도 브로드캐스팅이 가능하다.
case1= np.array([[1,2,3,4,5],[2,5,6,7,5],[8,9,10,11,5],[12,13,14,15,15],[16,17,18,19,19]])
case1.shape

(5, 5)
case1_1 = np.array([1])
case1_1.shape
(1,)

case1 + case1_1
array([[ 2,  3,  4,  5,  6],
       [ 3,  6,  7,  8,  6],
       [ 9, 10, 11, 12,  6],
       [13, 14, 15, 16, 16],
       [17, 18, 19, 20, 20]])
case2 = np.array([3,3,3,3,3])
case1 + case2

array([[ 4,  5,  6,  7,  8],
       [ 5,  8,  9, 10,  8],
       [11, 12, 13, 14,  8],
       [15, 16, 17, 18, 18],
       [19, 20, 21, 22, 22]])
       
** 자릿수가 맞아야 한다. [3,3,3,3]  은 에러
ValueError: operands could not be broadcast together with shapes (5,5) (4,) 
case1= np.array([[1,2,3,4,5],[2,5,6,7,5],[8,9,10,11,5],[12,13,14,15,15],[16,17,18,19,19]])
case2 = np.array([[3],[3],[3],[3],[3]])
case1 + case2

array([[ 4,  5,  6,  7,  8],
       [ 5,  8,  9, 10,  8],
       [11, 12, 13, 14,  8],
       [15, 16, 17, 18, 18],
       [19, 20, 21, 22, 22]])

# 행렬의 내적을 구할 때나 브로드캐스트 할때에도 
# (2 x 3) (3 x 1) 이런 식으로 맞아야 연산이 가능하다.

case3 = np.array([1,2,3,4,5])
case1+case3
array([[ 2,  4,  6,  8, 10],
       [ 3,  7,  9, 11, 10],
       [ 9, 11, 13, 15, 10],
       [13, 15, 17, 19, 20],
       [17, 19, 21, 23, 24]])
np.dot(case1,case3)
array([ 55,  83, 125, 215, 275])

 

 

 

 

a 를 가중치, b 를 값 배열이라고 했을 때, 

 

(1) 내적을 이용한 가중합 계산: np.dot(a, b) or np.matmul(a, b)

(2) 브로드캐스팅(broadcasting)을 이용하여 가중치와 값을 원소끼리 곱한 후 합하는

     np.sum(a.reshape(5, 1) * b, axis=0)

(3) repeat()로 가중치를 값 배열 1축만큼 반복 생성한 후, 가중치와 값의 원소끼리 곱한 후 합하는, 

     np.sum(a.reshape(5, 1).repeat(3, axis=1) * b, axis=0)



출처: https://rfriend.tistory.com/tag/np.dot() [R, Python 분석과 프로그래밍의 친구 (by R Friend)]

 

 

 

n1 = np.ones((3,3))
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
       
n2 = np.array([1,2,3,4,5,6])
array([1, 2, 3, 4, 5, 6])

np.dot(n1,n2)
# error
ValueError: shapes (3,3) and (6,) not aligned: 3 (dim 1) != 6 (dim 0)

n3 = n2.reshape(3,2)
array([[1, 2],
       [3, 4],
       [5, 6]])

np.dot(n1,n3)
array([[ 9., 12.],
       [ 9., 12.],
       [ 9., 12.]])

 

'PYTHON' 카테고리의 다른 글

Pandas  (0) 2021.07.01
0629 시험대비  (0) 2021.06.28
한글폰트 설정  (0) 2021.06.28
Numpy  (0) 2021.06.24
Selenium _ 0623  (0) 2021.06.23

+ Recent posts