我有不同的整数开始值和停止值,并且需要介于两者之间的所有整数值作为一个形状数组(theRange,finalLength)中的数组。
例子:
finalLength = 6
start = 2
stop = 3456
theRange = (stop - start) + 1
>>> array([[0, 0, 0, 0, 0, 2],
[0, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 0, 4],
...,
[0, 0, 3, 4, 5, 4],
[0, 0, 3, 4, 5, 5],
[0, 0, 3, 4, 5, 6]])
>>> array.shape (3455, 6)
因为我需要运行这个函数数十亿次,所以目前的方法是放慢速度。
目前我使用创建所需的范围np.linspace
。整数在 ( Split integer into numbers using numpy之后) 被拆分为数字。
如果最大数的位数不等于 finalLength,则添加前导零。最后,将生成的数组翻转并转置为所需的输出格式。我认为整数拆分和转置需要最多的计算时间。
时间随着 finalLength 的增加而增加:Timeit 10000 reps
finalLength = 6 --> 时间:2.815263898999546
finalLength = 12 --> 时间:4.158567378000043
finalLength = 24 --> 时间:5.038266787999419
有没有更快的方法来创建最终数组?
可重现的代码:
import numpy as np
finalLength = 6
start = 2
stop = 3456
theRange = (stop - start) + 1
def makeRangeArray(start, stop, theRange, finalLength):
# create integers within range
ll = np.array(np.linspace(start=start, stop=stop, num=theRange), dtype=np.int64)
# split integers into arrays
b = 10
n = np.ceil(np.max(np.log(ll) / np.log(b))).astype(np.int64)
d = np.arange(n)
d.shape = d.shape + (1,) * ll.ndim
out = ll // b ** d % b
# add leading zeros if necessary
if finalLength - out.shape[0] != 0:
addZeros = np.zeros([finalLength - out.shape[0], out.shape[1]], dtype=np.int64)
out = np.append(out, addZeros, axis=0) # insert zeros at the end of array
# flip
out = np.flip(out, axis=0)
# transpose to desired final output format
aaa = out.transpose().reshape((theRange, finalLength))
return aaa