如何制作一定大小的列表 (k)

How to make a list a certain size (k)

我正在创建一个查找 k 最近邻预测的函数。

def knn_predict(data, x_new, k):
    """ (tuple, number, int) -> number

    data is a tuple.
    
    data[0] are the x coordinates and 
    data[1] are the y coordinates.
    
    k is a positive nearest neighbor parameter.
    
    Returns k-nearest neighbor estimate using nearest
    neighbor parameter k at x_new.
    
    Assumes i) there are no duplicated values in data[0],
    ii) data[0] is sorted in ascending order, and
    iii) x_new falls between min(x) and max(x).
    
    >>> knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 2, 2)
    4.0
    
    >>> knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 2, 3)
    1.0

    >>> knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 8, 2)
    1.0

    >>> knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 8, 3)
    4.333333333333333
    """
    
  
    #use find_index and the x_new value for k loops to find N\/k(x_new) (list of indexes)
    #incorporate k value!!!
    nk = [find_index(data[0], x_new) for k in range(k)] #here
    #use N\/k(x_new) indexes to find correlated y values
    yvals = [data[1][val] for val in nk]
    #use correlated y values summed together divided by k to find y new
    ynew = sum(yvals) / k
    return ynew

重要的一行:

nk = [find_index(data[0], x_new) for k in range(k)] #here

末尾带有#here的行应该使用这个函数:

def find_index(x, x_new):
    """ (list, number) -> int
    
    Returns the smallest index i such that x[i] <= x_new 
    and x[i+1] >= x_new.
    
    Assumes i) there are no duplicated values in x,
    ii) x is sorted in ascending order, and
    iii) x_new falls between min(x) and max(x).
    
    >>> find_index([1, 5, 7, 9], 1)
    0

    >>> find_index([1, 5, 7, 9], 2)
    0

    >>> find_index([1, 5, 7, 9], 6)
    1

    >>> find_index([1, 5, 7, 9], 7)
    1

    >>> find_index([1, 5, 7, 9], 8)
    2

    >>> find_index([1, 5, 7, 9], 9)
    2
    """
   
    for i, element in enumerate(x):
        
        if x_new <= x[i+1] and element <= x_new:
            return i

和 return 索引。 k 是它将找到的索引数。我怎样才能正确修复该行,以便它找到 k 个索引(列表应该是 k 长)

这三行代码就解决了你的问题。我认为 find_index 根本没有用。请注意,此代码不关心条目是否按顺序排列,甚至不关心值是否在 min(x) 和 max(x) 之间。

def knn_predict(data, x_new, k):
    """ (tuple, number, int) -> number

    data is a tuple.
    
    data[0] are the x coordinates and 
    data[1] are the y coordinates.
    
    k is a positive nearest neighbor parameter.
    
    Returns k-nearest neighbor estimate using nearest
    neighbor parameter k at x_new.
    """
    
    # Find the deltas from our target to the x values.
    
    deltas = [(abs(t-x_new),y) for t,y in zip(*data)]

    # Sort the values by the distance.

    deltas.sort()

    # Return the sum of the Ys.

    return sum( d[1] for d in deltas[:k] ) / k

print( knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 2, 2) )
print( knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 2, 3) )
print( knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 8, 2) )
print( knn_predict(([0, 5, 10, 15], [1, 7, -5, 11]), 8, 3) )

输出:

4.0
1.0
1.0
4.333333333333333