我正在尝试做一个字符串数组的样本,为数组的每个元素合并权重。具体来说,
new_name_event = sample(events,Weights(dict_betas_choices[string(new_player)][new_zone][string(new_time)][string(new_loc)][string(new_res)]))
>ERROR:
MethodError: no method matching Weights(::Array{Any,1}, ::Float64)
Closest candidates are:
Weights(!Matched::var"#18#V", ::var"#16#S") where {var"#16#S"<:Real, var"#17#T"<:Real, var"#18#V"<:AbstractArray{var"#17#T",1}} at C:\Users\semed\.julia\packages\StatsBase\ZxhK8\src\weights.jl:13
Weights(::Any) at C:\Users\semed\.julia\packages\StatsBase\ZxhK8\src\weights.jl:16
Stacktrace:
[1] Weights(::Array{Any,1}) at C:\Users\semed\.julia\packages\StatsBase\ZxhK8\src\weights.jl:16
[2] top-level scope at .\In[22]:62
[3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
在哪里,
println(events, typeof(events))
>["Shot", "Duel", "Pass"]Array{String,1}
pesos = dict_betas_choices[string(new_player)][new_zone][string(new_time)][string(new_loc)][string(new_res)]
println(pesos, typeof(pesos), typeof(pesos[1]))
>Any[0.00114591, 0.69774462, 0.30110947] Array{Any,1} Float64
最佳答案
这应该可以解决您的问题:
Float64.(dict_betas_choices[string(new_player)][new_zone][string(new_time)][string(new_loc)][string(new_res)])
如果它出错,则意味着您的数据不仅仅由数字组成。
通常,您应该将源中的数据固定为 eltype
而不是 Any
而是一些实数类型,例如Float64
如上所述(这应该在数据结构创建时修复)。
关于arrays - MethodError: 没有方法匹配 Weights(::Array{Any,1},::Float64),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67891489/