-3
我寫了一個函數來使用座標下降執行套索迴歸。在Python中的If條件中使用遞歸調用
的代碼如下:
def lasso_cyclical_coordinate_descent(feature_matrix, output, weights, l1_penalty, tolerance):
for i in range(len(weights)):
old_weights_i = weights[i]
weights[i] = lasso_coordinate_descent_step(i, feature_matrix, output, weights, l1_penalty)
diff = []
diff.append(abs(old_weights_i - weights[i]))
if max(diff) > tolerance:
weights = lasso_cyclical_coordinate_descent(feature_matrix, output, weights, l1_penalty, tolerance)
return weights
我在這裏的想法是,運行如果條件內的遞歸調用將導致權重返回,只有當條件不滿足,那就是當我有我想要的結果。在此之前,函數將繼續遞歸調用自身。
我的邏輯正確嗎?我和別人討論過這件事,他說沒有,但沒有進一步解釋。
'return'語句需要縮進以便它是函數的一部分。 – Barmar
對不起,在代碼中,我忘了在這裏縮進它。 – Sabhijiit
我不確定我明白你爲什麼要問我們這個代碼。爲什麼不運行它,看看它是如何做的?我們無法對其進行測試(因爲它不是[mcve]),所以我們沒有太多的貢獻。 – Blckknght