【问题标题】:Is there a Counter object in Julia?Julia 中有 Counter 对象吗?
【发布时间】:2017-02-07 02:16:26
【问题描述】:

在 Python 中,可以使用高性能对象 collections.Counter 对列表中的项目进行计数:

>>> from collections import Counter
>>> l = [1,1,2,4,1,5,12,1,51,2,5]
>>> Counter(l)
Counter({1: 4, 2: 2, 5: 2, 4: 1, 12: 1, 51: 1})

我在http://docs.julialang.org/en/latest/search.html?q=counter 中搜索过,但似乎找不到计数器对象。

我也看过http://docs.julialang.org/en/latest/stdlib/collections.html,但我也找不到。

我在 Julia 中尝试了 histogram 函数,它返回了一波弃用消息:

> l = [1,1,2,4,1,5,12,1,51,2,5]
> hist(l)

[出]:

WARNING: sturges(n) is deprecated, use StatsBase.sturges(n) instead.
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in sturges(::Int64) at ./deprecated.jl:623
 in hist(::Array{Int64,1}) at ./deprecated.jl:646
 in include_string(::String, ::String) at ./loading.jl:441
 in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/liling.tan/.julia/v0.5/IJulia/src/execute_request.jl:175
 in eventloop(::ZMQ.Socket) at /Users/liling.tan/.julia/v0.5/IJulia/src/eventloop.jl:8
 in (::IJulia.##13#19)() at ./task.jl:360
while loading In[65], in expression starting on line 1
WARNING: histrange(...) is deprecated, use StatsBase.histrange(...) instead
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in histrange(::Array{Int64,1}, ::Int64) at ./deprecated.jl:582
 in hist(::Array{Int64,1}, ::Int64) at ./deprecated.jl:645
 in hist(::Array{Int64,1}) at ./deprecated.jl:646
 in include_string(::String, ::String) at ./loading.jl:441
 in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/liling.tan/.julia/v0.5/IJulia/src/execute_request.jl:175
 in eventloop(::ZMQ.Socket) at /Users/liling.tan/.julia/v0.5/IJulia/src/eventloop.jl:8
 in (::IJulia.##13#19)() at ./task.jl:360
while loading In[65], in expression starting on line 1
WARNING: hist(...) and hist!(...) are deprecated. Use fit(Histogram,...) in StatsBase.jl instead.
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in #hist!#994(::Bool, ::Function, ::Array{Int64,1}, ::Array{Int64,1}, ::FloatRange{Float64}) at ./deprecated.jl:629
 in hist(::Array{Int64,1}, ::FloatRange{Float64}) at ./deprecated.jl:644
 in hist(::Array{Int64,1}, ::Int64) at ./deprecated.jl:645
 in hist(::Array{Int64,1}) at ./deprecated.jl:646
 in include_string(::String, ::String) at ./loading.jl:441
 in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/liling.tan/.julia/v0.5/IJulia/src/execute_request.jl:175
 in eventloop(::ZMQ.Socket) at /Users/liling.tan/.julia/v0.5/IJulia/src/eventloop.jl:8
 in (::IJulia.##13#19)() at ./task.jl:360
while loading In[65], in expression starting on line 1

**Is there a Counter object in Julia?**

【问题讨论】:

    标签: containers histogram counter julia


    【解决方案1】:

    如果您使用的是 Julia 0.5+,直方图函数 has been deprecated 并且您应该改用 StatsBase.jl 模块。警告中也有描述:

    警告:不推荐使用 hist(...) 和 hist!(...)。改为在 StatsBase.jl 中使用 fit(Histogram,...)。

    但如果您使用的是StatsBase.jl,可能countmap 更接近您的需要:

    julia> import StatsBase: countmap                                                                                                                                             
    
    julia> countmap([1,1,2,4,1,5,12,1,51,2,5])                                                                                                                                    
    Dict{Int64,Int64} with 6 entries:
      4  => 1
      2  => 2
      5  => 2
      51 => 1
      12 => 1
      1  => 4
    

    【讨论】:

    • 谢谢!任何想法是StatsBase 一个单独的包,而不是原生 Julia 0.5 的一部分?
    • @alvas 是一个单独的包,但是安装包是一个Pkg.add() 的距离。
    • @alvas 一直在努力缩小 Base Julia 的大小,因此 Base 中没有很多功能。放在包中不会影响性能。
    【解决方案2】:

    DataStructures.jl 包也有 Accumulators / Counters 和更多 使用和组合计数器的通用方法集。

    添加包后

    using Pkg
    Pkg.add("DataStructures")
    

    你可以通过构造一个计数器来计算一个序列的元素

    # generate some data to count
    using Random
    
    seq = [ Random.randstring('a':'c', 2) for _ in 1:100 ]
    
    
    # count the elements in seq
    using DataStructures
    
    counts = counter(seq)
    

    【讨论】:

      猜你喜欢
      • 2015-12-24
      • 2021-11-27
      • 2021-09-10
      • 2015-09-15
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多