亲宝软件园·资讯

展开

python pd.cut()与pd.qcut()

Ancky_W 人气:0

1、pd.cut()

用于将数据值按照值本身进行分段并排序到 bins 中。
参数包含:x, bins, right, include_lowest, labels, retbins, precision

x :被划分的数组
bins :被划分的区间/区间数

# x = [1,2,3,5,3,4,1],  bins = 3
[In ] pd.cut(np.array([1,2,3,5,3,4,1]),3)  
[Out] [(0.996, 2.333], (0.996, 2.333], (2.333, 3.667], (3.667, 5.0], (2.333, 3.667], (3.667, 5.0], (0.996, 2.333]]
      Categories (3, interval[float64]): [(0.996, 2.333] < (2.333, 3.667] < (3.667, 5.0]]

# x = [1,2,3,5,3,4,1],  bins = [1,2,3]
[In ] pd.cut(np.array([1,2,3,5,3,4,1]),[1,2,3])
[Out] [NaN, (1.0, 2.0], (2.0, 3.0], NaN, (2.0, 3.0], NaN, NaN]
      Categories (2, interval[int64]): [(1, 2] < (2, 3]]

right :是否包含右端点,默认为 True;
include_lowest :是否包含左端点,默认为 False;

# x = [1,2,3,5,3,4,1],  bins = [1,2,3], 默认不包含左端点 1,默认包含右端点 3
[In ] pd.cut(np.array([1,2,3,5,3,4,1]),[1,2,3])
[Out] [NaN, (1.0, 2.0], (2.0, 3.0], NaN, (2.0, 3.0], NaN, NaN]
      Categories (2, interval[int64]): [(1, 2] < (2, 3]]

# x = [1,2,3,5,3,4,1],  bins = [1,2,3], 设置包含左端点 1,设置包含右端点 3
[In ] pd.cut(np.array([1,2,3,5,3,4,1]),[1,2,3],include_lowest=True,right=False)
[Out] [[1.0, 2.0), [2.0, 3.0), NaN, NaN, NaN, NaN, [1.0, 2.0)]
      Categories (2, interval[int64]): [[1, 2) < [2, 3)]

labels :是否用标记来替代返回的 bins,默认为 False,如需标记,标记数需与 bins 数一致,并为 labels 赋值一组列表;

# x = [1,2,3,5,3,4,1],  bins = 3, 设置用指定标签 ['A','B','C'] 返回序列
[In ] pd.cut(np.array([1,2,3,5,3,4,1]),3,labels=['A','B','C'])
[Out] [A, A, B, C, B, C, A]
      Categories (3, object): [A < B < C]

retbins : 是否返回间距 bins,默认为 False,仅返回 x 中每个值对应的 bin 的列表,若 retbins = True,则返回 bin 的列表及对应的 bins。

# x = [1,2,3,5,3,4,1],  bins = 3, 设置一并返回对应 bins 序列
[In ] pd.cut(np.array([1,2,3,5,3,4,1]),3,retbins=True)
[Out] ([(0.996, 2.333], (0.996, 2.333], (2.333, 3.667], (3.667, 5.0], (2.333, 3.667], (3.667, 5.0], (0.996, 2.333]]
      Categories (3, interval[float64]): [(0.996, 2.333] < (2.333, 3.667] < (3.667, 5.0]],
      array([0.996     , 2.33333333, 3.66666667, 5.        ]))

precision : 精度,区间边界值保留的小数点位数

# x = [1,2,3,5,3,4,1],  bins = 3, 精度为2
[In ] pd.cut(np.array([1,2,3,5,3,4,1]),3,precision=2)
[Out] [(1.0, 2.33], (1.0, 2.33], (2.33, 3.67], (3.67, 5.0], (2.33, 3.67], (3.67, 5.0], (1.0, 2.33]]
      Categories (3, interval[float64]): [(1.0, 2.33] < (2.33, 3.67] < (3.67, 5.0]]

2、pd.qcut()

基于分位数的离散化功能。 根据等级或基于样本分位数(或者说基于样本值落在区间的频率),将变量分离为相等大小的桶。

参数包含:x, q, labels, retbins, precision, duplicates

3、pd.cut() v.s. pd.qcut()

[In] ll = [1,2,3,5,3,4,1,2]
     print('- - - pd.cut()示例1 - - -')
     print(pd.cut(ll, 4, precision=2).value_counts())
     print('- - - pd.cut()示例2 - - -')
     print(pd.cut(ll, [1,2,4], precision=2).value_counts())
     print('- - - pd.qcut()示例 - - -')
     print(pd.qcut(ll, 4, precision=2).value_counts())

[Out] - - - pd.cut()示例1 - - -
     (1.0, 2.0]    4
     (2.0, 3.0]    2
     (3.0, 4.0]    1
     (4.0, 5.0]    1
     dtype: int64
     - - - pd.cut()示例2 - - -
     (1, 2]    2
     (2, 4]    3
     dtype: int64
     - - - pd.qcut()示例 - - -
     (0.99, 1.75]    2
     (1.75, 2.5]     2
     (2.5, 3.25]     2
     (3.25, 5.0]     2
     dtype: int64

加载全部内容

相关教程
猜你喜欢
用户评论