我可以从一维阵列中取样就好了.例如
julia> a = [1; 2; 3]
3-element Array{Int64,1}:
1
2
3
julia> sample(a, myweights, 5)
5-element Array{Int64,1}:
1
2
1
3
3
Run Code Online (Sandbox Code Playgroud)
我也可以采取加权样本:
julia> myweights = weights([0.8, 0.1, 0.1])
StatsBase.WeightVec{Float64,Array{Float64,1}}([0.8,0.1,0.1],1.0)
julia> sample(a, myweights, 5)
5-element Array{Int64,1}:
2
1
1
1
1
Run Code Online (Sandbox Code Playgroud)
我想对2D数组做同样的事情,但是按行而不是按元素进行采样.例如,如果我有阵列
julia> b = [1 1 1; 2 2 2; 3 3 3]
3×3 Array{Int64,2}:
1 1 1
2 2 2
3 3 3
Run Code Online (Sandbox Code Playgroud)
我希望能够采用未加权和加权的样本,为我提供输出
1 1 1
2 2 2
1 1 1
1 1 1
3 3 3
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
这里最简单的解决方案是从行的索引中进行采样,然后使用它来索引到矩阵:
julia> idxs = sample(indices(b, 1), myweights, 10)
10-element Array{Int64,1}:
1
1
1
2
1
1
3
1
1
1
julia> b[idxs, :]
10×3 Array{Int64,2}:
1 1 1
1 1 1
1 1 1
2 2 2
1 1 1
1 1 1
3 3 3
1 1 1
1 1 1
1 1 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
386 次 |
| 最近记录: |