[英]Pandas.cut VS df.describe()
您的cut
將范圍划分為 4 個等寬的bin,而describe
使用quartiles 。 僅對於均勻分布的數據,兩者都會導致相同的細分。
例子:
import pandas as pd
import numpy as np
df = pd.DataFrame({'uniform': np.random.rand(1_000_000), 'normal': np.random.randn(1_000_000)})
with np.printoptions(formatter={'float': '{:.3f}'.format}):
print( 'uniform:\n'
f' {df.uniform.describe().iloc[3:].values}\n'
f' {pd.cut(df.uniform, 4).dtype.categories.to_tuples().to_list()}')
print( 'normal:\n'
f' {df.normal.describe().iloc[3:].values}\n'
f' {pd.cut(df.normal, 4).dtype.categories.to_tuples().to_list()}')
輸出:
uniform:
[0.000 0.250 0.499 0.750 1.000]
[(-0.001, 0.25), (0.25, 0.5), (0.5, 0.75), (0.75, 1.0)]
normal:
[-4.908 -0.675 0.001 0.674 5.082]
[(-4.918, -2.411), (-2.411, 0.0867), (0.0867, 2.584), (2.584, 5.082)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.