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

我有一个形状矩阵(10,10000)。对于矩阵中的每一列,我想要一个1at max 值索引和其他值0。有没有办法避免for循环?


收到一只叮咚
浏览 150回答 2
2回答

扬帆大鱼

这是使用 numpy 的一种选择。首先导入 numpy 并将矩阵转换为 numpy 数组:import numpy as npmy_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 herenew_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)

慕盖茨4494581

在稀疏存储中创建这个矩阵实际上很容易。>>> from scipy.sparse import csc_matrix>>>&nbsp;>>> m, n = 3, 7>>>&nbsp;>>> data = np.random.randint(0, 10, (m, n))>>>&nbsp;>>> dataarray([[9, 0, 0, 7, 3, 1, 3],&nbsp; &nbsp; &nbsp; &nbsp;[8, 0, 4, 4, 3, 2, 4],&nbsp; &nbsp; &nbsp; &nbsp;[2, 3, 2, 5, 7, 5, 3]])>>>&nbsp;>>> result = csc_matrix((np.ones(n), data.argmax(0), np.arange(n+1)), (m, n))>>> result<3x7 sparse matrix of type '<class 'numpy.float64'>'&nbsp; &nbsp; &nbsp; &nbsp; with 7 stored elements in Compressed Sparse Column format>>>> result.Aarray([[1., 0., 0., 1., 0., 0., 0.],&nbsp; &nbsp; &nbsp; &nbsp;[0., 0., 1., 0., 0., 0., 1.],&nbsp; &nbsp; &nbsp; &nbsp;[0., 1., 0., 0., 1., 1., 0.]])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python