Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Counter object in Julia?

In Python, it's possible to count items in list using a high-performance object 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})

I've search in http://docs.julialang.org/en/latest/search.html?q=counter but I can't seem to find a counter object.

I've also looked at http://docs.julialang.org/en/latest/stdlib/collections.html but I couldn't find it either.

I've tried the histogram function in Julia and it returned a wave of deprecation messages:

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

[out]:

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?**
like image 406
alvas Avatar asked Jan 05 '23 14:01

alvas


1 Answers

If you are using Julia 0.5+, the histogram functions has been deprecated and you are supposed to use the StatsBase.jl module instead. It is also described in the warning:

WARNING: hist(...) and hist!(...) are deprecated. Use fit(Histogram,...) in StatsBase.jl instead.

But if you are using StatsBase.jl, probably countmap is closer to what you need:

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
like image 167
kennytm Avatar answered Jan 30 '23 02:01

kennytm