Python: Frequency of occurrences

前端 未结 3 2108
难免孤独
难免孤独 2021-01-01 18:05

I have list of integers and want to get frequency of each integer. This was discussed here

The problem is that approach I\'m using gives me frequency of floating num

3条回答
  •  天涯浪人
    2021-01-01 18:32

    (Late to the party, just thought I would add a seaborn implementation)

    Seaborn Implementation of the above question:

    seaborn.__version__ = 0.9.0 at time of writing.

    Load the libraries and setup mock data.

    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    data = np.array([3]*10 + [5]*20 + [7]*5 + [9]*27 + [11]*2)
    

    Plot the data using seaborn.distplot:

    Using specified bins, calculated as per the above question.

    sns.distplot(data,bins=np.arange(data.min(), data.max()+1),kde=False,hist_kws={"align" : "left"})
    plt.show()
    

    Trying numpy built-in binning methods

    I used the doane binning method below, which produced integer bins, migth be worth trying out the standard binning methods from numpy.histogram_bin_edges as this is how matplotlib.hist() bins the data.

    sns.distplot(data,bins="doane",kde=False,hist_kws={"align" : "left"})
    plt.show()
    

    Produces the below Histogram:

提交回复
热议问题