網(wǎng)站首頁 編程語言 正文
1.cut()可以實現(xiàn)類似于對成績進行優(yōu)良統(tǒng)計的功能,來看代碼示例。
假如我們有一組學生成績,我們需要將這些成績分為不及格(0-59)、及格(60-70)、良(71-85)、優(yōu)(86-100)這幾組。這時候可以用到cut()
import numpy as np
import pandas as pd
# 我們先給 scores傳入30個從0到100隨機的數(shù)
scores = np.random.uniform(0,100,size=30)
# 然后使用 np.round()函數(shù)控制數(shù)據(jù)精度
scores = np.round(scores,1)
# 指定分箱的區(qū)間
grades = [0,59,70,85,100]
cuts = pd.cut(scores,grades)
print('\nscores:')
print(scores)
print('\ncuts:')
print(cuts)
# 我們還可以計算出每個箱子中有多少個數(shù)據(jù)
print('\ncats.value_counts:')
print(pd.value_counts(cuts))
======output:======
scores:
[ 6. ?50.8 80.2 22.1 60.1 75.1 30.8 50.8 81.6 17.4 13.4 24.3 67.3 84.4
?63.4 21.3 17.2 ?3.7 40.1 12.4 15.7 23.1 67.4 94.8 72.6 12.8 81. ?82.
?70.2 54.1]
cuts:
[(0, 59], (0, 59], (70, 85], (0, 59], (59, 70], ..., (0, 59], (70, 85], (70, 85], (70, 85], (0, 59]]
Length: 30
Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 85] < (85, 100]]
cuts.value_counts:
(0, 59] ? ? ?17
(70, 85] ? ? ?8
(59, 70] ? ? ?4
(85, 100] ? ? 1
dtype: int64
默認情況下,cat()的區(qū)間劃分是左開右閉,可以傳遞right=False來改變哪一邊是封閉的
代碼示例:
cuts = pd.cut(scores,grades,right=False)
也可以通過向labels選項傳遞一個列表或數(shù)組來傳入自定義的箱名
代碼示例:
group_names = ['不及格','及格','良','優(yōu)秀']
cuts = pd.cut(scores,grades,labels=group_names)
當我們不需要自定義劃分區(qū)間時,而是需要根據(jù)數(shù)據(jù)中最大值和最小值計算出等長的箱子。
代碼示例:
# 將成績均勻的分在四個箱子中,precision=2的選項將精度控制在兩位
cuts = pd.cut(scores,4,precision=2)
2.qcut()可以生成指定的箱子數(shù),然后使每個箱子都具有相同數(shù)量的數(shù)據(jù)
代碼示例:
import numpy as np
import pandas as pd
# 正態(tài)分布
data = np.random.randn(100)
# 分四個箱子
cuts = pd.qcut(data,4)
print('\ncuts:')
print(cuts)
print('\ncuts.value_counts:')
print(pd.value_counts(cuts))
======output:======
cuts:
[(-0.745, -0.0723], (0.889, 2.834], (-0.745, -0.0723], (0.889, 2.834], (0.889, 2.834], ..., (-0.745, -0.0723], (-0.0723, 0.889], (-3.1599999999999997, -0.745], (-0.745, -0.0723], (-0.0723, 0.889]]
Length: 100
Categories (4, interval[float64]): [(-3.1599999999999997, -0.745] < (-0.745, -0.0723] < (-0.0723, 0.889] <
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (0.889, 2.834]]
cuts.value_counts:
(0.889, 2.834] ? ? ? ? ? ? ? ? ? 25
(-0.0723, 0.889] ? ? ? ? ? ? ? ? 25
(-0.745, -0.0723] ? ? ? ? ? ? ? ?25
(-3.1599999999999997, -0.745] ? ?25
dtype: int64
原文鏈接:https://blog.csdn.net/marioivy/article/details/96766913
相關(guān)推薦
- 2022-06-16 golang?validator庫參數(shù)校驗實用技巧干貨_Golang
- 2022-11-16 常用的Git便捷操作合集_相關(guān)技巧
- 2022-10-18 Qt實現(xiàn)TCP客戶端和服務(wù)器通訊程序_C 語言
- 2022-05-22 docker部署訪問postgres數(shù)據(jù)庫的實現(xiàn)方法_docker
- 2022-12-01 Python?Flask前端自動登錄功能實現(xiàn)詳解_python
- 2023-05-16 python實現(xiàn)動態(tài)規(guī)劃算法的示例代碼_python
- 2022-03-16 .NET6導入和導出EXCEL_實用技巧
- 2023-09-12 linux的root用戶,用戶組
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支