我想迭代這個帶有引數 theta、X 和 i 的函式。
hypothesis = theta[0]*X[i][0] theta[1]*X[i][1] theta[2]*X[i][2] ...
theta 是一維陣列,X 是一個二維陣列。我嘗試使用這樣的 for 回圈,但我不確定如何先遍歷所有 i fortheta[0]*X[i][0]然后運行 ??i fortheta[1]*X[i][1]等等。
for i in range(i):
for j in range(j):
hypothesis = theta[i]*X[j][i]
uj5u.com熱心網友回復:
你想用 X 的第 i 行做 theta 的點積嗎?
如果是這樣,那么您可以執行以下操作:
def dot_product(theta, x, i):
hypothesis = 0
for j in range(len(theta)):
hypothesis = theta[j] * x[i][j]
return hypothesis
或者您可以通過 Python 的生成器功能使其更簡潔:
def dot_product(theta, x, i):
return sum(theta[j] * x[i][j] for j in range(len(theta)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322611.html
