numpy find the max value in a row and return back to it's column index
I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.
array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])
Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!
for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))
python numpy
add a comment |
I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.
array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])
Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!
for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))
python numpy
Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29
numpy.ndarray.argmaxand specifyaxis.
– jpp
Nov 12 at 11:30
You can usenp.argmaxwithaxis=1to get the column index of the maximum value in each row.
– jdehesa
Nov 12 at 11:30
add a comment |
I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.
array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])
Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!
for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))
python numpy
I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.
array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])
Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!
for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))
python numpy
python numpy
asked Nov 12 at 11:27
Shin Yu Wu
968
968
Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29
numpy.ndarray.argmaxand specifyaxis.
– jpp
Nov 12 at 11:30
You can usenp.argmaxwithaxis=1to get the column index of the maximum value in each row.
– jdehesa
Nov 12 at 11:30
add a comment |
Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29
numpy.ndarray.argmaxand specifyaxis.
– jpp
Nov 12 at 11:30
You can usenp.argmaxwithaxis=1to get the column index of the maximum value in each row.
– jdehesa
Nov 12 at 11:30
Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29
Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29
numpy.ndarray.argmax and specify axis.– jpp
Nov 12 at 11:30
numpy.ndarray.argmax and specify axis.– jpp
Nov 12 at 11:30
You can use
np.argmax with axis=1 to get the column index of the maximum value in each row.– jdehesa
Nov 12 at 11:30
You can use
np.argmax with axis=1 to get the column index of the maximum value in each row.– jdehesa
Nov 12 at 11:30
add a comment |
2 Answers
2
active
oldest
votes
Use np.argmax.
Demo:
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.argmax(a, axis=1)
array([4, 4])
You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.
Another demo:
>>> a
array([[5, 9, 7, 6, 8],
[8, 7, 7, 6, 9]])
>>> np.argmax(a, axis=1)
array([1, 4])
add a comment |
Numpy quite mathematical sometimes. Try with
argmax
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261196%2fnumpy-find-the-max-value-in-a-row-and-return-back-to-its-column-index%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use np.argmax.
Demo:
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.argmax(a, axis=1)
array([4, 4])
You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.
Another demo:
>>> a
array([[5, 9, 7, 6, 8],
[8, 7, 7, 6, 9]])
>>> np.argmax(a, axis=1)
array([1, 4])
add a comment |
Use np.argmax.
Demo:
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.argmax(a, axis=1)
array([4, 4])
You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.
Another demo:
>>> a
array([[5, 9, 7, 6, 8],
[8, 7, 7, 6, 9]])
>>> np.argmax(a, axis=1)
array([1, 4])
add a comment |
Use np.argmax.
Demo:
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.argmax(a, axis=1)
array([4, 4])
You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.
Another demo:
>>> a
array([[5, 9, 7, 6, 8],
[8, 7, 7, 6, 9]])
>>> np.argmax(a, axis=1)
array([1, 4])
Use np.argmax.
Demo:
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.argmax(a, axis=1)
array([4, 4])
You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.
Another demo:
>>> a
array([[5, 9, 7, 6, 8],
[8, 7, 7, 6, 9]])
>>> np.argmax(a, axis=1)
array([1, 4])
answered Nov 12 at 11:31
timgeb
48.8k116390
48.8k116390
add a comment |
add a comment |
Numpy quite mathematical sometimes. Try with
argmax
add a comment |
Numpy quite mathematical sometimes. Try with
argmax
add a comment |
Numpy quite mathematical sometimes. Try with
argmax
Numpy quite mathematical sometimes. Try with
argmax
edited Nov 12 at 13:54
answered Nov 12 at 11:30
user3142459
448314
448314
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261196%2fnumpy-find-the-max-value-in-a-row-and-return-back-to-its-column-index%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29
numpy.ndarray.argmaxand specifyaxis.– jpp
Nov 12 at 11:30
You can use
np.argmaxwithaxis=1to get the column index of the maximum value in each row.– jdehesa
Nov 12 at 11:30