资源经验分享cut 与qcut

cut 与qcut

2019-11-12 | |  60 |   0

原标题: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 ]))

newsimg1108_05.png

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 ]))

newsimg1108_06.png

免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com

上一篇:(Python)从零开始,简单快速学机器仿人视觉Opencv---第十八节:轮廓的性质

下一篇:基于深度神经网络的多分类函数

用户评价
全部评价

热门资源

  • Python 爬虫(二)...

    所谓爬虫就是模拟客户端发送网络请求,获取网络响...

  • TensorFlow从1到2...

    原文第四篇中,我们介绍了官方的入门案例MNIST,功...

  • TensorFlow从1到2...

    “回归”这个词,既是Regression算法的名称,也代表...

  • 机器学习中的熵、...

    熵 (entropy) 这一词最初来源于热力学。1948年,克...

  • TensorFlow2.0(10...

    前面的博客中我们说过,在加载数据和预处理数据时...