简体   繁体   English

对给定 k 求和所有可能对 (x_ik, y_j) 的最有效方法?

[英]The most efficient way to sum all possible pairs (x_ik, y_j) for a given k?

I have two numpy array x with shape (n,m) and y with shape (p,) .我有两个 numpy 数组x形状(n,m)y形状(p,) I would like to sum all possible pairs x[k, i] and y[j] to create a new numpy array z with shape (n, m*p) .我想将所有可能的对x[k, i]y[j]相加,以创建一个新的 numpy 数组z ,其形状为(n, m*p)

A naïve algorithm would be:一个朴素的算法是:

import numpy as np
# some code
z = np.empty((n, m*p))
for k in range(n):
    for i in range(m):
        for j in range(p):
            z[k, i + m * j] = x[k, i] + y[j]

This algorithm has a polynomial complexity: O(n*m*p) Knowing I am working on array with $n ~ 1e6$ I am looking a for a more efficient algorithm using the power of numpy and/or pandas.该算法具有多项式复杂度: O(n*m*p)知道我正在处理 $n ~ 1e6$ 的数组,我正在寻找一种更有效的算法,使用 numpy 和/或 pandas 的力量。

I have done some research and I found a possible solution: Efficient way to sum all possible pairs我做了一些研究,找到了一个可能的解决方案: Efficient way to sum all possible pairs

But it does not fit with my specific problem, I mean I can use it but it will still not be pythonic as I would iterate with one loop (the solution is reusable without much effort for n=1).但它不适合我的具体问题,我的意思是我可以使用它,但它仍然不是 Pythonic,因为我会用一个循环进行迭代(对于 n=1,该解决方案无需太多努力即可重用)。

As others have said in the comments, not improving on the complexity but making use of vectorization and memory contiguity:正如其他人在评论中所说,没有提高复杂性,而是利用矢量化和 memory 连续性:

np.add.outer(X,y).reshape(len(X), -1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 求和所有可能对的有效方法 - Efficient way to sum all possible pairs 计算图像中给定矩形内所有像素总和的最有效方法是什么? - What is the most efficient way to compute the sum of all pixels inside a given rectangle in an image? 删除指定区域中x,y坐标的最有效方法 - Most efficient way to remove x,y coordinates in a specified area 生成大量 (x,y,z) 坐标的最有效方法 - Most efficient way to generate a large array of (x,y,z) coordinates 从XY元组列表字典中获得最大X和Y的最有效方法 - Most efficient way to get the maximum X and Y from a dictionary of lists of X-Y tuples 构造1到n的四元组的所有可能组合的最有效方法 - Most efficient way to construct all possible combinations of a quadruple for 1 to n 大多数pythonic(和有效)的方式将列表成对嵌套 - Most pythonic (and efficient) way of nesting a list in pairs 给定向量 a,在矩阵 F(a[i], a[j]) 上使用 .sum 执行三角和的有效方法 - Efficient way to perform a triangular sum with .sum over a matrix F(a[i], a[j]), given the vector a python 中最有效的计算方式在对列表中查找对 - Most efficient computational way in python find pairs in a list of pairs 以给定总和有效地计算所有对 - Counting efficiently all the pairs with given sum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM