将m行*n列数组转换为最大值为1最小值为0的相同shape数组

时间:2022-10-12 15:12:17

今天遇到个问题,给定一个m行*n列数组,将每行中最大值转换为1, 其余值转换为0,返回相同shape的一个数组,初遇到纠结了一会儿,不过很快也就搞定了,方法主要有2个, 如下:

In [54]: import numpy as np

In [55]: arr = np.random.randn(10, 5)

In [56]: arr
Out[56]:
array([[ 0.18857493, 0.46240256, -1.83704935, 1.69043403, -0.19316615],
[ 0.44804567, 0.06185933, -1.41199774, -0.80787301, -0.79040495],
[ 1.72422331, 0.55960974, 0.00684317, 0.10717335, 1.89600802],
...,
[-1.6557832 , -0.00702669, 1.48660851, 1.69618841, -0.56193109],
[-0.21719117, 0.37526781, -0.24954224, -0.29203481, -0.12656354],
[-2.34081393, -0.39387936, -1.02882634, -0.15803579, -1.73322845]]
)

方法1:

#利用pandas库
In [57]: import pandas as pd

In [58]: pd.get_dummies(arr.argmax(1))
Out[58]:
0 1 2 3 4
0 0 0 0 1 0
1 1 0 0 0 0
2 0 0 0 0 1
3 0 0 0 1 0
4 0 0 1 0 0
5 0 0 1 0 0
6 0 0 1 0 0
7 0 0 0 1 0
8 0 1 0 0 0
9 0 0 0 1 0


#利用sklearn库(这两个异曲同工)
In [59]: from sklearn.preprocessing import label_binarize

In [60]: label_binarize(arr.argmax(1),classes=list(range(5)))
Out[60]:
array([[0, 0, 0, 1, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
...,
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 1, 0]])

方法2:

#只使用numpy库,主要是利用了numpy的点乘,当然还有我最爱的map

In [61]: list(map(lambda x: x==max(x), arr)) * np.ones(shape=arr.shape)
Out[61]:
array([[ 0., 0., 0., 1., 0.],
[ 1., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 1.],
...,
[ 0., 0., 0., 1., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 0., 1., 0.]])