python - 将一列合并并与 (2,N) 数组的另一列相加
问题描述
问题:
我有一个如下数据集:
import numpy as np
x = np.arange(0,10000,0.5)
y = np.arange(x.size)/x.size
在 log-log 空间中绘图,它看起来像这样:
import matplotlib.pyplot as plt
plt.loglog(x, y)
plt.show()
显然这个log log plot中有很多冗余信息。我不需要10000分来代表这种趋势。
我的问题是:如何对这些数据进行分类,以便在对数刻度的每个数量级中显示统一数量的点?在每个数量级,我想得到大约十分。因此,我需要用指数增长的 bin 大小来 bin 'x',然后取y
每个 bin 对应的所有元素的平均值。
试图:
首先,我生成要用于x
.
# need a nicer way to do this.
# what if I want more than 10 bins per order of magnitude?
bins = 10**np.arange(1,int(round(np.log10(x.max()))))
bins = np.unique((bins.reshape(-1,1)*np.arange(0,11)).flatten())
#array([ 0, 10, 20, 30, 40, 50, 60, 70, 80,
# 90, 100, 200, 300, 400, 500, 600, 700, 800,
# 900, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
# 9000, 10000])
其次,我找到 x 的每个元素对应的 bin 的索引:
digits = np.digitize(x, bins)
现在我可以真正使用帮助的部分。我想取y
每个 bin 对应的每个元素的平均值,然后绘制这些平均值与 bin 中点的关系:
# need a nicer way to do this.. is there an np.searchsorted() solution?
# this way is quick and dirty, but it does not scale with acceptable speed
averages = []
for d in np.unique(digits):
mask = digits==d
y_mean = np.mean(y[mask])
averages.append(y_mean)
del mask, y_mean, d
# now plot the averages within each bin against the center of each bin
plt.loglog((bins[1:]+bins[:-1])/2.0, averages)
plt.show()
摘要:
有没有更流畅的方法来做到这一点?如何生成n
每个数量级的任意点而不是 10?
解决方案
我将回答您的几个问题中的两个:如何交替创建箱并生成n
每个数量级的任意点而不是 10?
您可以使用np.logspace
并np.outer
为任意n
值创建您的 bin,如下所示。默认基数logspace
为 10。它生成对数间隔的点,类似于linspace
生成线性间隔的网格。
为了n=10
n = 10
bins = np.unique(np.outer(np.logspace(0, 3, 4), np.arange(0, n+1)))
# array([0.e+00, 1.e+00, 2.e+00, 3.e+00, 4.e+00, 5.e+00, 6.e+00, 7.e+00,
# 8.e+00, 9.e+00, 1.e+01, 2.e+01, 3.e+01, 4.e+01, 5.e+01, 6.e+01,
# 7.e+01, 8.e+01, 9.e+01, 1.e+02, 2.e+02, 3.e+02, 4.e+02, 5.e+02,
# 6.e+02, 7.e+02, 8.e+02, 9.e+02, 1.e+03, 2.e+03, 3.e+03, 4.e+03,
# 5.e+03, 6.e+03, 7.e+03, 8.e+03, 9.e+03, 1.e+04])
为了n=20
n = 20
bins = np.unique(np.outer(np.logspace(0, 3, 4), np.arange(0, n+1)))
# array([0.0e+00, 1.0e+00, 2.0e+00, 3.0e+00, 4.0e+00, 5.0e+00, 6.0e+00, 7.0e+00, 8.0e+00, 9.0e+00, 1.0e+01, 1.1e+01, 1.2e+01, 1.3e+01, 1.4e+01, 1.5e+01, 1.6e+01, 1.7e+01, 1.8e+01, 1.9e+01, 2.0e+01, 3.0e+01, 4.0e+01, 5.0e+01, 6.0e+01, 7.0e+01, 8.0e+01, 9.0e+01, 1.0e+02, 1.1e+02, 1.2e+02, 1.3e+02, 1.4e+02, 1.5e+02, 1.6e+02, 1.7e+02, 1.8e+02, 1.9e+02, 2.0e+02, 3.0e+02, 4.0e+02, 5.0e+02, 6.0e+02, 7.0e+02, 8.0e+02, 9.0e+02, 1.0e+03, 1.1e+03, 1.2e+03, 1.3e+03, 1.4e+03, 1.5e+03, 1.6e+03, 1.7e+03, 1.8e+03, 1.9e+03, 2.0e+03, 3.0e+03, 4.0e+03, 5.0e+03, 6.0e+03, 7.0e+03, 8.0e+03, 9.0e+03, 1.0e+04, 1.1e+04, 1.2e+04, 1.3e+04, 1.4e+04, 1.5e+04, 1.6e+04, 1.7e+04, 1.8e+04, 1.9e+04, 2.0e+04])
编辑
如果您愿意0, 10, 20, 30...90, 100, 200, 300...
,可以执行以下操作
n = 10
bins = np.unique(np.outer(np.logspace(1, 3, 3), np.arange(0, n+1)))
# array([ 0., 10., 20., 30., 40., 50., 60., 70.,
# 80., 90., 100., 200., 300., 400., 500., 600.,
# 700., 800., 900., 1000., 2000., 3000., 4000., 5000.,
# 6000., 7000., 8000., 9000., 10000.])
推荐阅读
- r - 用闪亮的 ggplot2 绘制基因 FPKM 归一化计数值
- python - 合并具有相同行长和不同列号python的多个无标题csv文件
- javascript - Jquery:包括来自外部文件的头部
- reactjs - 在我的反应应用程序中,设置间隔在浏览器刷新时变得清晰
- python - 我们可以写一个脚本来抓取多个站点吗
- c++ - 根据 Boost 创建 Python 包
- debugging - 无法使用 org-link-open-from-string 打开链接,因为 (file-name-directory (buffer-file-name)) 似乎返回 nil
- node.js - 使用 Express 覆盖所有响应标头
- ios - Xcode 尝试在未从模拟器卸载的情况下运行应用程序时显示错误(多配置文件)
- latex - 使用 gnuplot,如何获取具有画布大小的 PDF,包括文本?