Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- classifier-free guidance
- 논문 리뷰
- ddim
- colorization
- posco 채용
- controlNet
- Generative Models
- 포스코 채용
- 포스코 코딩테스트
- ip-adapter
- kt인적성
- 프로그래머스
- stable diffusion
- KT
- DDPM
- 코딩테스트
- dp
- 과제형 코딩테스트
- Image Generation
- diffusion models
- manganinja
Archives
- Today
- Total
Paul's Grit
[Python] [Data Analysis] Numpy 기초 본문
Numpy¶
- python에서 사용하는 과학 계산용 오픈 소스 라이브러리
- python의 느린 연산 속도를 보완
- ndarray라는 데이터 타입을 사용
- C, C++, 포트란으로 만들어짐
선형대수¶
- 스칼라(scalar)
- 하나의 숫자로만 이루어진 데이터
- 예) 1
- 벡터(vector)
- 여러개의 숫자가 특정한 순서대로 모여 있는 것
- 예) [1, 2, 3]
- $\mathbf{v}=\begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}$
- 행렬(matrix)
- 2차원 배열로 구성
- 행(rows)과 열(columns)
- 예) [[1, 2, 3], [4, 5, 6]]
In [1]:
import numpy as np
01. ndarray 객체 생성¶
In [4]:
scalar = np.array(5)
type(scalar)
Out[4]:
numpy.ndarray
In [8]:
vector = np.array([1, 2, 3])
vector
Out[8]:
array([1, 2, 3])
In [10]:
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix
Out[10]:
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [14]:
# 배열의 차원 확인
print(scalar.ndim, vector.ndim, matrix.ndim)
0 1 2
In [15]:
# 배열의 모양 확인
print(scalar.shape, vector.shape, matrix.shape)
() (3,) (3, 3)
In [19]:
# 문자열도 배열 데이터 생성이 가능
str_vector = np.array(list('ABCD'))
str_vector
Out[19]:
array(['A', 'B', 'C', 'D'], dtype='<U1')
In [21]:
# numpy는 한 가지 데이터 타입으로만 인식
ls = [1, 2, 3, 'D']
np.array(ls)
Out[21]:
array(['1', '2', '3', 'D'], dtype='<U21')
In [24]:
# .astype()으로 데이터 타입 변경
print(np.array([1, 2, 3, '4']).astype(np.float64))
print(np.array([1, 2, 3, '4']).astype(np.int64))
[1. 2. 3. 4.] [1 2 3 4]
02. np.arang()¶
In [30]:
print(list(range(5)))
print(list(np.arange(5)))
[0, 1, 2, 3, 4] [0, 1, 2, 3, 4]
In [34]:
print(list(np.arange(1, 5)))
print(list(np.arange(1, 5, 2))) # stride = 2
[1, 2, 3, 4] [1, 3]
03. np.zeros(), np.ones()¶
- np.zeros() : 0으로 채워진 데이터
- np.ones() : 1로 채워진 데이터
- 초기화 값을 지정하는 경우 활용
In [35]:
mat_zero = np.zeros((2, 2, 3))
print(mat_zero)
[[[0. 0. 0.] [0. 0. 0.]] [[0. 0. 0.] [0. 0. 0.]]]
In [36]:
mat_ones = np.ones((2, 2, 3))
print(mat_ones)
[[[1. 1. 1.] [1. 1. 1.]] [[1. 1. 1.] [1. 1. 1.]]]
04. np.random()¶
In [137]:
# 정수 데이터 타입을 균일분포로부터 생성
np.random.randint(10, size = (2, 3)) # 0~9
Out[137]:
array([[7, 8, 9], [1, 0, 8]])
In [166]:
# seed
# seed 값을 고정하면 동일한 출력
np.random.seed(10)
np.random.randint(0, 10, size = 10)
Out[166]:
array([9, 4, 0, 1, 9, 0, 1, 8, 9, 0])
In [224]:
np.random.choice(5, 10) # 5가 end point (5 포함x)
Out[224]:
array([3, 1, 3, 2, 1, 2, 0, 4, 2, 2])
In [241]:
# 0~4 숫자 중 한 개 선택
# 반환할 샘플의 개수, 10개
# 선택 확률 지정
# 확률은 선택할 수 있는 값의 배열과 길이가 동일해야 합니다.
np.random.choice(5, 10, p = [0.1, 0, 0.3, 0.6, 0])
Out[241]:
array([2, 3, 3, 3, 3, 0, 3, 3, 2, 3])
05. np.linspace()¶
np.linspace(start, stop, num = 50, endpoint = True)
- 시작값과 종료값 사이에서 지정된 개수만큼 균등간격으로 선형적으로 분포하는 값 생성
- endpoint: True로 설정하면, stop 값을 배열에 포함
In [249]:
np.linspace(10, 20, 11)
Out[249]:
array([10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.])
06. ndarray 객체에서 데이터 선택¶
- 오프셋 인덱스, 인덱싱, 슬라이싱
In [257]:
vec = np.random.randint(10, size =10)
print(vec)
[2 8 5 8 0 9 7 7 4 0]
In [259]:
# 처음부터 시작하고, 3번째 인덱스 값 까지 선택해주세요
print(vec[:3])
[2 8 5]
In [261]:
# 역순에서 3번째 값부터 시작하고, 끝가지 선택해주세요
print(vec[-3:])
[7 4 0]
In [262]:
# 역순에서 4번째 값을 선택해주세요
print(vec[-4])
7
In [263]:
# 처음부터 끝까지 역순으로 2단계씩 건너띄어 주세요
print(vec[::-2])
[0 7 9 8 8]
행렬에서 슬라이싱 (1)¶
In [268]:
mat = np.random.randint(5, size = (3, 3))
print(mat)
[[1 3 3] [0 2 0] [4 1 3]]
In [272]:
print(mat[0][1])
print(mat[0, 1])
3 3
행렬에서 슬라이싱 (2)¶
In [279]:
# 1~25까지의 정수가 담긴 열 벡터 생성
vec = np.arange(1, 26)
print(vec)
print(len(vec))
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25] 25
In [282]:
mat = vec.reshape(5, 5)
print(mat)
[[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15] [16 17 18 19 20] [21 22 23 24 25]]
In [298]:
# 세번째 행 전체 선택
print(mat[2])
print(mat[2,:])
[11 12 13 14 15] [11 12 13 14 15]
In [299]:
# 세번째 열 전체 선택
print(mat[:, 2])
[ 3 8 13 18 23]
In [302]:
# 두번째 행부터 세번째 행
# 두번째 열부터 세번째 열
print(mat[1:3, 1:3])
[[ 7 8] [12 13]]
In [345]:
# 행을 역순으로 변환
print(mat[::-1, :])
[[21 22 23 24 25] [16 17 18 19 20] [11 12 13 14 15] [ 6 7 8 9 10] [ 1 2 3 4 5]]
In [346]:
# 행렬 전체를 역순으로 변환
print(mat[::-1, ::-1])
[[25 24 23 22 21] [20 19 18 17 16] [15 14 13 12 11] [10 9 8 7 6] [ 5 4 3 2 1]]
07. 행을 역순으로 변환¶
np.reshape(a, newshape)
- a : 형태를 변경할 배열
- newshape : 새로운 형태로 재구성할 배열의 차원을 지정하는 튜플
- (6, 2)는 6행 2열
- (3, 4)는 3행 4열
In [324]:
a = np.arange(9)
print(a)
print(len(a))
print(a.reshape(3, 3))
[0 1 2 3 4 5 6 7 8] 9 [[0 1 2] [3 4 5] [6 7 8]]
In [319]:
a_reshape = np.reshape(a, (3, 3))
print(a_reshape)
[[0 1 2] [3 4 5] [6 7 8]]
In [321]:
# 차원이 맞지 않으면 에러
b = np.arange(10)
b.reshape(3, 3)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[321], line 3 1 # 차원이 맞지 않으면 에러 2 b = np.arange(10) ----> 3 b.reshape(3, 3) ValueError: cannot reshape array of size 10 into shape (3,3)
In [328]:
# -1: 행을 3개로 설정하는데, 열은 자동으로 설정
a = np.arange(9)
print(a)
print(a.reshape(3, -1))
[0 1 2 3 4 5 6 7 8] [[0 1 2] [3 4 5] [6 7 8]]
In [368]:
image = np.random.randint(0, 256, size = (32, 32, 3), dtype = np.uint8)
print(image.shape)
(32, 32, 3)
In [382]:
# 이미지 시각화 예시
import matplotlib.pyplot as plt
image = np.random.randint(0, 256, size = (32, 32, 3), dtype = np.uint8)
plt.imshow(image)
plt.show()
In [378]:
# 이미지 데이터 reshape 예시
image.reshape(32*32, 3)
Out[378]:
array([[247, 28, 165], [155, 9, 53], [ 58, 9, 126], ..., [ 67, 209, 70], [100, 195, 102], [ 31, 129, 155]], dtype=uint8)
08. np.where()¶
np.where(condition, [x, y])
- 조건에 따라 값을 선택
- condition
- 조건을 나타내는 배열 또는 조건문
- True or False
- x, y는 서로 동일한 크기(shape) 배열 형태
- condition이 True인 경우 x 값 반환
- condition이 False인 경우 y 값 반환
In [351]:
condition = [True, False, True]
x = [1, 2, 3]
y = [10, 20, 30]
np.where(condition, x, y)
Out[351]:
array([ 1, 20, 3])
In [388]:
# np.where()를 반복문으로 구현
result = []
for con, x_value, y_value in zip(condition, x, y):
if con:
result.append(x_value)
else:
result.append(y_value)
print(result)
[1, 20, 3]
In [389]:
# list comhrehension과 zip()을 사용한 구현
result = [xv if con else yv for con, xv, yv in zip(condition, x, y)]
print(result)
[1, 20, 3]
'Data Analysis > Pandas' 카테고리의 다른 글
[Python] [Data Analysis] Pandas를 활용한 서울시 범죄 데이터 전처리 (1) | 2023.08.08 |
---|---|
[Python] [Data Analysis] Pandas DataFrame 가지고 놀기 (1) | 2023.08.07 |
[Python] [Data Analysis] Pandas 기초 (3) | 2023.08.04 |