0

我编写了一个 python 程序,我尝试对它进行 cythonize。是否有任何建议如何使 for 循环更有效,因为这需要 99% 的时间?

这是for循环:

    for i in range(l):
        b1[i] = np.nanargmin(locator[i,:]) # Closer point
        locator[i, b1[i]] = NAN # Do not consider Closer point
        b2[i] = np.nanargmin(locator[i,:]) # 2nd Closer point
        Adjacents[i,0] = np.array((Existed_Pips[b1[i]]), dtype=np.double)
        Adjacents[i,1] = np.array((Existed_Pips[b2[i]]), dtype=np.double)

这是代码的其余部分:

import numpy as np
cimport numpy as np
from libc.math cimport NAN #, isnan

def PIPs(np.ndarray[np.double_t, ndim=1, mode='c'] ys, unsigned int nofPIPs, unsigned int typeofdist):
    cdef:
        unsigned int currentstate, j, i
        np.ndarray[np.double_t, ndim=1, mode="c"] D
        np.ndarray[np.int64_t, ndim=1, mode="c"] Existed_Pips
        np.ndarray[np.int_t, ndim=1, mode="c"] xs
        np.ndarray[np.double_t, ndim=2] Adjacents, locator, Adjy, Adjx, Raw_Fire_PIPs, Raw_Fem_PIPs
        np.ndarray[np.int_t, ndim=2, mode="c"] PIP_points, b1, b2

    cdef unsigned int l = len(ys)
    xs = np.arange(0,l, dtype=np.int) # Column vector with xs
    PIP_points = np.zeros((l,1), dtype=np.int) # Binary indexation
    PIP_points[0] = 1 # One indicate the PIP points.The first two PIPs are the first and the last observation.
    PIP_points[-1] = 1
    Adjacents = np.zeros((l,2), dtype=np.double)
    currentstate = 2 # Initial PIPs

    while currentstate <= nofPIPs: #    for eachPIPs in range(nofPIPs)
        Existed_Pips = np.flatnonzero(PIP_points)
        currentstate = len(Existed_Pips)
        locator = np.full((l,currentstate), NAN, dtype=np.double) #np.int*
        for j in range(currentstate):
            locator[:,j] = np.absolute(xs-Existed_Pips[j])
        b1 = np.zeros((l,1), dtype=np.int)
        b2 = np.zeros((l,1), dtype=np.int)
        for i in range(l):
            b1[i] = np.nanargmin(locator[i,:]) # Closer point
            locator[i, b1[i]] = NAN # Do not consider Closer point
            b2[i] = np.nanargmin(locator[i,:]) # 2nd Closer point
            Adjacents[i,0] = np.array((Existed_Pips[b1[i]]), dtype=np.double)
            Adjacents[i,1] = np.array((Existed_Pips[b2[i]]), dtype=np.double)

        ##Calculate Distance
        Adjx = Adjacents        
        Adjy = np.array([ys[np.array(Adjacents[:,0], dtype=np.int)], ys[np.array(Adjacents[:,1], dtype=np.int)]]).transpose()
        Adjx[Existed_Pips,:] = NAN # Existed PIPs are not candidates for new PIP.
        Adjy[Existed_Pips,:] = NAN

        if typeofdist == 1: #Euclidean Distance
            ##[D] = EDist(ys,xs,Adjx,Adjy)
            ED = np.power(np.power((Adjx[:,1]-xs),2) + np.power((Adjy[:,1]-ys),2),(0.5)) + np.power(np.power((Adjx[:,0]-xs),2) + np.power((Adjy[:,0]-ys),2),(0.5))

        EDmax = np.nanargmax(ED)
        PIP_points[EDmax]=1

        currentstate=currentstate+1

    return np.array([Existed_Pips, ys[Existed_Pips]]).transpose()
4

1 回答 1

1

几个建议:

  1. 将调用np.nanargmin退出循环(使用axis参数让您一次对整个数组进行操作。这减少了您必须进行的 Python 函数调用的数量:

    b1 = np.nanargmin(locator,axis=1)
    locator[np.arange(locator.shape[0]),b1] = np.nan
    b2 = np.nanargmin(locator,axis=1)
    
  2. 您的分配Adjacents很奇怪-您似乎首先为右侧创建了一个长度为 1 的数组。相反,只是做

    Adjacents[i,0] = Existed_Pips[b1[i]]
    # ...
    

    但是,在这种情况下,您也可以将两行都放在循环之外,从而消除整个循环:

    Adjacents = np.vstack((Existing_Pips[b1], Existings_Pips[b2])).T
    

所有这些都依赖于 numpy 而不是 Cython 来加速,但它可能会超过你的版本。

于 2017-07-06T17:30:27.537 回答