在三次迭代语句中优化python计数

时间:2014-12-28 07:03:55

标签: python

所有,

我尝试计算满足以下条件的项目数 house id == m和转换日段id == n和邻域函数== m 其中house id由docs ['house_id']表示,日段id由docs ['transformed_dayseg_id']表示,邻域函数由self.CF/self.TF/self.BF表示。

为此,我按照以下代码进行计数。但是,它太慢了。有关优化python计数代码的想法吗?

def get_mnf_counter(self, docs, dtype):
    x = np.zeros((self.M,self.N,self.K))
    for m in range(self.M):
        for n in range(self.N):
            for k in range(self.K):
                if dtype==1: #checkin
                    x[m, n, k]=sum((np.array(docs['house_id'])==m) & (np.array(docs['transformed_dayseg_id'])==n) & (self.CF==k))
                elif dtype==2: #taxi
                    x[m, n, k]=sum((np.array(docs['house_id'])==m) & (np.array(docs['transformed_dayseg_id'])==n) & (self.TF==k))
                elif dtpe==3: #bus
                    x[m, n, k]=sum((np.array(docs['house_id'])==m) & (np.array(docs['transformed_dayseg_id'])==n) & (self.BF==k))
                else:
                    raise Exception("index of checkin/taxi/bus/ is wrong")
    return x

1 个答案:

答案 0 :(得分:2)

您可以使用itertools.product,就像这样

from itertools import product
...
...

for m, n, k in product(range(self.M), range(self.N), range(self.K)):
   ...

更多好消息是,您可以进一步优化代码,例如

from itertools import product
def get_mnf_counter(self, docs, dtype):
    x = np.zeros((self.M, self.N, self.K))

    if dtype not in (1, 2, 3):
        raise Exception("index of checkin/taxi/bus/ is wrong")

    if dtype == 1:
        value = self.CF
    elif dtype == 2:
        value = self.TF
    else:
        value = self.BF

    house_id = np.array(docs['house_id'])
    dayseg_id = np.array(docs['transformed_dayseg_id'])

    for m, n, k in product(range(self.M), range(self.N), range(self.K)):
        x[m, n, k] = sum((house_id == m) & (dayseg_id == n) & (value == k))

由于你的算法是O(N ^ 3),你在这里无能为力。