[딥러닝] Numpy 맛보기

Chris Kim·2024년 11월 12일

딥러닝

목록 보기
1/5

numpy 배열 연산

# 1.5.1 numpy 가져오기
import numpy as np

# 1.5.2 넘파이 배열 생성하기

x = np.array([1.0, 2.0, 3.0])
print(x) 
print(type(x))


# 1.5.3 넘파이 산술 연산
x = np.array([1.0, 2.0, 3.0])
y = np.array([2.0, 4.0, 6.0])
print(x+y)
print(x-y)
print(x*y)
print(x/y)  
print(x/2.0)

# 1.5.4 넘파이의 N차원 배열
A = np.array([[1, 2],[3, 4]])
print(A)
print(A.shape)
print(A.dtype)

B = np.array([[3, 0],[0, 6]])
print(A + B)
print(A * B) #별(*) 연산이다. dot product, 행렬곱이 아니다

# 1.5.5 브로드캐스트
A = np.array([[1, 2],[3, 4]])
B = np.array([10, 20])
print(A * B)

# 1.5.6 원소 접근
X = np.array([[51, 55], [14, 19], [0, 4]])
print(X)
print(X[0])
print(X[0][1])

for row in X:
    print(row)

X = X.flatten() # X를 1차원 배열로 변환
print(X)

print(X[np.array([0, 2, 4])]) # 인덱스를 통한 접근

print(X>15) #[ True  True False  True False False]
print(X[X>15]) # [51 55 19]

matplotlib.pyplot: 그래프 그리기

# 1.6.1 단순한 그래프 그리기
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 6, 0.1) # 0에서 6까지 간격 0.1 설정
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle="--", label="cos")

plt.xlabel("x") #x축 이름
plt.ylabel("y") #y축 이름
plt.title('sin & cos')
plt.legend() # 범례
plt.show()


matplotlib.image: 이미지 가져오기

import matplotlib.pyplot as plt
from matplotlib.image import imread

img = imread('./test.png')

plt.imshow(img)
plt.show()
profile
회계+IT=???

0개의 댓글