줄리아에서 가중치를 주고 랜덤 샘플링 하는 법
개요
줄리아에서 R에서의 sample()
이나 파이썬 패키지 numpy의 random.choice()
와 같은 역할을 하는 함수인 sample()
과 Weights
함수의 사용법이다.
코드 1
using StatsBase
items = 0:5
weights = 0:5
sample(items, Weights(weights))
# With replacement
my_samps = sample(items, Weights(weights), 10)
# Without replacement
my_samps = sample(items, Weights(weights), 2, replace=false)
실행 결과
julia> using StatsBase
julia> items = 0:5
0:5
julia> weights = 0:5
0:5
julia> sample(items, Weights(weights))
5
julia> # With replacement
julia> my_samps = sample(items, Weights(weights), 10)
10-element Array{Int64,1}:
4
3
2
1
3
3
5
5
2
2
julia> # Without replacement
julia> my_samps = sample(items, Weights(weights), 2, replace=false)
2-element Array{Int64,1}:
4
5
환경
- OS: Windows
- julia: v1.5.0