본문 바로가기

데이터분석/Numpy

[Numpy] 연산과 난수

728x90
반응형

연산

넘파이 배열간에 연산자(+, -, *, /) 또는 함수를 통한 연산이 가능하다.

import numpy as np

arr1 = np.array([1,2,3,4])
arr2 = np.array([1,2,3,4])

print(f"arr1 + arr2 = {arr1+arr2}")
print(f"add(arr1,arr2) = {np.add(arr1,arr2)}")

 

 

기본 수학 함수

함수 설명
add() Numpy array(배열 + 배열, 배열 + 행렬, 행렬 + 행렬) 간의 덧셈 연산을 한다. (+ 연산자로 대체 가능)
substract() Numpy array(배열 - 배열, 배열 - 행렬, 행렬 - 행렬) 간의 뺄셈 연산을 한다. (- 연산자로 대체 가능)
multiply() Numpy array(배열 * 배열, 배열 * 행렬, 행렬 * 행렬) 간의 곱셈 연산을 한다. (* 연산자로 대체 가능)
divide() Numpy array(배열 / 배열, 배열 / 행렬, 행렬 / 행렬) 간의 나눗셈 연산을 한다. (/ 연산자로 대체 가능)
abs(),fabs() 정수, 부동 소수점 또는 복소수 값에 대한 요소별 절대값
sqrt() 각 요소의 제곱근(arr ** 0.5와 동일)
square() 각 요소의 제곱(arr ** 2와 동일)
exp() 밑이 자연상수 e인 지수함수(e^x)로 변환
log(), log10(), log2() 로그값(자연로그, 10, 2, 등)
sign() 양수이면 1, 음수면 -1, 0이면 0으로 만들어줌
ceil() 올림
floor() 내림
rint() 반올림
isnan() NaN(Not a Number)인지 체크
isfinite(), isinf() 유한(finite), 무한(inf) 체크
sin(), cos(), tan() 삼각함수
sinh(), cosh(), tanh() 하이퍼볼릭 삼각함수

 

 

Mathematical functions — NumPy v1.24 Manual

gcd(x1, x2, /[, out, where, casting, order, ...]) Returns the greatest common divisor of |x1| and |x2|

numpy.org

 

 

선형 대수학 함수

함수 설명
diag()  
dot() 내적 연산
trace() 배열의 대각선을 따라 합을 계산
det() 배열의 행렬식을 계산
eig() 정사각형 배열의 고유값과 오른쪽 고유 벡터를 계산
inv() 역행렬을 계산(부동 소수점 산술을 사용하지 않음)
pinv() 역행렬을 계산
qr() 행렬의 qr 인수 분해 계산
svd() 단수 값 분해
solve() 선형 행렬 방정식 또는 선형 스칼라 방정식의 시스템을 계산
lstsq() 최소 제곱 솔루션을 선형 행렬 방정식으로 되돌림

 

 

Linear algebra (numpy.linalg) — NumPy v1.24 Manual

The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficient low level implementations of standard linear algebra algorithms. Those libraries may be provided by NumPy itself using C versions of a subset of their reference implementations

numpy.org

 

 

난수 생성

Numpy는 난수 Numpy Array를 만들 수 있다.

import numpy as np

randmat_3x2 = np.random.rand(3,2) # 0~1 사이 균일 분포
randGmat_3x2 = np.random.randn(3,2) # 평균 0, 표준편차 1의 가우시안 분포
randint_1_9 = np.random.randint(1,10)
samplingRN = np.random.normal(1,2,3) # 정규분포 N(1,10^2)로 부터 얻은 임의 숫자 3개 

print(randmat_3x2)
print(randint_1_9)
print(samplingRN)

 

난수 관련 함수들

 

Random sampling (numpy.random) — NumPy v1.24 Manual

Numpy’s random number routines produce pseudo random numbers using combinations of a BitGenerator to create sequences and a Generator to use those sequences to sample from different statistical distributions: BitGenerators: Objects that generate random n

numpy.org

 

728x90
반응형

'데이터분석 > Numpy' 카테고리의 다른 글

[Numpy]Numpy 정리  (0) 2023.07.11
[Numpy] Numpy 시작  (0) 2023.02.12