2017-01-19 7 views
0

Ich kann aus einem 1-D-Array einfach nur probieren. Z.B.Sample-Zeilen aus einem Array in Julia

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 

Ich kann auch gewichtete Proben nehmen:

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 

Ich mag würde das Gleiche für ein 2D-Array zu tun, aber Probenahme durch Reihen- und nicht durch das Element. Z.B. wenn ich das Array

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 

Ich möchte ungewichteten und gewichteten Proben nehmen können, die mir geben Ausgänge wie

1 1 1 
2 2 2 
1 1 1 
1 1 1 
3 3 3 

Wie kann ich das tun?

Antwort

5

Die einfachste Lösung ist, hier aus dem Indizes der Zeilen zu probieren, und dann, dass in Ihre Matrix Index verwenden:

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