Hướng dẫn find index of k smallest element in list python - tìm chỉ số của k phần tử nhỏ nhất trong danh sách python

Sử dụng np.argpartition. Nó không sắp xếp toàn bộ mảng. Nó chỉ đảm bảo rằng phần tử

print(A[idx[:k]])
# [ 0.1  1.   1.5]
0 ở vị trí được sắp xếp và tất cả các phần tử nhỏ hơn sẽ được di chuyển trước nó. Do đó, các yếu tố
print(A[idx[:k]])
# [ 0.1  1.   1.5]
1 đầu tiên sẽ là các yếu tố Kmallest.

import numpy as np

A = np.array([1, 7, 9, 2, 0.1, 17, 17, 1.5])
k = 3

idx = np.argpartition(A, k)
print(idx)
# [4 0 7 3 1 2 6 5]

Điều này trả về các giá trị Kmallest nhất. Lưu ý rằng những điều này có thể không theo thứ tự sắp xếp.

print(A[idx[:k]])
# [ 0.1  1.   1.5]

Để có được các giá trị lớn nhất sử dụng

idx = np.argpartition(A, -k)
# [4 0 7 3 1 2 6 5]

A[idx[-k:]]
# [  9.  17.  17.]

Cảnh báo: Không (Re) sử dụng

print(A[idx[:k]])
# [ 0.1  1.   1.5]
2 để có được K lớn nhất. Điều đó sẽ không luôn luôn hoạt động. Ví dụ: đây không phải là 3 giá trị lớn nhất trong
print(A[idx[:k]])
# [ 0.1  1.   1.5]
3:

x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
idx = np.argpartition(x, 3)
x[idx[-3:]]
array([ 70,  80, 100])

Dưới đây là một so sánh với

print(A[idx[:k]])
# [ 0.1  1.   1.5]
4, cũng hoạt động nhưng chỉ sắp xếp toàn bộ mảng để có được kết quả.

In [2]: x = np.random.randn(100000)

In [3]: %timeit idx0 = np.argsort(x)[:100]
100 loops, best of 3: 8.26 ms per loop

In [4]: %timeit idx1 = np.argpartition(x, 100)[:100]
1000 loops, best of 3: 721 µs per loop

In [5]: np.alltrue(np.sort(np.argsort(x)[:100]) == np.sort(np.argpartition(x, 100)[:100]))
Out[5]: True

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Trong bài viết này, chúng ta hãy xem cách tìm số k của các giá trị nhỏ nhất từ ​​một mảng numpy.

    Phương pháp 1: Sử dụng np.sort (). & Nbsp;

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    5
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    6
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    7
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    8
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    9

    In [2]: x = np.random.randn(100000)
    
    In [3]: %timeit idx0 = np.argsort(x)[:100]
    100 loops, best of 3: 8.26 ms per loop
    
    In [4]: %timeit idx1 = np.argpartition(x, 100)[:100]
    1000 loops, best of 3: 721 µs per loop
    
    In [5]: np.alltrue(np.sort(np.argsort(x)[:100]) == np.sort(np.argpartition(x, 100)[:100]))
    Out[5]: True
    
    0
    print(A[idx[:k]])
    # [ 0.1  1.   1.5]
    
    8
    In [2]: x = np.random.randn(100000)
    
    In [3]: %timeit idx0 = np.argsort(x)[:100]
    100 loops, best of 3: 8.26 ms per loop
    
    In [4]: %timeit idx1 = np.argpartition(x, 100)[:100]
    1000 loops, best of 3: 721 µs per loop
    
    In [5]: np.alltrue(np.sort(np.argsort(x)[:100]) == np.sort(np.argpartition(x, 100)[:100]))
    Out[5]: True
    
    2

    In [2]: x = np.random.randn(100000)
    
    In [3]: %timeit idx0 = np.argsort(x)[:100]
    100 loops, best of 3: 8.26 ms per loop
    
    In [4]: %timeit idx1 = np.argpartition(x, 100)[:100]
    1000 loops, best of 3: 721 µs per loop
    
    In [5]: np.alltrue(np.sort(np.argsort(x)[:100]) == np.sort(np.argpartition(x, 100)[:100]))
    Out[5]: True
    
    3
    print(A[idx[:k]])
    # [ 0.1  1.   1.5]
    
    8

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    5
    The original list is : [5, 6, 10, 4, 7, 1, 19]
    Indices list of min K elements is : [5, 3, 0]
    
    6
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    7
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    8
    The original list is : [5, 6, 10, 4, 7, 1, 19]
    Indices list of min K elements is : [5, 3, 0]
    
    9

    Đầu ra:

    The original list is : [5, 6, 10, 4, 7, 1, 19]
    Indices list of min K elements is : [5, 3, 0]
    

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    

    Bàn luận Using np.sort() . 

    Approach:

    1. Trong bài viết này, chúng ta hãy xem cách tìm số k của các giá trị nhỏ nhất từ ​​một mảng numpy.
    2. Phương pháp 1: Sử dụng np.sort (). & Nbsp;
    3. Tạo một mảng numpy.
    4. Xác định giá trị của k.

    Python3

    Sắp xếp mảng theo thứ tự tăng dần bằng phương thức sort ().

    In các giá trị k đầu tiên của mảng được sắp xếp.

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    5
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    1
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    2

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    4

    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    0
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    1

    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    2
    print(A[idx[:k]])
    # [ 0.1  1.   1.5]
    
    8
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    4
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    5
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    7
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    0
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1
    In [2]: x = np.random.randn(100000)
    
    In [3]: %timeit idx0 = np.argsort(x)[:100]
    100 loops, best of 3: 8.26 ms per loop
    
    In [4]: %timeit idx1 = np.argpartition(x, 100)[:100]
    1000 loops, best of 3: 721 µs per loop
    
    In [5]: np.alltrue(np.sort(np.argsort(x)[:100]) == np.sort(np.argpartition(x, 100)[:100]))
    Out[5]: True
    
    2
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1____
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    6____21__

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4np.argpartition2np.argpartition3
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    2

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4np.argpartition6

    Output:

    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [1 3 4 5]
    

    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    5
    print(A[idx[:k]])
    # [ 0.1  1.   1.5]
    
    8
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    6
    Using np.argpartition() 

    Approach:

    1. Trong bài viết này, chúng ta hãy xem cách tìm số k của các giá trị nhỏ nhất từ ​​một mảng numpy.
    2. Phương pháp 1: Sử dụng np.sort (). & Nbsp;
    3. Tạo một mảng numpy.
    4. Xác định giá trị của k.

    Python3

    Sắp xếp mảng theo thứ tự tăng dần bằng phương thức sort ().

    In các giá trị k đầu tiên của mảng được sắp xếp.

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    5
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    1
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    2

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    4

    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    0
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    1

    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    2
    print(A[idx[:k]])
    # [ 0.1  1.   1.5]
    
    8
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    4
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    5
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1
    Input: [1,3,5,2,4,6] 
    k = 3
    
    Output: [1,2,3] 
    
    7
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1
    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    0
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1
    In [2]: x = np.random.randn(100000)
    
    In [3]: %timeit idx0 = np.argsort(x)[:100]
    100 loops, best of 3: 8.26 ms per loop
    
    In [4]: %timeit idx1 = np.argpartition(x, 100)[:100]
    1000 loops, best of 3: 721 µs per loop
    
    In [5]: np.alltrue(np.sort(np.argsort(x)[:100]) == np.sort(np.argpartition(x, 100)[:100]))
    Out[5]: True
    
    2
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    1____
    idx = np.argpartition(A, -k)
    # [4 0 7 3 1 2 6 5]
    
    A[idx[-k:]]
    # [  9.  17.  17.]
    
    6____21__

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4np.argpartition2np.argpartition3
    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    
    2

    x = np.array([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0])
    idx = np.argpartition(x, 3)
    x[idx[-3:]]
    array([ 70,  80, 100])
    
    4
    print(A[idx[:k]])
    # [ 0.1  1.   1.5]
    
    33

    Output:

    The Original Array Content
    [23 12  1  3  4  5  6]
    4 smallest elements of the array
    [4 3 1 5]
    

    Làm thế nào để bạn tìm thấy chỉ số của số nhỏ nhất trong danh sách trong Python?

    Hàm Inbuilt của Python cho phép chúng tôi tìm thấy nó trong một dòng, chúng tôi có thể tìm thấy mức tối thiểu trong danh sách bằng hàm min () và sau đó sử dụng hàm index () để tìm ra chỉ mục của phần tử tối thiểu đó.using the min() function and then use the index() function to find out the index of that minimum element.

    Làm thế nào để bạn tìm thấy chỉ số của giá trị tối thiểu trong một mảng trong Python?

    Danh sách sử dụng ...
    a_list = [3, 2, 1].
    min_value = min (a_list) Tìm giá trị tối thiểu của `a_list`.
    min_index = a_list.Chỉ mục (MIN_VALUE) Tìm chỉ mục của giá trị tối thiểu ..
    print(min_index).

    Làm thế nào để bạn tìm thấy chỉ số của phần tử nhỏ nhất trong một mảng numpy trong Python?

    Tìm chỉ số giá trị tối thiểu:..
    # Nhận các chỉ số của phần tử tối thiểu trong mảng numpy ..
    kết quả = numpy.trong đó (mảng == numpy. amin (mảng)).
    in ('trả về mảng:', kết quả).
    in ('danh sách các chỉ số của phần tử tối thiểu:', kết quả [0]).

    Làm thế nào để bạn tìm thấy chỉ mục của một giá trị trong danh sách Python?

    Phương thức Index () trả về chỉ mục của phần tử được chỉ định trong danh sách ...
    Phần tử - Phần tử sẽ được tìm kiếm ..
    Bắt đầu (Tùy chọn) - Bắt đầu tìm kiếm từ chỉ mục này ..
    End (Tùy chọn) - Tìm kiếm phần tử lên đến chỉ mục này ..