Extract each column of non-zero array blocks from the two-dimensional array and set 1

The specific function is the same as the previous blog post, but this is processed for each column.

import copy
import itertools
import operator
import numpy as np
L = np.array([[0,0,0,0,0,0,0,0,0],[0,2,0,6,0,0,0,0,0],[1,3,5,5,2,0,0,1,5],[4,2,0,0,4,0,2,1,0],[1,0,0,0,2,1,3,0,0],[0,0,0,0,0,4,2,0,0],[0,0,0,0,0,1,0,0,0]])
L1 = copy.deepcopy(L)
L2 = copy.deepcopy(L)
[a, b] = np.shape(L)
zipped_dataset = np.zeros((a, b))

for i in range(b):
    col_array = L[:,i]
    col_nonzeros_index = np.where(col_array != 0)
    L1[:,i][col_nonzeros_index] = 1
    col_nonzeros_index_array = [[i for i, value in it] for key, it in
                                itertools.groupby(enumerate(L1[:,i]), key=operator.itemgetter(1)) if key != 0]
    for j in range(len(col_nonzeros_index_array)):
        number_L2 = col_nonzeros_index_array[j]
        if np.size(number_L2) > 1:
            mean_value = np.mean(np.array(L2[:,i][number_L2]))
            min_value_index = (np.abs(L2[:,i][number_L2] - mean_value)).argmax()
            relativity_index = number_L2[min_value_index]
            zipped_dataset[:,i][relativity_index] = 1
        else:
            zipped_dataset[:,i][number_L2] = 1
print(L)
print(zipped_dataset)

The input array is:

[[0 0 0 0 0 0 0 0 0]
 [0 2 0 6 0 0 0 0 0]
 [1 3 5 5 2 0 0 1 5]
 [4 2 0 0 4 0 2 1 0]
 [1 0 0 0 2 1 3 0 0]
 [0 0 0 0 0 4 2 0 0]
 [0 0 0 0 0 1 0 0 0]]

The output array is:

[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 1. 1. 0. 0. 0. 0. 1. 1.]
 [1. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]]

It can be seen that after the non-zero number blocks of each column are extracted, the maximum value is set to 1, and it is set to 0 with the non-zero number

Guess you like

Origin blog.csdn.net/kakangel/article/details/84998699