Isa*_*man 4 python grouping histogram discrete-mathematics seaborn
我试图根据我拥有的表达水平变化的数据,对一种植物(拟南芥)的约 11,000 个基因进行分组,以响应光照。
每个基因的原始值是连续的随机变量,但我希望将这些值离散化为 20 个离散类。
所以而不是:
change_in_expression = array([-2.2, -1.1, -1.2, ..., 0.6, -1. , -0.9])
Run Code Online (Sandbox Code Playgroud)
我有课堂输出:
change_in_expression = array(["3_to_4","2_to_3","1_to_2",...])
Run Code Online (Sandbox Code Playgroud)
我用 绘制分布seaborn's distplot(),我相信它使用了KDE:
import seaborn as sns
d = array([-2.2, -1.1, -1.2, ..., 0.6, -1. , -0.9]) # = change_in_expression
dis = sns.distplot(d, fit=stats.laplace, kde=False)
plt.title("Distribution of Differential Expression")
plt.xlabel("Log2FoldChange in expression")
plt.ylabel("Frequency")
plt.show()
Run Code Online (Sandbox Code Playgroud)
而且我知道matplotlib.pyplot's hist()允许您提取垃圾箱,当默认设置“自动”生成这些分组时......
问题是,我怎样才能对我的基因进行分组?这是一个更广泛的问题,而不仅仅是询问...的seaborn版本,matplotlib's hist()因为seaborn's distplot使用KDE。
ax通过查看以下可用方法,我似乎无法从seaborn 创建的对象中获取垃圾箱:
dir(sns.distplot(d, fit=stats.laplace, kde=False)
Run Code Online (Sandbox Code Playgroud)
我想,一种方法是检查 seaborndistplot源代码的内容,弄清楚他们如何在绘图之前对数据进行分类……但这远远超出了我的独角兽技能范围……
Seaborn 调用pyplot.hist,而后者又调用numpy.histogram。因此,bins如果没有指定,可以检查 seaborn 使用什么作为参数。即a作为数据,
bins = min(_freedman_diaconis_bins(a), 50)
Run Code Online (Sandbox Code Playgroud)
其中1
def _freedman_diaconis_bins(a):
"""Calculate number of hist bins using Freedman-Diaconis rule."""
# From https://stats.stackexchange.com/questions/798/
a = np.asarray(a)
if len(a) < 2:
return 1
h = 2 * iqr(a) / (len(a) ** (1 / 3))
# fall back to sqrt(a) bins if iqr is 0
if h == 0:
return int(np.sqrt(a.size))
else:
return int(np.ceil((a.max() - a.min()) / h))
Run Code Online (Sandbox Code Playgroud)
和iqr 2
def iqr(a):
"""Calculate the IQR for an array of numbers."""
a = np.asarray(a)
q1 = stats.scoreatpercentile(a, 25)
q3 = stats.scoreatpercentile(a, 75)
return q3 - q1
Run Code Online (Sandbox Code Playgroud)
所有这些应该大致相同
bins = min(len(numpy.histogram_bin_edges(a, bins="fd")), 50)
Run Code Online (Sandbox Code Playgroud)
或者
bins = 50 if len(numpy.histogram_bin_edges(a, bins="fd")) > 50 else "fd"
Run Code Online (Sandbox Code Playgroud)
然后传递bins到pyplot.hist,
plt.hist(a, bins=bins)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
888 次 |
| 最近记录: |