我正在使用一种算法,要求每个示例都具有一个矩阵,例如Xi,即ai x b,并且对于每个O(n^2)对示例,我要找到每行Xiu - Xjv之间的差,然后求和外部产品sum_u sum_v np.outer(Xiu - Xjv, Xiu - Xjv)。不幸的是,这个内部的双重和相当慢,并且导致运行时间在大型数据集上失控。现在,我只是使用for循环来执行此操作。有一些pythonic的方法可以加快此内部操作的速度吗?我一直在想一想,没有运气。需要说明的是,对于每个n示例,都有一个尺寸为Xi的矩阵ai x b,其中每个示例的ai不同。对于每对(Xi, Xj),我想遍历两个矩阵之间的所有O(ai * bi)行对,找到Xiu - Xjv,将其外积与自身np.outer(Xiu - Xjv, Xiu - Xjv)取值,最后求和所有这些外积。例如:假设D为[[1,2],[3,4]],我们正在为两个矩阵使用D。那么,第一步可能是np.outer(D[0] - D[1], D[0] - D[1]),这将是矩阵[4,4],[4,4]。简而言之,(0,0)和(1,1)只是0个矩阵,而(0,1)和(1,0)都是4个矩阵,因此,对的所有四个外积的最终总和为。 最佳答案 好的,这很有趣。我仍然不禁以为,只需对numpy.tensordot进行一次巧妙的调用就可以完成所有操作,但是无论如何,这似乎消除了所有Python级别的循环:import numpydef slow( a, b=None ): if b is None: b = a a = numpy.asmatrix( a ) b = numpy.asmatrix( b ) out = 0.0 for ai in a: for bj in b: dij = bj - ai out += numpy.outer( dij, dij ) return outdef opsum( a, b=None ): if b is None: b = a a = numpy.asmatrix( a ) b = numpy.asmatrix( b ) RA, CA = a.shape RB, CB = b.shape if CA != CB: raise ValueError( "input matrices should have the same number of columns" ) out = -numpy.outer( a.sum( axis=0 ), b.sum( axis=0 ) ); out += out.T out += RB * ( a.T * a ) out += RA * ( b.T * b ) return outdef test( a, b=None ): print( "ground truth:" ) print( slow( a, b ) ) print( "optimized:" ) print( opsum( a, b ) ) print( "max abs discrepancy:" ) print( numpy.abs( opsum( a, b ) - slow( a, b ) ).max() ) print( "" )# OP exampletest( [[1,2], [3,4]] )# non-symmetric examplea = [ [ 1, 2, 3 ], [-4, 5, 6 ], [7, -8, 9 ], [ 10, 11, -12 ] ]a = numpy.matrix( a, dtype=float )b = a[ ::2, ::-1 ] + 15test( a, b )# non-integer exampletest( numpy.random.randn( *a.shape ), numpy.random.randn( *b.shape ) )使用该示例输入(相当随意),opsum的计时(在IPython中使用timeit opsum(a,b)进行测量)看起来仅比slow好3-5倍。但是,当然它的伸缩性要好得多:将数据点的数量放大100倍,将功能点的数量放大10倍,然后我们已经在考虑将速度提高10,000倍。关于python - 使用NumPy的所有行对的快速差异,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27627896/
10-13 06:40
查看更多