블로그 카테고리 용도:
🔎 매주 KT 에이블 스쿨에서 배운 내용을 복습하기 위해
[Python library]
1. Numpy
Numpy는 행렬이나 일반적으로 대규모 다차원 배열을 쉽게 처리할 수 있도록
지원하는 Python의 Library입니다. 리스트에서 할 수 없는 행렬의 다양한 연산을
Numpy를 통해 할 수있게 되는것이죠
# 라이브러리 불러오기
import numpy as np
# 1차원 배열 만들기
a1 = [1,2,3,4,5]
b1 = np.array(a1)
b1 = [1 2 3 4 5]
# 2차원 배열 만들기
a2 = [[1.5, 2.5, 3.2],
[4.2, 5.7, 6.4]]
b2 = np.array(a2)
b2 = [[1.5 2.5 3.2]
[4.2 5.7 6.4]]
# 3차원 배열 만들기
a3 = [[[1, 3, 1],
[4, 7, 6],
[8, 3, 4]],
[[6, 2, 4],
[8, 1, 5],
[3, 5, 9]]]
b3 = np.array(a3)
# b3.shape = (2,3,3)
<용어 정리>
- axis: 배열의 각 축
(2차원에서 axis = 0 -> 행 axis = 1 -> 열>
(3차원에서 axis는 차원/행/열의 개수로 표현합니다)
예를들어 reshape(3,4,3)은 (4행,3열의 2차원 데이터가 3개 있다는 뜻입니다)
📌 Axis 0의 의미는 데이터 분석 및 모델링에서 분석 단위의 개수입니다.
- rank: 축의개수
- ndim: 행렬의 차원
- shape:축의 길이,배열의 크기,요소의 개수
[배열의 인덱싱과 슬라이싱]
배열에 있는 요소들을 조회하기 위해 인덱싱과 슬라이싱을 사용합니다.
자주 안쓰면 헷갈리니 자주쓰면서 익숙해져야 합니다.
# 첫 번째 행, 두 번째 열 요소 조회 (요소조회)
print(a[0, 1])
# 첫 번째, 두 번째 행 조회 (행 조회)
print(a[[0, 1]])
# 첫 번째, 두 번째, 세 번째 행 조회 (행 조회)
print(a[[0, 1, 2]])
# 첫 번째, 두 번째 열 조회 (행,열 조회)
print(a[:, [0, 1]])
# 두 번째 ~ 끝 행, 두 번째 ~ 끝 열 조회
print(a[1:, 1:])
[배열 연산함수]
- 더하기 : + 또는 np.add() 함수를 사용합니다.
- 빼기 : - 또는 np.subtract() 함수를 사용합니다.
- 곱셉 : * 또는 np.multiply() 함수를 사용합니다.
- 나눗셈 : / 또는 np.divide() 함수를 사용합니다.
- 제곱 : ** 또는 np.power() 함수를 사용합니다.
- 집계 : np.sum()
[추가함수]
np.argmax()를 통해 전체,행방향,열방향 최대값 인덱스를 찾을 수있습니다.
np.argmax(a) => 전체에서 최대값 인덱스 찾기
np.argmax(a,axis=0) => 행 방향 최대값 인덱스
np.argmax(a,axis=1) => 열 방향 최대값 인덱스
'KT-AIVLE' 카테고리의 다른 글
[KT-AIVLE 5주차 AI Track] 딥러닝 (0) | 2023.03.17 |
---|---|
[KT-AIVLE 4주차 AI Track] 머신러닝 (0) | 2023.03.17 |
[KT-AIVLE 3주차 AI Track] 미프,코마,웹크롤링 (0) | 2023.03.17 |
[KT-AIVLE 2주차 AI Track] Pandas Library (0) | 2023.03.17 |
[KT-AIVLE 1주차 AI Track] Git , Algorithm (0) | 2023.03.17 |