說明:計算並存儲R = 1000個隨機值,從0-1開始爲x。 move_window_average(x,n_neighbors)從3a預先加載到內存中。計算n_neighbors 1-9範圍內x的移動窗口平均值。商店X以及每個平均值爲被叫Y.平滑值(1-9之間的鄰居)
連續名單列表的我的解決辦法:
R = 1000
n_neighbors = 9
x = [random.uniform(0,1) for i in range(R)]
Y = [moving_window_average(x, n_neighbors) for n_neighbors in range(1,n_neighbors)]
其中moving_window_average(X,N_NEIGHBORS)是一個功能如下:
def moving_window_average(x, n_neighbors=1):
n = len(x)
width = n_neighbors*2 + 1
x = [x[0]]*n_neighbors + x + [x[-1]]*n_neighbors
# To complete the function,
# return a list of the mean of values from i to i+width for all values i from 0 to n-1.
mean_values=[]
for i in range(1,n+1):
mean_values.append((x[i-1] + x[i] + x[i+1])/width)
return (mean_values)
這給了我一個錯誤,再次檢查你的用法。即使我已經測試了幾個值,但我還沒有得到爲什麼這個練習有問題。我只是誤解了一些東西?