【发布时间】:2013-12-09 06:24:15
【问题描述】:
我很欣赏这对其他人来说不是一个非常普遍或适用的问题,但它让我感到难过,并且可能比我想象的教我更多。
def gradient_descent((x,y)):
x = (x,y)[0]
y = (x,y)[1]
point_history_x[0] = x
point_history_y[0] = y #set initial states
while numpy.linalg.norm(f_prime((x,y))) >= 0.1: #modulus of gradient
x = x - gamma*f_prime((x,y))[0]
y = y - gamma*f_prime((x,y))[1] #find next point
numpy.append(point_history_x,x)
numpy.append(point_history_y,y) #add to history of point movement
return point_history_x, point_history_y
之前,point_history_x(和 y)被全局定义为 numpy.zeros((1))。 当针对点 (0,0) 运行时,它对两个历史数组都返回 0。当我将每个级别自己输入到 python shell 中时,它可以正常工作并创建一个变体 x 的数组;但是当我运行模块时,它只返回输入。
所有证据似乎都指向我错误地使用了 append,但就像我所说的,我在 shell 中键入它并运行良好。
真的很郁闷,任何cmets都将不胜感激。
【问题讨论】:
-
2 建议:给我们一个只有附加的例子,没有 f_prime 的东西。并演示您如何调用该函数。正如答案所暗示的,你如何使用这个函数可能比它的内部更重要。
标签: python arrays numpy append