2017-12-25 11 views
0

기계 학습에서 코사라 과정을 수강하고 있습니다. 첫 번째 주 비용 함수를 파이썬으로 변환하려고합니다.numpy - 비용 함수를 계산할 때 모양 오류가 발생했습니다.

import numpy as np 

def computeCost(X, y, theta): 
    inner = np.power(((X.dot(theta)) - y), 2) 
    return np.sum(inner)/(2 * len(X)) 

처음에는 작동합니다. 그러나 3D 공간으로 그릴 때 더 이상 비용이 들지 않습니다.

inner = np.power(((X * (theta.T)) - y), 2) 
return np.sum(inner)/(2 * len(X)) 

나는 차원의 오류 :

은 내가 "328.0929는"

나는이 시도 가야,이 print(computeCost(X, y, [theta0_vals[0], theta1_vals[0]]))

내가 그러나 "30109.7923098"를 얻을 실행합니다. 에 ValueError :

m = len(y)     #number of training examples 
#X = np.array([np.ones(m), X])  #I did this beofre calling the function 
X = X.transpose() 

theta = theta.transpose() 
c = np.dot(X, theta)   #Matrix multiplication X*theta 
c = c.transpose() - y 
J = np.sum(c**2)/(2*m)   #Calculating cost 
return J 

는 I 오류 얻을 :

가 I이 시도 형상 (2,97) 및 (1,2)는 정렬되지 97 DIM (1) = 1

(0 희미한)!

자세한 정보가 필요하면 알려주세요.

+0

처럼? –

+0

행렬 치수가 일치하지 않습니다. 수정을 위해 변형을 사용하십시오. – gurpinars

답변

0

당신은 매트릭스 dimensions.My 솔루션을 해결하기 위해 모양 변경을 사용해야은 X와 Y의 모양입니다 무슨

theta = theta - (alpha/m) * np.dot(X.T.reshape(2, 97), np.dot(X, theta).flatten() - y).reshape(2, 1)