将1d NumPy中的值分配到类中

时间:2014-03-28 16:55:58

标签: python numpy

如果我有1d数组:

arr = np.array([ 5.243618,  5.219185,  4.755633,  5.685147,  5.2342  ,  6.06918 ,
        5.324837,  4.857919,  5.768971,  4.310884,  4.442189,  4.883281,
        4.591852,  5.8325  ,  5.865175,  5.642187,  5.941979,  6.30038 ,
        6.475276,  4.598086,  5.822819,  5.938378,  6.271719,  5.465492,
        4.230573,  4.331199,  4.912246,  4.878696,  5.393229,  4.857071,
        4.95928 ,  4.83672 ,  5.530075,  4.233449,  5.591468,  4.546228,
        4.710242,  4.880406,  4.279519,  4.461141,  6.168588,  6.074305,
        5.720245,  6.127273,  5.79335 ,  6.176584,  5.04695 ,  5.80022 ,
        5.899088,  5.925466,  5.095225,  6.33216 ,  6.335905,  3.918357,
        4.703728,  4.605504,  5.216878,  6.144148,  4.883721,  5.601009,])

和包含上限的列表:

bins = [4.9122459999999997, 5.3932289999999998, 5.7202450000000002, 6.0743049999999998, 6.475276]

我希望将相同大小的数组返回到arr,其中包含每个值(1, 1, 0, 2, 1, 3, 1等的bin编号。)

我已尝试np.split()使用垃圾箱(显然是错误的),但我找不到一个简单的方法来执行此操作。

1 个答案:

答案 0 :(得分:4)

您可以使用numpy digitize方法将数据分成垃圾箱:

np.digitize(arr, bins)

输出包含每个数据点所属的bin。请参阅此处的文档:LINK