Index of top k largest in array python

If the input is a vector (rank=1), finds the k largest entries in the vector and outputs their values and indices as vectors. Thus

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

1 is the

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

2-th largest entry in

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

3, and its index is

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

4.

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

For matrices (resp. higher rank input), computes the top k entries in each row (resp. vector along the last dimension). Thus,

input = tf.random.normal(shape=(3,4,5,6)) k = 2 values, indices = tf.math.top_k(input, k=k) values.shape.as_list() [3, 4, 5, 2] values.shape == indices.shape == input.shape[:-1] + [k] True

The indices can be used to

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

6 from a tensor who's shape matches

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

3.

gathered_values = tf.gather(input, indices, batch_dims=-1) assert tf.reduce_all(gathered_values == values)

If two elements are equal, the lower-index element appears first.

result = tf.math.top_k([1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0], k=3) result.indices.numpy() array([0, 1, 3], dtype=int32)

By default, indices are returned as type

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

8, however, this can be changed by specifying the

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

9.

result = tf.math.top_k([1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0], k=3, index_type=tf.int16) result.indices.numpy() array([0, 1, 3], dtype=int16)

Args

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

3 1-D or higher

input = tf.random.normal(shape=(3,4,5,6)) k = 2 values, indices = tf.math.top_k(input, k=k) values.shape.as_list() [3, 4, 5, 2] values.shape == indices.shape == input.shape[:-1] + [k] True

1 with last dimension at least k.`k`0-D

input = tf.random.normal(shape=(3,4,5,6)) k = 2 values, indices = tf.math.top_k(input, k=k) values.shape.as_list() [3, 4, 5, 2] values.shape == indices.shape == input.shape[:-1] + [k] True

1 of type

input = tf.random.normal(shape=(3,4,5,6)) k = 2 values, indices = tf.math.top_k(input, k=k) values.shape.as_list() [3, 4, 5, 2] values.shape == indices.shape == input.shape[:-1] + [k] True

5,

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

8 or

input = tf.random.normal(shape=(3,4,5,6)) k = 2 values, indices = tf.math.top_k(input, k=k) values.shape.as_list() [3, 4, 5, 2] values.shape == indices.shape == input.shape[:-1] + [k] True

7. Number of top element to look for along the last dimension (along each row for matrices).

input = tf.random.normal(shape=(3,4,5,6)) k = 2 values, indices = tf.math.top_k(input, k=k) values.shape.as_list() [3, 4, 5, 2] values.shape == indices.shape == input.shape[:-1] + [k] True

8 If true the resulting k elements will be sorted by the values in descending order.

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

9 Optional dtype for output indices.

gathered_values = tf.gather(input, indices, batch_dims=-1) assert tf.reduce_all(gathered_values == values)

1 Optional name for the operation.

Returns

A tuple with two named fields:

gathered_values = tf.gather(input, indices, batch_dims=-1) assert tf.reduce_all(gathered_values == values)

2 The k largest elements along each last dimensional slice.

gathered_values = tf.gather(input, indices, batch_dims=-1) assert tf.reduce_all(gathered_values == values)

4 The indices of

gathered_values = tf.gather(input, indices, batch_dims=-1) assert tf.reduce_all(gathered_values == values)

2 within the last dimension of

result = tf.math.top_k([1, 2, 98, 1, 1, 99, 3, 1, 3, 96, 4, 1], k=3) result.values.numpy() array([99, 98, 96], dtype=int32) result.indices.numpy() array([5, 2, 9], dtype=int32)

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.

Last updated 2023-09-27 UTC.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]

How do you find the top K value in a NumPy array?

To find the top k:.

top_k = np. partition(x, -k)[-k:] # Results are not sorted np. ... .

bottom_k = np. partition(x, k)[:k] # Results are not sorted np. ... .

idx = np. argpartition(x, -k)[-k:] # Indices not sorted idx[np. ... .

idx = np. argpartition(x, k)[:k] # Indices not sorted idx[np..

How to get top 5 values in NumPy array?

The np. argsort() function sorts the numpy array in ascending order and returns their indices. Since the problem statement wants us to find the indices of N maximum numbers, we use [::-1] to reverse the array. Lastly, we use [:N] to get the first N values.

How do you find the index of n largest elements in Python?

argsort() function to get the indices of the sorted numpy array in ascending order. Use negative indexing and extract the last N indices from the sorted array. Use the extracted indices to get the corresponding elements from the original numpy array. Return the N maximum elements from the original list.

How do you find the highest value in an array in Python?

The max() function returns the item with the highest value, or the item with the highest value in an iterable. If the values are strings, an alphabetically comparison is done.