python - 获取 KDE 的稀疏区域
问题描述
我有一个 20k 实数数组,我用它pd.DataFrame(scores).plot.kde(figsize=(24,8))
来获得以下内核密度估计。如何纯粹以编程方式选择稀疏区域的索引,或者相反地选择密集区域的索引?
我目前的方法是这种形式np.where(scores > np.percentile(scores, 99))[0]
,我非常喜欢这种方式,99
因为它在生产中可能效果不佳。我不确定如何解决的一个潜在解决方案是选择密度低于 20,000 的指数
解决方案
考虑哪个区域“稀疏”和哪个“密集”可能是非常主观的。它还很大程度上取决于数据的含义。一个想法是决定一些截止百分位数。下面的示例使用最低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), color='crimson', ls=':')
ax.set_ylim(ymin=0) # avoid the kde "floating in the air"
plt.show()
推荐阅读
- c# - 一个信号通知非活动消息
- algorithm - 根据相邻点在随机数据中寻找中心
- jquery - div中的Jquery日历,单击时添加日期以输入
- html - 漏洞?Mac 上的 Chrome 忽略提交按钮的字体大小
- mysql - SQL:从给定的纬度和经度查找不同半径内的最近点
- node.js - nodejs 重用同一服务器的最后一个 TLS 连接
- ms-access - 如何通过 ODBC 将表与 VBA 代码链接
- angular - 类型“LocationComponent”是 2 个模块声明的一部分:Angular 6?
- javascript - 获取数据并将结果返回给动态创建的元素
- html - 无论内部元素如何,固定 div 的高度