ValueError:形状(4,100)和(3,1)不对齐:100(dim 1)!= 3(dim 0

时间:2019-10-04 15:00:07

标签: python numpy jupyter-notebook

我创建了用于计算逻辑回归的 cost gradient 函数的代码,由于某种原因,我无法重塑或转置数组以获取适当的值结果。我在接下来的每个方块中都会出错

import pandas as pd
data = pd.read_csv('data/assg-03-data.csv', names=['exam1', 'exam2', 'admitted'])
x = data[['exam1', 'exam2']].as_matrix()
y = data.admitted.as_matrix()
m = y.size
print(x.shape)
print(y.shape)
print(m)

data.head(5)

结果是 (100,2) (100,) 100

enter image description here

代码

def sigmoid(z):
    new_val = 1 / (1 + np.exp(-z))
    return new_val 

def h(theta,x):
    return sigmoid(np.dot(x,theta))

def compute_logistic_cost(theta, X, y):
    m = y.size
    #h = g(theta.T, np.dot (x))

    J = (1/m) * np.sum((-y * np.log(h(theta,X))) - ((1-y)*np.log(1-h(theta,x))))
    return J 

def compute_logistic_cost_gradients(theta, X, y):
    m = y.size
    h = sigmoid(X.dot(theta.reshape(-1,1)))
    grad =(1/m)*X.T.dot(h-y)
    return(grad)

X = np.ones( (3, m) )
print(X)

X[1:,:] = x.T
print(X)

代码

theta = np.zeros( (3, 1) )
print(compute_logistic_cost(theta, X, y))

theta = np.array([[1.0],
              [1.0],
              [1.0]])

print(theta)
print(compute_logistic_cost(theta, X, y))

theta = np.array([[0.1],
              [0.1],
              [0.1]])
print(theta)
print(compute_logistic_cost(theta, X, y))
#---------------------------------------------------------

theta = np.zeros( (3, 1) )
print(compute_logistic_cost_gradients(theta, X, y))

theta = np.array([[1.0],
              [1.0],
              [1.0]])
print(compute_logistic_cost_gradients(theta, X, y))

theta = np.array([[0.1],
              [0.1],
              [0.1]])
print(compute_logistic_cost_gradients(theta, X, y))

错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-f9e664174896> in <module>
      1 theta = np.zeros( (3, 1) )
----> 2 print(compute_logistic_cost(theta, X, y))
      3 
      4 
      5 theta = np.array([[1.0],

<ipython-input-9-4b23d2fb9ba9> in compute_logistic_cost(theta, x, y)
     10     #h = g(theta.T, np.dot (x))
     11 
---> 12     J = (1/m) * np.sum((-y * np.log(h(theta,X))) - ((1-y)*np.log(1-h(theta,x))))
     13     return J
     14 

<ipython-input-9-4b23d2fb9ba9> in h(theta, x)
      4 
      5 def h(theta,x):
----> 6     return sigmoid(np.dot(x,theta))
      7 
      8 def compute_logistic_cost(theta, x, y):

ValueError: shapes (4,100) and (3,1) not aligned: 100 (dim 1) != 3 (dim 0)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-f8672fbf30fe> in <module>
      1 theta = np.zeros( (3, 1) )
----> 2 print(compute_logistic_cost_gradients(theta, X, y))
      3 
      4 theta = np.array([[1.0],
      5                   [1.0],

<ipython-input-14-ba33afe8ffb3> in compute_logistic_cost_gradients(theta, X, y)
      1 def compute_logistic_cost_gradients(theta, X, y):
      2     m = y.size
----> 3     h = sigmoid(X.dot(theta.reshape(-1,1)))
      4     grad =(1/m)*X.T.dot(h-y)
      5     return(grad)

ValueError: shapes (4,100) and (3,1) not aligned: 100 (dim 1) != 3 (dim 0)

任何人都可以帮助我了解这一故障

0 个答案:

没有答案