python-3.x - 使用 scipy.stats.mstats.winsorize 对我的样本进行 1% 和 99% 的 winsorize 后,我的样本的最大值仍然大于 99% 时的值

标签 python-3.x pandas scipy

我想对我的样本进行 1% 和 99% 的缩尾处理,因此我使用 scipy 对我的样本进行缩尾处理。缩尾后,我的样本最大值比 99% 百分位处的值大得惊人。我想知道为什么会这样呢? 我的样本是:

Total Sales         Assets     Market value 
1000                 123        4892  
1232                 12         NaN
125                  1569       156

我用过:

import scipy.stats as sp

for col in df.columns: 
     sp.mstats.winsorize(df[col], limits=0.01, inplace=True)

用我的代码进行缩尾处理后,我发现样本中的最大值仍然大于 99% 时的值。我想我犯了一些错误,但我不知道它在哪里?

最佳答案

问题出在就地操作上。而是将列分配回来:

for col in df.columns: 
     df[col] = stats.mstats.winsorize(df[col], limits=0.01)

示例数据

import numpy as np
import pandas as pd
from scipy import stats

df = pd.DataFrame(np.random.randint(1, 10000, (500000, 2)))
print(df.describe())
#                   0              1
#count  500000.000000  500000.000000
#mean     4993.512288    5004.678502
#std      2888.254381    2884.128073
#min         1.000000       1.000000
#25%      2486.000000    2513.000000
#50%      4985.000000    5005.000000
#75%      7492.000000    7502.000000
#max      9999.000000    9999.000000

# inpalce doesn't change anything when looping over columns:
for col in df.columns: 
     stats.mstats.winsorize(df[col], limits=0.01, inplace=True)
print(df.describe())
#                   0              1
#count  500000.000000  500000.000000
#mean     4993.512288    5004.678502
#std      2888.254381    2884.128073
#min         1.000000       1.000000
#25%      2486.000000    2513.000000
#50%      4985.000000    5005.000000
#75%      7492.000000    7502.000000
#max      9999.000000    9999.000000

for col in df.columns: 
     df[col] = stats.mstats.winsorize(df[col], limits=0.01)
print(df.describe())
#                   0              1
#count  500000.000000  500000.000000
#mean     4993.505330    5004.690118
#std      2886.521538    2882.414353
#min       101.000000     101.000000
#25%      2486.000000    2513.000000
#50%      4985.000000    5005.000000
#75%      7492.000000    7502.000000
#max      9899.000000    9901.000000

关于python-3.x - 使用 scipy.stats.mstats.winsorize 对我的样本进行 1% 和 99% 的 winsorize 后,我的样本的最大值仍然大于 99% 时的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55671182/

相关文章:

python - 如何修复pylint错误 "Unnecessary use of a comprehension"

python - 搜索和更新 Pandas 数据框中每一行值的最快方法

python - 如果某些较低级别索引不存在,则删除较高级别多重索引的所有行

pandas - 为什么我不能删除默认的 Pandas plot logy yticklabels?

python - 使用 scipy.integrate.quad 积分 gamma 函数

python - 下采样非均匀一维信号

python - fastapi 自定义响应类作为默认响应类

python-3.x - 使用 flask sqlalchemy 进行多处理 - psycopg2.DatabaseError : error with status PGRES_TUPLES_OK and no message from the libpq

Python 对数组的每一列应用函数

python-3.x - 忽略特定 flake8-docstrings 错误的整个文件夹