Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 二维阵列上滑动窗口分类器1-in-N分类的有效多数投票_Python_Arrays_Python 3.x_Numpy_Matrix Indexing - Fatal编程技术网

Python 二维阵列上滑动窗口分类器1-in-N分类的有效多数投票

Python 二维阵列上滑动窗口分类器1-in-N分类的有效多数投票,python,arrays,python-3.x,numpy,matrix-indexing,Python,Arrays,Python 3.x,Numpy,Matrix Indexing,简短版本:我想使用2D数组中的值来索引较大数组的相应子集的第三维,然后增加这些元素 我将非常感谢您能帮助我们更快地将两种投票算法结合起来。实际上,在数组上滑动分类器并计算最佳步长并不是重点 长版本: 我有一个算法,它将R1xC1 2D数组中的每个元素分类为N个类中的1个 我想对尺寸为R2xC2的较大2D数组进行分类。我不想将较大的数组细分为多个R1xC1 2D数组,而是希望将分类器滑动到较大的数组上,以便对较大数组中的每个元素进行多次分类。这意味着我将有一个R2xC2xN数组来存储结果,当窗口在

简短版本:我想使用2D数组中的值来索引较大数组的相应子集的第三维,然后增加这些元素

我将非常感谢您能帮助我们更快地将两种投票算法结合起来。实际上,在数组上滑动分类器并计算最佳步长并不是重点

长版本: 我有一个算法,它将R1xC1 2D数组中的每个元素分类为N个类中的1个

我想对尺寸为R2xC2的较大2D数组进行分类。我不想将较大的数组细分为多个R1xC1 2D数组,而是希望将分类器滑动到较大的数组上,以便对较大数组中的每个元素进行多次分类。这意味着我将有一个R2xC2xN数组来存储结果,当窗口在大数组中滑动时,窗口中的每个像素将增加三维元素中的一个(即N类中的一个)

在所有滑动完成后,我们可以简单地获得分类对应维度中的argmax,以获得每元素分类

我打算将其放大,以对一个包含数百万像素和几十个像素的数组进行分类,因此我关心的是使用分类结果在每个元素的分类维度中增加一个值的效率

下面是这个问题的玩具版本,我整个晚上都在用Python 3制作。它有一个简单的double-for-loop实现,通过索引旋转和一些智能索引获得了一个稍好的实现。分类器只是随机的

import numpy as np

map_rows = 8
map_cols = 10
num_candidates = 3
vote_rows = 6
vote_cols = 5


def display_tally(the_tally):
    print("{:25s}{:25s}{:25s}".format("Class 0", "Class 1", "Class 2"))
    for i in range(map_rows):
        for k in range(num_candidates):
            for j in range(map_cols):
                print("{:<2}".format(the_tally[i, j, k]), end='')
            print("     ", end='')
        print("")


def incorporate_votes(current_tally, this_vote, left, top):
    for i in range(vote_rows):
        for j in range(vote_cols):
            current_tally[top + i, left + j, this_vote[i, j]] += 1
    return current_tally


def incorporate_votes2(current_tally, this_vote, left, top):
    for i in range(num_candidates):
        current_tally[i, top:top + vote_rows, left:left + vote_cols][this_vote == i] += 1
    return current_tally


tally = np.zeros((map_rows, map_cols, num_candidates), dtype=int)
swizzled_tally = np.zeros((num_candidates, map_rows, map_cols), dtype=int)
print("Before voting")
display_tally(tally)

print("\n Votes from classifier A (centered at (2,2))")
votes = np.random.randint(num_candidates, size=vote_rows*vote_cols).reshape((vote_rows, vote_cols))
print(votes)

tally = incorporate_votes(tally, votes, 0, 0)
swizzled_tally = incorporate_votes2(swizzled_tally, votes, 0, 0)

print("\nAfter classifier A voting (centered at (2,2))")
display_tally(tally)

print("\n Votes from classifier B (Centered at (5, 4))")
votes2 = np.random.randint(num_candidates, size=vote_rows*vote_cols).reshape((vote_rows, vote_cols))
print(votes2)

tally = incorporate_votes(tally, votes2, 3, 2)
swizzled_tally = incorporate_votes2(swizzled_tally, votes2, 3, 2)

print("\nAfter classifier B voting (Centered at (5, 4))")
print("Naive vote counting")
display_tally(tally)
print("\nSwizzled vote counting")
display_tally(np.moveaxis(swizzled_tally, [-2, -1], [0, 1]))

new_tally = np.moveaxis(tally, -1, 0)
classifications = np.argmax(swizzled_tally, axis=0)
print("\nNaive classifications")
print(classifications)

print("\nSwizzled classifications")
classifications = np.argmax(tally, axis=2)
print(classifications)

也许值得在YouTube上给Grant of 3Blue1Brown一个戳戳。
Before voting
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      

 Votes from classifier A (centered at (2,2))
[[1 1 2 2 1]
 [0 2 0 2 1]
 [0 2 2 0 2]
 [1 1 1 2 0]
 [1 0 0 2 1]
 [2 1 1 1 0]]

After classifier A voting (centered at (2,2))
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      1 1 0 0 1 0 0 0 0 0      0 0 1 1 0 0 0 0 0 0      
1 0 1 0 0 0 0 0 0 0      0 0 0 0 1 0 0 0 0 0      0 1 0 1 0 0 0 0 0 0      
1 0 0 1 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 1 1 0 1 0 0 0 0 0      
0 0 0 0 1 0 0 0 0 0      1 1 1 0 0 0 0 0 0 0      0 0 0 1 0 0 0 0 0 0      
0 1 1 0 0 0 0 0 0 0      1 0 0 0 1 0 0 0 0 0      0 0 0 1 0 0 0 0 0 0      
0 0 0 0 1 0 0 0 0 0      0 1 1 1 0 0 0 0 0 0      1 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      

 Votes from classifier B (Centered at (5, 4))
[[2 2 2 0 0]
 [0 1 2 1 2]
 [2 0 0 2 0]
 [2 2 1 1 1]
 [1 2 0 2 1]
 [1 1 1 1 2]]

After classifier B voting (Centered at (5, 4))
Naive vote counting
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      1 1 0 0 1 0 0 0 0 0      0 0 1 1 0 0 0 0 0 0      
1 0 1 0 0 0 0 0 0 0      0 0 0 0 1 0 0 0 0 0      0 1 0 1 0 0 0 0 0 0      
1 0 0 1 0 0 1 1 0 0      0 0 0 0 0 0 0 0 0 0      0 1 1 1 2 1 0 0 0 0      
0 0 0 1 1 0 0 0 0 0      1 1 1 0 1 0 1 0 0 0      0 0 0 1 0 1 0 1 0 0      
0 1 1 0 1 1 0 1 0 0      1 0 0 0 1 0 0 0 0 0      0 0 0 2 0 0 1 0 0 0      
0 0 0 0 1 0 0 0 0 0      0 1 1 1 0 1 1 1 0 0      1 0 0 1 1 0 0 0 0 0      
0 0 0 0 0 1 0 0 0 0      0 0 0 1 0 0 0 1 0 0      0 0 0 0 1 0 1 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 1 1 1 1 0 0 0      0 0 0 0 0 0 0 1 0 0      

Swizzled vote counting
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      1 1 0 0 1 0 0 0 0 0      0 0 1 1 0 0 0 0 0 0      
1 0 1 0 0 0 0 0 0 0      0 0 0 0 1 0 0 0 0 0      0 1 0 1 0 0 0 0 0 0      
1 0 0 1 0 0 1 1 0 0      0 0 0 0 0 0 0 0 0 0      0 1 1 1 2 1 0 0 0 0      
0 0 0 1 1 0 0 0 0 0      1 1 1 0 1 0 1 0 0 0      0 0 0 1 0 1 0 1 0 0      
0 1 1 0 1 1 0 1 0 0      1 0 0 0 1 0 0 0 0 0      0 0 0 2 0 0 1 0 0 0      
0 0 0 0 1 0 0 0 0 0      0 1 1 1 0 1 1 1 0 0      1 0 0 1 1 0 0 0 0 0      
0 0 0 0 0 1 0 0 0 0      0 0 0 1 0 0 0 1 0 0      0 0 0 0 1 0 1 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 1 1 1 1 0 0 0      0 0 0 0 0 0 0 1 0 0      

Naive classifications
[[1 1 2 2 1 0 0 0 0 0]
 [0 2 0 2 1 0 0 0 0 0]
 [0 2 2 0 2 2 0 0 0 0]
 [1 1 1 0 0 2 1 2 0 0]
 [1 0 0 2 0 0 2 0 0 0]
 [2 1 1 1 0 1 1 1 0 0]
 [0 0 0 1 2 0 2 1 0 0]
 [0 0 0 1 1 1 1 2 0 0]]

Swizzled classifications
[[1 1 2 2 1 0 0 0 0 0]
 [0 2 0 2 1 0 0 0 0 0]
 [0 2 2 0 2 2 0 0 0 0]
 [1 1 1 0 0 2 1 2 0 0]
 [1 0 0 2 0 0 2 0 0 0]
 [2 1 1 1 0 1 1 1 0 0]
 [0 0 0 1 2 0 2 1 0 0]
 [0 0 0 1 1 1 1 2 0 0]]