lr = 0.1n_iterations = 1000m = 5theta = np.array([[1000],[989],[123],[3455]])for iterations in n_iterations: gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y) theta = theta - lr * gradients theta
执行代码后显示错误“int”不可迭代。
更多数据:
X_b = np.asanyarray(df[['area', 'bedrooms', 'age']])
来自csv文件
我使用三个参数(面积、卧室数、年龄)来预测价格,即y
请帮助我解决这个错误
回答:
n_iterations
是一个整数,正如错误所述,它不可迭代。我认为你想循环 n_iterations
次。
尝试使用 range
来实现这一点,如下所示:
lr = 0.1n_iterations = 1000m = 5theta = np.array([[1000],[989],[123],[3455]])for iterations in range(n_iterations): gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y) theta = theta - lr * gradients theta