Notice
Recent Posts
Recent Comments
Link
관리 메뉴

뛰는 놈 위에 나는 공대생

Numpy에 대하여 본문

프로그래밍 Programming

Numpy에 대하여

보통의공대생 2020. 9. 23. 22:21

Numpy : python의 라이브러리 중 하나로 고성능 수치 계산에 유용하다. N차원 배열 객체를 제공한다.

Numpy에 대한 explanation : numpy.org/doc/stable/search.html

 

Search — NumPy v1.19 Manual

Search Please activate JavaScript to enable the search functionality. From here you can search these documents. Enter your search words into the box below and click "search". Note that the search function will automatically search for all of the words. Pag

numpy.org

나처럼 아마추어 코딩하는 사람에게 manual과 google은 아주 고마운 존재다.

특히 manual의 장점은, 함수의 input의 data type 및 종류를 알려주고, output이 어떤 형태로 나올지 알려준다는 장점이 있다. 보통 일반 사람들이 쓴 글에 이런 세세한 부분까지 커버해주기는 힘들기 때문.

 

1. 배열 생성

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
 
= np.array([1,2,3,4]) #배열 array([1,2,3,4]) 생성
= np.array([[1,2],
              [3,4]])
= np.zeros(5, dtype=int)
= np.ones((2,3), dtype=complex)
= np.arange(0192#일정한 간격으로 array를 만들고 싶을 때
= np.linspace(0206#특정 범위 안에서 정해진 원소 갯수의 array를 만들고 싶을 때
= np.random.random((2,2)) # [0,1) 사이의 랜덤값을 뽑아 정해진 사이즈의 배열 생성
= np.random.normal(0,1,(2,2)) # 평균이 0이고 표준편차가 1인 배열
= np.random.randint(0,10,(2,2))
# [0,10) 사이의 discrete uniform distribution에 따라 랜덤값 배열 생성
cs

(+추가정보)

  • numpy.arange([start]stop[step]dtype=None)
    start를 지정하지 않으면 default로 0을 부여, step을 지정하지 않으면 default로 1을 부여
  • numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
    Return evenly spaced numbers over a specified interval.
  • 배열에 대한 정보
1
2
3
4
5
6
= np.random.randint(10, size=(3,4))
 
x.dim # 2
x.shape # (3, 4)
x.size # 12
x.dtype # int64
cs

 

2. 배열 인덱싱, 슬라이싱

 

3. 브로드캐스팅 : shape가 다른 array끼리 연산하는 것

 

A = [[1,2,3] [4,5,6] [7,8,9]]

B = [1]

C = [0 1 2]

D = [[0][1][2]]

 

이렇게 차원이 다른 행렬이 있어도

A+B

A+C

C+D

계산이 가능하다.

 

# 집계함수 : sum, min, max, mean 등 데이터에 대한 요약 통계

1
2
3
4
5
matrix = np.arange(9).reshape(3,3)
np.sum(matrix) # 결과는 32 
np.min(matrix) # 0
np.max(matrix) # 8
np.mean(matrix) # 3.55555...
cs

 

# 마스킹 연산 : True, False array를 통해서 특정 값들을 뽑아내는 방법

 

 

@ 조금씩 보충할 예정

 

코드를 작성하고 가져온 사이트 : colorscripter.com/

Comments