小编wea*_*guy的帖子

快速python numpy功能在哪里?

我在几个for循环中多次使用numpy的函数,但它变得太慢了.有没有办法更快地执行此功能?我读过你应该尝试在线循环,以及在for循环之前为函数创建局部变量,但似乎没有什么能提高速度(<1%).在len(UNIQ_IDS)〜800 emiss_dataobj_data有numpy的ndarrays具有形状=(2600,5200).我用import profile得到的瓶颈在哪里手柄,并且wherefor循环是一个大的.

import numpy as np
max = np.max
where = np.where
MAX_EMISS = [max(emiss_data[where(obj_data == i)]) for i in UNIQ_IDS)]
Run Code Online (Sandbox Code Playgroud)

python performance for-loop numpy where

10
推荐指数
3
解决办法
5438
查看次数

在numpy数组中查找条件索引的最快方法

我试图找到在二维 numpy 数组上获得 numpy 'where' 语句功能的最快方法;即检索满足条件的索引。它比我使用过的其他语言(例如 IDL、Matlab)慢得多。

我已经一个在嵌套 for 循环中遍历数组的函数进行了cythonized。速度几乎提高了一个数量级,但如果可能的话,我想进一步提高性能。

测试.py:

from cython_where import *
import time
import numpy as np

data = np.zeros((2600,5200))
data[100:200,100:200] = 10

t0 = time.time()
inds,ct = cython_where(data,'EQ',10)
print time.time() - t0

t1 = time.time()
tmp = np.where(data == 10)
print time.time() - t1
Run Code Online (Sandbox Code Playgroud)

我的 cython_where.pyx 程序:

from __future__ import division
import numpy as np
cimport numpy as np
cimport cython

DTYPE1 = np.float
ctypedef np.float_t DTYPE1_t
DTYPE2 = np.int
ctypedef …
Run Code Online (Sandbox Code Playgroud)

python performance numpy cython

5
推荐指数
1
解决办法
3778
查看次数

python increment运算符在一行条件语句中有奇怪的行为

idcter超过时为什么不重置为0 maxid

maxid=9999
idcter=9999
idcter += 1 if(idcter <= maxid) else 0
print('this is good: ' + str(idcter))

idcter += 1 if(idcter <= maxid) else 0
print('now this is weird: ' + str(idcter))

idcter=10000
idcter = idcter + 1 if(idcter <= maxid) else 0
print("that's better: " + str(idcter))
Run Code Online (Sandbox Code Playgroud)

输出:

this is good: 10000
now this is weird: 10000
that's better: 0
Run Code Online (Sandbox Code Playgroud)

所以这是一个简单的修复,但是为什么在超过之后会不会重置maxid

python increment

0
推荐指数
1
解决办法
146
查看次数

标签 统计

python ×3

numpy ×2

performance ×2

cython ×1

for-loop ×1

increment ×1

where ×1