본문 바로가기
Python/Pandas & numpy

Numpy 배열연산1

by Mr.DonyStark 2024. 3. 9.

□ 사칙연산

arr1 = np.array([[1,2,3,4,5],[6,7,8,9,10]])
arr2 = np.array([[2,2,2,2,2],[2,2,2,2,2]])
print(f"2차원배열 arr1 : {arr1}")
print('\n')
print(f"2차원배열 arr2 : {arr2}")
print('\n')
print(f"arr1+arr2 : {arr1+arr2}")
print(f"arr1+arr2 : {np.add(arr1,arr2)}")
print('\n')
print(f"arr1-arr2 : {arr1-arr2}")
print(f"arr1-arr2 : {np.subtract(arr1,arr2)}")
print('\n')
print(f"arr1*arr2 : {arr1*arr2}")
print(f"arr1*arr2 : {np.multiply(arr1,arr2)}")
print('\n')
print(f"arr1/arr2 : {arr1/arr2}")
print(f"arr1/arr2 : {np.divide(arr1,arr2)}")
print('\n')
print(f"arr1**arr2 제곱 : {arr1**arr2}")
print(f"arr1**arr2 제곱 : {np.square(arr1)}")
print('\n')
print(f"arr1**arr2 제곱근 : {np.sqrt(arr1)}")
print('\n')
print(f"arr1/arr2 몫 : {arr1//2}") #몫
print(f"arr1/arr2 나머지 : {arr1%2}") #나머지

 

□ .dot : 행렬곱

arr1 = np.array([2,3,4])
arr2 = np.array([1,2,3])
print(f"arr1 :\n{arr1}")
print("\n")
print(f"arr2 :\n{arr2}")
print(f".dot(arr1, arr2) : {np.dot(arr1,arr2)}") #원소간의 곲셈후 합산

arr3= np.array([[1,2],[3,4]])
arr4 = np.array([[5,6],[7,8]])
print(f"arr3 :\n{arr3}")
print("\n")
print(f"arr3 :\n{arr4}")
print(f".dot(arr3, arr4) : {np.dot(arr3,arr4)}") #원소간의 곲셈후 합산

 

□ .abs : 절대값 / .ceil : 올림 / .round : 반올림 / .trunk : 버림

#절대값  : abs
arr3 = np.array([[1,2,3],[-4,-5,-6]])
print(f"절대값 abs :\n{np.abs(arr3)}")

#올림
arr4 = np.array([[1.932,2.454,3.012],[-4.59,-5.18748,-6.2476]])
print(f"올림 ceil :\n{np.ceil(arr3)}")

#내림
print(f"내림 floor :\n{np.floor(arr3)}")

#반올림
print(f"반올림 round :\n{np.round(arr3)}")

#버림
print(f"내림 trunk :\n{np.trunc(arr3)}")