로 배열 요소를 설정 내가 오류를 받고 있어요 scipy.optimize
의 fmin
를 사용하는 경우 이해가 안 : 여기 Scipy는 ValueError를 FMIN 최적화 : 시퀀스
ValueError: setting an array element with a sequence.
입증하는 간단한 제곱 오차의 예입니다 :
import numpy as np
from scipy.optimize import fmin
def cost_function(theta, X, y):
m = X.shape[0]
error = X.dot(theta) - y
J = 1/(2*m) * error.T.dot(error)
return J
X = np.array([[1., 1.],
[1., 2.],
[1., 3.],
[1., 4.]])
y = np.array([[2],[4],[6],[8]])
initial_theta = np.ones((X.shape[1], 1)) * 0.01
# test cost_function
print cost_function(initial_theta, X, y)
# [[ 14.800675]] seems okay...
# but then error here...
theta = fmin(cost_function, initial_theta, args=(X, y))
#Traceback (most recent call last):
# File "C:\Users\me\test.py", line 21, in <module>
# theta = fmin(cost_function, initial_theta, args=(X, y))
# File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 278, in fmin
# fsim[0] = func(x0)
#ValueError: setting an array element with a sequence.
어디에서 잘못 될지 설명하는 데 도움이 되었으면 좋겠습니다.
당신은 비용 함수에 대한 배열을 리턴하고,'fmin'은 단일 값을 기대합니다. 'J [0,0]'를 반환하거나 비용 함수를 'J = 1/(2 * m) * np.sum (error ** 2)'로 다시 써라. –