在Julia中,在statsbase中创建权重向量

在Julia中,在statsbase中创建权重向量,julia,Julia,我正在和茱莉亚玩一点 考虑这一功能: function drawValues(fromDistribution, byCount) #= inputs: fromDistribution : A 2D array Each element is an array with two elements The first one is a value, and the second one is the probability of that value We w

我正在和茱莉亚玩一点

考虑这一功能:

function drawValues(fromDistribution, byCount)

#=
inputs:
fromDistribution : 
    A 2D array
    Each element is an array with two elements
    The first one is a value, and the second one is the probability of that value
    We will draw a value out of this distribution from a random number generator
    
byCount :
    An integer
    We draw that many values from the source distribution

=#



values = []
wts    = []

for i = 1:length(fromDistribution)
    
    push!(values, fromDistribution[i][1])
    push!(wts   , fromDistribution[i][2])
    
end


w = Weights(wts)

res = []

for i = 1:byCount
    
    r = sample(values, w)
    push!(res, r)
    
end


plot(values, wts)
print(res)


end
这将抛出错误:

错误:MethodError:没有匹配权重的方法(::数组{Any,1}, ::Float64)最接近的候选项是:权重(::var“#18#V”,
::var“#16#S”),其中{var“#16#S”您的
向量
S具有任意类型的元素

应该是:

wts = Float64[]
当您编写
wts=[]
时,它相当于
wts=Any[]

查看
重量
方法:

julia> methods(weights)
# 3 methods for generic function "weights":
[1] weights(vs::AbstractArray{T,1} where T<:Real) in StatsBase at c:\JuliaPkg\Julia1.5.3\packages\StatsBase\EA8Mh\src\weights.jl:76
[2] weights(vs::AbstractArray{T,N} where N where T<:Real) in StatsBase at c:\JuliaPkg\Julia1.5.3\packages\StatsBase\EA8Mh\src\weights.jl:77
[3] weights(model::StatisticalModel) in StatsBase at c:\JuliaPkg\Julia1.5.3\packages\StatsBase\EA8Mh\src\statmodels.jl:143
values=first.(fromDistribution)
wts=last.(fromDistribution)
而不是循环。这是与相同的帖子。请不要交叉发布,或者至少提供链接以避免浪费人们的时间。
value = Float64[]
res = Float64[]  # or maybe Int[] depending on what your code does