Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

python - How to converse an array of n-column labels to 1-column?

this is a label array that I use in my classification model:

array([[1, 0, 0],
   [0, 1, 0],
   [0, 0, 1]], dtype=uint8)

But I'd like to reverse it to one column, so it will look like this:

  array([[0],
   [1],
   [2]], dtype=uint8)

Any suggestions are appreciated.

question from:https://stackoverflow.com/questions/65869353/how-to-converse-an-array-of-n-column-labels-to-1-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use np.argmax

np.argmax(arr, axis=1).reshape(arr.shape[0], 1).astype(np.int8)

# array([[0],
#        [1],
#        [2]], dtype=int8)

If you want to take always the position of the ones:

np.argmax(arr==1, axis=1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...