일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- DATAPATH
- DS
- github
- computer
- DoM
- Java
- web
- mysql
- system
- Class
- MacOS
- Algorithm
- MIPS
- react
- data structure
- XML
- Pipelining
- instruction
- php
- function
- javascript
- for
- while
- DB
- architecture
- python
- Linux
- control
- CSS
- html
- Today
- Total
YYYEJI
[Python] AI를 위한 Numpy 기본 사용법 본문
Numpy란?
다차원 배열을 효과적으로 처리할 수 있도록 도와주는 도구로써,
현실 세계의 다양한 데이터를 배열 형태로 표현하고 python list 기능에 비해 빠르고 강력한 기능을 제공합니다.
↓↓↓ Numpy의 차원 ↓↓↓
(0, 0) | (0, 1) | (0, 2) | (0, 3) |
(1, 0) | (1, 1) | (1, 2) | (1, 3) |
가로는 행(Row), 세로는 열(Column) 입니다.
Numpy를 사용할 때는 아래 코드를 Import 해주면 됩니다.
$ import numpy as np
np.array()
np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype = int)
array 함수에 원하는 정보를 첫번째 파라미터로 넣어주고,
dtype을 이용해서 type을 지정해줍니다.
np.zeros()
$ np.zeros((3, 5), dtype = int)
zeros()는 array(배열)안에 '0'으로 가득 채워주는 함수입니다.
np.ones()
$ np.ones((4, 4), dtype = int)
ones()는 array(배열)안에 '1'로 가득 채워주는 함수입니다.
np.random.rand()
$ np.random.rand(1, 5)
random.rand()는 0부터 1까지의 난수를 (x, y) x행 y열만큼 반환해주는 함수입니다.
np.random.randn()
$ np.random.randn(1, 5)
np.random.randn()은 평균 0과 표준편차 1인 가우시안 표준정규분포에서 난수를 (x, y) x행 y열만큼 반환해주는 함수입니다.
object.shape
ndarray = np.ones((2,4))
ndarray.shape
shape는 튜플(Tuple) 배열의 차원을 알려줍니다.
(Row, Column)
object.ndim
ndarray = np.ones((2, 4, 1))
ndarray.ndim
ndim은 array의 차원을 알려줍니다.
object.reshape()
ndarray = np.ones((2, 4))
ndarray.reshape(4, 2)
reshape는 배열의 구조를 재배열해줍니다.
ndarray = np.ones((2, 4))
ndarray.reshape(1, -1)
reshape 파라미터에 '-1'을 넣으면 랜덤으로 열(or 행)의 차원을 정해줍니다.
object.dtype
ndarray = np.ones((2, 4))
ndarray.dtype
ndarray의 데이터 타입을 알려줍니다.
np.arrange().reshape()
ndarray = np.arange(2, 22).reshape(4, 5)
ndarray
arraynge() 함수 안에는 array 안에서 증가할 값의 범위가 파라미터로 들어가며,
reshape() 함수에는 array의 차원을 지정해 줍니다.
np.dot()
ndarray1 = np.arange(2, 22).reshape(4, 5)
ndarray2 = np.arange(0, 5).reshape(5, 1)
ndarray3 = np.dot(ndarray1, ndarray2)
ndarray3
dot() 함수는 두 행렬의 내적을 구해주는 함수입니다.
object.T
ndarray1 = np.arange(2, 22).reshape(4, 5)
ndarray2 = np.arange(0, 5).reshape(5, 1)
ndarray3 = np.dot(ndarray1, ndarray2)
ndarray3.T
T는 transpose의 약자로 행렬을 전치시키는 역할을 합니다.
np.add()
ndarray1 = np.arange(2, 22).reshape(4, 5)
ndarray2 = np.arange(0, 5).reshape(5, 1)
ndarray3 = np.dot(ndarray1, ndarray2)
ndarray4 = ndarray3.reshape(2, 2)
np.add(ndarray4, ndarray4)
add()는 두 행렬을 더해주는 함수입니다.
np.subtract()
ndarray1 = np.arange(2, 22).reshape(4, 5)
ndarray2 = np.arange(0, 5).reshape(5, 1)
ndarray3 = np.dot(ndarray1, ndarray2)
ndarray4 = ndarray3.reshape(2, 2)
np.subtract(ndarray4, ndarray4.T)
subtract()는 두 행렬을 빼주는 함수입니다.
np.multiply()
ndarray1 = np.arange(2, 22).reshape(4, 5)
ndarray2 = np.arange(0, 5).reshape(5, 1)
ndarray3 = np.dot(ndarray1, ndarray2)
ndarray4 = ndarray3.reshape(2, 2)
np.multiply(ndarray4, ndarray4.T)
multiply()는 두 행렬을 곱하는 함수입니다.
np.divide()
ndarray1 = np.arange(2, 22).reshape(4, 5)
ndarray2 = np.arange(0, 5).reshape(5, 1)
ndarray3 = np.dot(ndarray1, ndarray2)
ndarray4 = ndarray3.reshape(2, 2)
np.divide(ndarray4, ndarray4.T)
divide()는 두 행렬을 나누는 함수입니다.
ndarray1 < 10
ndarray1 = np.arange(2, 22).reshape(4, 5)
ndarray1
index = ndarray1 < 10
index
부등호를 통해 조건을 정해주면 True/False로 결과가 출력됩니다.
ndarray1[index]
ndarray1 = np.arange(2, 22).reshape(4, 5)
index = ndarray1 < 10
ndarray1[index]
index 조건에 맞는 값을 출력해줍니다.
◡̈