Python连续数据离散化处理和pandas.cut函数用法

栏目: Python · 发布时间: 5年前

内容简介:连续数据离散化场景:数据分析和统计的预处理阶段,经常的会碰到年龄、消费等连续型数值,我们希望将数值进行离散化分段统计,提高数据区分度,那么下面介绍一个简单使用的pandas中的 cut() 方法函数用法:

连续数据离散化场景:

数据分析和统计的预处理阶段,经常的会碰到年龄、消费等连续型数值,我们希望将数值进行离散化分段统计,提高数据区分度,那么下面介绍一个简单使用的pandas中的 cut() 方法

函数用法:

**cut(series, bins, right=True, labels=NULL)**

series  (类似数组排列,必须是一维的)

bins  (表示分段数或分类区间,可以是数字,比如说4,就是分成4段,也可以是列表,表示各段的间隔点)

right=True(表示分组右边闭合,right=False表示分组左边闭合,)

labels(表示结果标签,一般最好添加,方便阅读和后续统计)

另外,请注意:

如果  cut_1 = pd.cut ()

cut_1.codes: 获得分组的codes码,即0,1,2,3,4…

pd.value_counts(cut_1):  返回分段计数的结果

如下成绩代码:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

np.random.seed(666)

score_list = np.random.randint(25, 100, size=20)
print(score_list)
# [27 70 55 87 95 98 55 61 86 76 85 53 39 88 41 71 64 94 38 94]

# 指定多个区间
bins = [0, 59, 70, 80, 100]

score_cut = pd.cut(score_list, bins)
print(type(score_cut)) # <class 'pandas.core.arrays.categorical.Categorical'>
print(score_cut)
'''
[(0, 59], (59, 70], (0, 59], (80, 100], (80, 100], ..., (70, 80], (59, 70], (80, 100], (0, 59], (80, 100]]
Length: 20
Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 80] < (80, 100]]
'''
print(pd.value_counts(score_cut)) # 统计每个区间人数
'''
(80, 100]    8
(0, 59]      7
(59, 70]     3
(70, 80]     2
dtype: int64
'''

df = DataFrame()
df['score'] = score_list
df['student'] = [pd.util.testing.rands(3) for i in range(len(score_list))]
print(df)
'''
    score student
0      27     1ul
1      70     yuK
2      55     WWK
3      87     EU6
4      95     Vqn
5      98     KAf
6      55     QNT
7      61     HaE
8      86     aBo
9      76     MMa
10     85     Ctc
11     53     5BI
12     39     wBp
13     88     WMB
14     41     q5t
15     71     MjZ
16     64     nTc
17     94     Kyx
18     38     Rlh
19     94     2uV
'''

# 使用cut方法进行分箱
print(pd.cut(df['score'], bins))
'''
0       (0, 59]
1      (59, 70]
2       (0, 59]
3     (80, 100]
4     (80, 100]
5     (80, 100]
6       (0, 59]
7      (59, 70]
8     (80, 100]
9      (70, 80]
10    (80, 100]
11      (0, 59]
12      (0, 59]
13    (80, 100]
14      (0, 59]
15     (70, 80]
16     (59, 70]
17    (80, 100]
18      (0, 59]
19    (80, 100]
Name: score, dtype: category
Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 80] < (80, 100]]
'''

df['Categories'] = pd.cut(df['score'], bins)
print(df)
'''
    score student Categories
0      27     1ul    (0, 59]
1      70     yuK   (59, 70]
2      55     WWK    (0, 59]
3      87     EU6  (80, 100]
4      95     Vqn  (80, 100]
5      98     KAf  (80, 100]
6      55     QNT    (0, 59]
7      61     HaE   (59, 70]
8      86     aBo  (80, 100]
9      76     MMa   (70, 80]
10     85     Ctc  (80, 100]
11     53     5BI    (0, 59]
12     39     wBp    (0, 59]
13     88     WMB  (80, 100]
14     41     q5t    (0, 59]
15     71     MjZ   (70, 80]
16     64     nTc   (59, 70]
17     94     Kyx  (80, 100]
18     38     Rlh    (0, 59]
19     94     2uV  (80, 100]
'''

# 但是这样的方法不是很适合阅读,可以使用cut方法中的label参数
# 为每个区间指定一个label
df['Categories'] = pd.cut(df['score'], bins, labels=['low', 'middle', 'good', 'perfect'])
print(df)
'''
    score student Categories
0      27     1ul        low
1      70     yuK     middle
2      55     WWK        low
3      87     EU6    perfect
4      95     Vqn    perfect
5      98     KAf    perfect
6      55     QNT        low
7      61     HaE     middle
8      86     aBo    perfect
9      76     MMa       good
10     85     Ctc    perfect
11     53     5BI        low
12     39     wBp        low
13     88     WMB    perfect
14     41     q5t        low
15     71     MjZ       good
16     64     nTc     middle
17     94     Kyx    perfect
18     38     Rlh        low
19     94     2uV    perfect
'''

更多 Python 相关信息见 Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-05/158639.htm


以上所述就是小编给大家介绍的《Python连续数据离散化处理和pandas.cut函数用法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithms in Python

Data Structures and Algorithms in Python

Michael T. Goodrich、Roberto Tamassia、Michael H. Goldwasser / John Wiley & Sons / 2013-7-5 / GBP 121.23

Based on the authors' market leading data structures books in Java and C++, this book offers a comprehensive, definitive introduction to data structures in Python by authoritative authors. Data Struct......一起来看看 《Data Structures and Algorithms in Python》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码