二维数组中提取每列非零数组块并置1

具体功能与前一篇博文一样,但是这个是针对每列进行处理的。

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)

输入数组为:

[[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]]

输出数组为:

[[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.]]

能够看到,每列的非零数块被提取之后,再最大值处置1,并将其与非零数置0

你可能感兴趣的:(Python,python,数据处理,稀疏矩阵)