我有以下python代码:
H1 = [[0.04,0.03,0.01,0.002],[0.02,0.04,0.001,0.5]]
H2 = [[0.06,0.02,0.02,0.004],[0.8,0.09,0.6,0.1]]
D1 = [0.01,0.02,0.1,0.01]
D2 = [0.1,0.3,0.01,0.4]
Tp = np.sum(D1)
Tn = np.sum(D2)
T = []
append2 = T.append
E = []
append3 = E.append
for h1,h2 in itertools.izip(H1,H2)
Err = []
append1 = Err.append
for v in h1:
L1 = [1 if i>=v else 0 for i in h1]
L2 = [1 if i>=v else 0 for i in h2]
Sp = np.dot(D1,L1)
Sn = np.dot(D2,L2)
err = min(Sp+Tn-Sn, Sn+Tp-Sp)
append1(err)
b = np.argmin(Err)
append2(h1[b])
append3(Err[b])
这只是一个示例代码。我需要运行内部 for 循环大约 20,000 次(这里它只运行两次)。但是内部 for 循环需要很长时间,因此使用起来不切实际。在 line profiler 中,它显示 line Sp = np.dot(D1,L1)
,Sn = np.dot(D2,L2)
并且 b = np.argmin(Err)
是最耗时的。如何减少上述代码所花费的时间。
任何帮助都感激不尽。
谢谢!