获取KDE的稀疏区域

问题描述

我有一个20k实数的数组,并且我使用RecyclerView来获得下面的核密度估计。我怎么能纯粹以编程方式选择稀疏区域或稠密区域的索引?

我目前的方法是采用pd.DataFrame(scores).plot.kde(figsize=(24,8))的形式,我对np.where(scores > np.percentile(scores,99))[0]的这种严格要求非常严格,因为它在生产中可能效果不佳。我不确定如何解决的潜在解决方案是选择密度低于20,000的索引

image

解决方法

哪个地区考虑“稀疏”,哪个“密集”可能是非常主观的。这在很大程度上还取决于数据的含义。一个想法是确定一些临界百分比。下面的示例使用最低的0.1 %和最高的99.9 %

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame({'score': np.random.randn(2000,10).cumsum(axis=0).ravel()})
df['score'].quantile([.01,.99])
ax = df.plot.kde(figsize=(24,8))
ax.axvline(df['score'].quantile(.001),color='crimson',ls=':')
ax.axvline(df['score'].quantile(.999),ls=':')
ax.set_ylim(ymin=0) # avoid the kde "floating in the air"
plt.show()

example plot