原标题:cut 与qcut
原文来自:CSDN 原文链接:https://blog.csdn.net/super_jackchen/article/details/102996096
cut qcut的区别
两者功能相似,都是将一个Series切割成若干个分组
首先创建数据
factors = np.random.randn(9)[-0.01866508 0.72088087 0.67396285 0.50709849 -2.0963765 1.1875407
0.56316144 -1.00214153 0.01905435]
pd.qcut()
qcut是根据这些值的频率来选择箱子的均匀间隔,即每个箱子中含有的数的数量是相同的。
传入q参数
pd.qcut(factors, 3)
>>>[(-2.097, 0.00648], (0.6, 1.188], (0.6, 1.188], (0.00648, 0.6], (-2.097, 0.00648], (0.6, 1.188], (0.00648, 0.6], (-2.097, 0.00648], (0.00648, 0.6]]
Categories (3, interval[float64]): [(-2.097, 0.00648] < (0.00648, 0.6] < (0.6, 1.188]]
pd.qcut(factors, 3).value_counts() #计算每个分组中含有的数的数量
>>>
(-2.097, 0.00648] 3
(0.00648, 0.6] 3
(0.6, 1.188] 3
dtype: int64
传入label参数
pd.qcut(factors, 3, labels = ["a","b","c"]) #返回每个数对应的分组,但分组名称由label指示
[a, c, c, b, a, c, b, a, b]
Categories (3, object): [a < b < c]
pd.qcut(factors, 3, labels = False) #返回每个数对应的分组,但仅显示分组下标
[0 2 2 1 0 2 1 0 1]
传入retbins参数
pd.qcut(factors, 3, retbins=True) #返回每个数对应的分组,且额外返回bins,即每个边界值
([(-2.097, 0.00648], (0.6, 1.188], (0.6, 1.188], (0.00648, 0.6], (-2.097, 0.00648], (0.6, 1.188], (0.00648, 0.6], (-2.097, 0.00648], (0.00648, 0.6]]
Categories (3, interval[float64]): [(-2.097, 0.00648] < (0.00648, 0.6] < (0.6, 1.188]], array([-2.0963765 , 0.00648121, 0.60009524, 1.1875407 ]))
pd.cut()
cut将根据值本身来选择箱子均匀间隔,即每个箱子的间距都是相同的
传入bins参数
pd.cut(factors, 3) #返回每个数对应的数组
[(-1.002, 0.0929], (0.0929, 1.188], (0.0929, 1.188], (0.0929, 1.188], (-2.1, -1.002], (0.0929, 1.188], (0.0929, 1.188], (-2.1, -1.002], (-1.002, 0.0929]]
Categories (3, interval[float64]): [(-2.1, -1.002] < (-1.002, 0.0929] < (0.0929, 1.188]]
pd.cut(factors, bins=[-3, -2, -1, 0, 1, 2, 3])
>>> [(-1, 0], (0, 1], (0, 1], (0, 1], (-3, -2], (1, 2], (0, 1], (-2, -1], (0, 1]]
Categories (6, interval[int64]): [(-3, -2] < (-2, -1] < (-1, 0] < (0, 1] < (1, 2] < (2, 3]]
pd.cut(factors, 3).value_counts() #计算每个分组中含有的数的数量
>>>
(-2.1, -1.002] 2
(-1.002, 0.0929] 2
(0.0929, 1.188] 5
dtype: int64
传入label参数
pd.cut(factors, 3, labels=["a", "b", "c"]) #返回每个数对应的分组,但分组名称由label指示
>>> [b, c, c, c, a, c, c, a, b]
Categories (3, object): [a < b < c]
pd.cut(factors, 3, labels=False) #返回每个数对应的分组,但仅显示分组下标
>>> [1 2 2 2 0 2 2 0 1]
传入retbins参数
pd.cut(factors, 3, retbins=True) #返回每个数对应的分组,且额外返回bins,即每个边界值
>>> ([(-1.002, 0.0929], (0.0929, 1.188], (0.0929, 1.188], (0.0929, 1.188], (-2.1, -1.002], (0.0929, 1.188], (0.0929, 1.188], (-2.1, -1.002], (-1.002, 0.0929]]
Categories (3, interval[float64]): [(-2.1, -1.002] < (-1.002, 0.0929] < (0.0929, 1.188]], array([-2.09966042, -1.00173743, 0.09290163, 1.1875407 ]))
免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。
合作及投稿邮箱:E-mail:editor@tusaishared.com