我正在創建一個查找 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
并回傳索引。k 是它將找到的索引數。如何正確修復該行,以便它找到 k 個索引(串列應為 k 長)
uj5u.com熱心網友回復:
這用三行代碼解決了您的問題。我認為根本沒有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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340415.html
上一篇:架構師之路-https底層原理
