为了账号安全,请及时绑定邮箱和手机立即绑定

如何在矩阵中沿列将每个最大值更改为 1,其他为 0?

如何在矩阵中沿列将每个最大值更改为 1,其他为 0?

收到一只叮咚 2021-11-30 16:23:51
我有一个形状矩阵(10,10000)。对于矩阵中的每一列,我想要一个1at max 值索引和其他值0。有没有办法避免for循环?
查看完整描述

2 回答

?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

这是使用 numpy 的一种选择。首先导入 numpy 并将矩阵转换为 numpy 数组:


import numpy as np

my_mat = np.asarray(my_original_mat)

现在是一个带有小矩阵的示例:


mat = np.random.randint(1, 10, size=(4, 4))

# array([[3, 9, 3, 1],

#       [1, 4, 2, 3],

#       [8, 4, 4, 2],

#       [7, 7, 3, 7]])

new_mat = np.zeros(mat.shape)  # our zeros and ones will go here

new_mat[np.argmax(mat, axis=0), np.arange(mat.shape[1])] = 1

# array([[0., 1., 0., 0.],

#        [0., 0., 0., 0.],

#        [1., 0., 1., 0.],

#        [0., 0., 0., 1.]])

基本上使用 numpy 切片来绕过需要循环。该new_mat[np.argmax(...), np.arange(...)]行为每一列指定哪一行包含最大值,并将这些行列对设置为 1。似乎有效。


请注意,如果您有重复的最大值,这只会将第一个(最高)最大值设置为 1。


另一个选项可以为每个最大值提供 1s ,包括重复的值(我看到 jdehesa 在评论中击败了我,但为了完整起见在这里重复):


(mat == mat.max(axis=0)).astype(mat.dtype)


查看完整回答
反对 回复 2021-11-30
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

在稀疏存储中创建这个矩阵实际上很容易。


>>> from scipy.sparse import csc_matrix

>>> 

>>> m, n = 3, 7

>>> 

>>> data = np.random.randint(0, 10, (m, n))

>>> 

>>> data

array([[9, 0, 0, 7, 3, 1, 3],

       [8, 0, 4, 4, 3, 2, 4],

       [2, 3, 2, 5, 7, 5, 3]])

>>> 

>>> result = csc_matrix((np.ones(n), data.argmax(0), np.arange(n+1)), (m, n))

>>> result

<3x7 sparse matrix of type '<class 'numpy.float64'>'

        with 7 stored elements in Compressed Sparse Column format>

>>> result.A

array([[1., 0., 0., 1., 0., 0., 0.],

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

       [0., 1., 0., 0., 1., 1., 0.]])


查看完整回答
反对 回复 2021-11-30
  • 2 回答
  • 0 关注
  • 147 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信