选择最快的方法
这个问题的答案提供了一种很好的方法来替换numpy数组中的元素.让我们检查,哪一个是最快的.
TL; DR:Numpy索引是赢家
def meth1(): # suggested by @Slam
for old,new in Y:
Xold[Xold == old] = new
def meth2(): # suggested by myself,convert y_dict = dict(Y) first
[y_dict[i] if i in y_dict.keys() else i for i in Xold]
def meth3(): # suggested by @Eelco Hoogendoom,import numpy_index as npi first
npi.remap(Xold,keys=Y[:,values=Y[:,1])
def meth4(): # suggested by @Brad Solomon,import pandas as pd first
pd.Series(Xold).map(pd.Series(Y[:,1],index=Y[:,0])).values
# suggested by @jdehesa. create Xnew = Xold.copy() and index
# idx = np.searchsorted(Xold,Y[:,0]) first
def meth5():
Xnew[idx] = Y[:,1]
结果并不那么令人惊讶
In [39]: timeit.timeit(meth1,number=1000000)
Out[39]: 12.08
In [40]: timeit.timeit(meth2,number=1000000)
Out[40]: 2.87
In [38]: timeit.timeit(meth3,number=1000000)
Out[38]: 55.39
In [12]: timeit.timeit(meth4,number=1000000)
Out[12]: 256.84
In [50]: timeit.timeit(meth5,number=1000000)
Out[50]: 1.12
因此,良好的旧列表理解是第二快的,并且获胜的方法是结合searchsorted()的numpy索引.