二维数组中提取每列非零数组块并置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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值