在Python中沿轴(公共)编号高效地分配数组

在Python中沿轴(公共)编号高效地分配数组,python,numpy,vectorization,Python,Numpy,Vectorization,我有一个图像,它是我的子样本 Count=0 classim = np.zeros([temp1.shape[0],temp1.shape[1]]) for rows in range(int(np.floor(temp1.shape[0]/SAMPLE_SIZE))): for cols in range(int(np.floor(temp1.shape[1]/SAMPLE_SIZE))): classim[np.multiply(rows,SAMPLE_SIZE):

我有一个图像,它是我的子样本

Count=0
classim = np.zeros([temp1.shape[0],temp1.shape[1]])
for rows in range(int(np.floor(temp1.shape[0]/SAMPLE_SIZE))):
    for cols in range(int(np.floor(temp1.shape[1]/SAMPLE_SIZE))):

        classim[np.multiply(rows,SAMPLE_SIZE):np.multiply(rows+1,SAMPLE_SIZE),
                np.multiply(cols,SAMPLE_SIZE):np.multiply(cols+1,SAMPLE_SIZE)] = predict.argmax(axis=-1)[Count]
        Count = np.add(Count,1)
这太慢了。我从predict.argmaxaxis=-1[Count]获得标签,但当然可以以向量形式。
换句话说,如何对上述循环进行矢量化?

将行计算放在内部循环之外会有所帮助。因此,每行仅进行一次计算

其他一些整理工作提供:

classim = np.zeros_like(temp1)
predict_args = predict.argmax(axis=-1)
for rows in range(temp1.shape[0]//SAMPLE_SIZE):
    row_0 = rows * SAMPLE_SIZE
    row_1 = (rows+1) * SAMPLE_SIZE
    for cols in range(temp1.shape[1]//SAMPLE_SIZE):
        col_0 = cols * SAMPLE_SIZE
        col_1 = (cols+1) * SAMPLE_SIZE
        classim[row_0:row_1,col_0:col_1] = predict_args[Count]
        Count+=1
在我做更多之前,你需要告诉我们更多关于预测对象的信息。但这些变化会有所帮助

-编辑-

您可以利用numpy.repeat函数。这样就不需要遍历整个classim:


predict对象是一个一维数组,包含要分配的值,例如[1,4,5,1,9,11,1,4,…]。predict的长度是rows*cols end value。。示例:对于在10000x1000像素的图像上分类的样本大小=30,会出现大约10000/30 333*333个分类实例。predict的长度为333*333=110k,并包含连续存储的类标签。谢谢你的整理,有什么帮助吗。由于剪切数的原因,这个过程目前非常缓慢。我已经更新了我的答案,但您可能需要查看您的预测数组。如果如您所说是1D,predict.argmaxaxis=-1将返回一个值。除非你是说阿格索特?你是对的。我的错。predict是nD,predict.argmaxaxis=-1是1D。我会试试你最新的答案。我会接受你的回答,不管结果如何,我都会带着性能更新回来。谢谢你抽出时间,谢谢。再次你好。我最终实现了np.repeat-method,以及其他一些更小的瓶颈循环。对于文章中的循环,对于中等大小的阵列50000 x 50000,它显示了3倍的改进。经过一天的代码重构,并提供了关于!Mx1M它显示了大约20倍的改进。那太疯狂了。谢谢你,科林。
SAMPLE_SIZE = 2
temp1 = np.arange(20*20).reshape((20,20))

sample_shape = (temp1.shape[0]//SAMPLE_SIZE, temp1.shape[0]//SAMPLE_SIZE)

#This line should work as per your question, but returns a single value
#predict_args = predict.argmax(axis=-1)
#Use this for illustration purposes
predict_args = np.arange(sample_shape[0] * sample_shape[1])


subsampled = predict_args.reshape(sample_shape)
classim = np.repeat(np.repeat(subsampled,SAMPLE_SIZE,axis =1),SAMPLE_SIZE, axis=0)


print(subsampled)
print(classim)