【问题标题】:Python Contingency TablePython 列联表
【发布时间】:2018-12-20 00:29:16
【问题描述】:

作为我正在编写的项目的一部分,我正在生成很多很多列联表。

工作流程是:

  • 获取具有连续(浮点)行的大型数据数组,并通过分箱将其转换为离散整数值(例如,结果行的值是 0-9)
  • 将两行切成向量 X 和 Y 并从中生成contingency table,这样我就有了二维频率分布
  • 例如,我有一个 10 x 10 的数组,计算出现的 (xi, yi) 的数量
  • 使用列联表做一些信息论数学

最初,我是这样写的:

def make_table(x, y, num_bins):
    ctable = np.zeros((num_bins, num_bins), dtype=np.dtype(int))
    for xn, yn in zip(x, y):
        ctable[xn, yn] += 1
    return ctable

这很好用,但是太慢了,占用了整个项目 90% 的运行时间。

我能想到的最快的纯 python 优化是这样的:

def make_table(x, y, num_bins):
    ctable = np.zeros(num_bins ** 2, dtype=np.dtype(int))
    reindex = np.dot(np.stack((x, y)).transpose(), 
                     np.array([num_bins, 1]))
    idx, count = np.unique(reindex, return_counts=True)
    for i, c in zip(idx, count):
        ctable[i] = c
    return ctable.reshape((num_bins, num_bins))

这(不知何故)要快得多,但对于看起来不应该成为瓶颈的东西来说,它仍然相当昂贵。是否有任何有效的方法可以做到这一点,我只是没有看到,或者我应该放弃并在 cython 中做到这一点?

另外,这里有一个基准函数。

def timetable(func):
    size = 5000
    bins = 10
    repeat = 1000
    start = time.time()
    for i in range(repeat):
        x = np.random.randint(0, bins, size=size)
        y = np.random.randint(0, bins, size=size)
        func(x, y, bins)
    end = time.time()
    print("Func {na}: {ti} Ms".format(na=func.__name__, ti=(end - start)))

【问题讨论】:

  • 除了 Cython,您可能还想考虑 Numba (numba.pydata.org) - 哪个做得更好会有所不同,但 Numba 可能更容易启动和运行。跨度>

标签: python numpy information-theory


【解决方案1】:

np.stack((x, y)) 的元素表示为整数的巧妙技巧可以更快:

In [92]: %timeit np.dot(np.stack((x, y)).transpose(), np.array([bins, 1]))
109 µs ± 6.55 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [94]: %timeit bins*x + y
12.1 µs ± 260 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

此外,您的第二个解决方案的最后一部分可以稍微简化,只需考虑

np.unique(bins * x + y, return_counts=True)[1].reshape((bins, bins))

更重要的是,由于我们处理的是等间距的非负整数,np.bincount 的性能将优于np.unique;有了这个,以上归结为

np.bincount(bins * x + y).reshape((bins, bins))

总而言之,这比您当前所做的提供了相当多的性能:

In [78]: %timeit make_table(x, y, bins)  # Your first solution
3.86 ms ± 159 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [79]: %timeit make_table2(x, y, bins)  # Your second solution
443 µs ± 23.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [101]: %timeit np.unique(bins * x + y, return_counts=True)[1].reshape((bins, bins))
307 µs ± 25 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [118]: %timeit np.bincount(bins * x + y).reshape((10, 10))
30.3 µs ± 3.44 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

您可能还想了解np.histogramdd,它同时处理舍入和分箱,尽管它可能会比舍入和使用np.bincount 慢。

【讨论】:

  • 做得非常好——这要好一个数量级。 np.bincount(bins * x + y).reshape((bins, bins)) 作为补充说明,它需要进行一些更改才能处理表中的 0:np.bincount(bins * x + y, minlength=bins ** 2).reshape((bins, bins)) 这非常有效,通过了测试并且速度更快。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 2015-06-28
相关资源
最近更新 更多