[英]ValueError: shapes (2,100) and (2,1) not aligned: 100 (dim 1) != 2 (dim 0)
[英]Repetitive Error: shapes (1,3) and (100,) not aligned: 3 (dim 1) != 100 (dim 0)
在我执行的每个代码部分之后,我都会收到此错误,并且无法解决此问题。 以下是代码:请需要解决此问题
成本 Function
def compute_logistic_cost(theta, X, y):
m=len(y)
J = (1/m) * np.sum((-y * np.log(h(theta,X))) - ((1-y)*np.log(1-h(theta,X))))
eps = 1e-12
h[h < eps] = eps
eps = 1.0 - 1e-12
h[h > eps] = eps
return J
# Recall that for a model with inputs, we actually use 3 parameters, theta_0, theta_1
# and theta_2. The inputs X need to have an initial column of all 1's for the theta_0
# parameter. So for our current data, X needs to be a 3xm shaped set of data, where
# the first value in each column is 1.0, and the next value in each column is our raw inputs
X = np.ones( (3, m) )
X[1:,:] = X.T # the second column contains the raw inputs
一个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-99-596c409301b0> in <module>
4 # the first value in each column is 1.0, and the next value in each column is our raw
inputs
5 X = np.ones( (3, m) )
----> 6 X[1:,:] = X.T # the second column contains the raw inputs
ValueError: could not broadcast input array from shape (3,3) into shape (2,3)
给定假设的计算阈值
theta = np.zeros( (3, 1) )
print(theta)
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))
又报错了:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-92-d07eafdd63a5> in <module>
2 print(theta)
3 print("Hello")
----> 4 print(compute_logistic_cost(theta, X, y))
5
6
<ipython-input-90-88cf8ecf55dc> in compute_logistic_cost(theta, X, y)
24 h = sigmoid(X.dot(theta))
25
---> 26 J = -1*(1/m)*(np.log(h).T.dot(y)+np.log(1-h).T.dot(1-y))
27
28 if np.isnan(J[0]):
ValueError: shapes (1,3) and (100,) not aligned: 3 (dim 1) != 100 (dim 0)
计算梯度 function:
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)
theta = np.zeros( (3, 1) )
print(compute_logistic_cost_gradients(theta, X, y))
# Expected Result >>> [ -0.1 -12.00921659 -11.26284221]
theta = np.array([[1.0],
[1.0],
[1.0]])
print(compute_logistic_cost_gradients(theta, X, y))
# Expected Result >>> [ 0.4 20.81292044 21.84815684]
theta = np.array([[0.1],
[0.1],
[0.1]])
print(compute_logistic_cost_gradients(theta, X, y))
# Expected Result >>> [ 0.39997223 20.81184964 21.84684953]
又是一个错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-108-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-107-0af7d4f48cd7> 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
5 grad =(1/m)*X.T.dot(h-y)
ValueError: shapes (100,2) and (3,1) not aligned: 2 (dim 1) != 3 (dim 0)
参数使用 SciPy 优化
from scipy.optimize import minimize
theta = np.zeros( 2 ) # initial theta parameters to start gradient descent from
res = minimize(compute_logistic_cost(theta, X, y), theta, method='CG',
jac=compute_logistic_cost_gradients(theta, X, y),
options={'disp': True})
print(res.x)
一个错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-93-bc65484d35ef> in <module>
1 from scipy.optimize import minimize
2 theta = np.zeros( 2 ) # initial theta parameters to start gradient descent from
----> 3 res = minimize(compute_logistic_cost(theta, X, y), theta, method='CG',
4 jac=compute_logistic_cost_gradients(theta, X, y),
5 options={'disp': True})
<ipython-input-90-88cf8ecf55dc> in compute_logistic_cost(theta, X, y)
22 """
23 m = y.size
---> 24 h = sigmoid(X.dot(theta))
25
26 J = -1*(1/m)*(np.log(h).T.dot(y)+np.log(1-h).T.dot(1-y))
ValueError: shapes (3,3) and (2,) not aligned: 3 (dim 1) != 2 (dim 0)
尝试 plot 分散 plot:
res = minimize(compute_logistic_cost(theta, X, y), theta, method='CG',
jac=compute_logistic_cost_gradients(theta, X, y),
options={'disp': True})
plt.plot(x[val1, 0], X[val1, 1], linestyle='', marker='^', color='r')
plt.plot(x[val2, 0], X[val2, 1], linestyle='', marker='o', color='y')
# Assigning the calculated θ to a variable
gradBFGS = res['x']
# Calculating x and y for the decision boundary
plot_x = np.array([np.min(X[:, 2])-1, np.max(X[:, 2])+1])
# From the decision boundary calculations x2 = (-1 / θ2) * (θ0 * x1 + θ0)
plot_y = (-1 / gradBFGS[2]) * (gradBFGS[1] * plot_x + gradBFGS[0])
plt.scatter(45, 85, s=30, c='r', marker='x')
# Plotting the data
plotData(X[:,1:], y, 'Exam 1 score', 'Exam 2 score', 'Admitted', 'Not Admitted')
plt.plot(plot_x, plot_y, c='b');
一个错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-94-a46335302bf6> in <module>
----> 1 res = minimize(compute_logistic_cost(theta, X, y), theta, method='CG',
2 jac=compute_logistic_cost_gradients(theta, X, y),
3 options={'disp': True})
4
5 # Assigning the calculated θ to a variable
<ipython-input-90-88cf8ecf55dc> in compute_logistic_cost(theta, X, y)
22 """
23 m = y.size
---> 24 h = sigmoid(X.dot(theta))
25
26 J = -1*(1/m)*(np.log(h).T.dot(y)+np.log(1-h).T.dot(1-y))
ValueError: shapes (3,3) and (2,) not aligned: 3 (dim 1) != 2 (dim 0)
所有这些错误都是由于您的 numpy arrays 的尺寸与您尝试的操作不兼容。 例如,在第一部分中,您尝试将形状为 (3, 3) 的数组分配给形状为 (2, 3) 的数组。
----> 6 X[1:,:] = X.T # the second column contains the raw inputs
ValueError: could not broadcast input array from shape (3,3) into shape (2,3)
请注意,这些 arrays 索引为 0,因此X[1:,:]
仅抓取该矩阵的第 2 行和第 3 行。 所有错误都是因为这些形状不匹配,在其他情况下是因为点积中的形状不兼容。
如果您尝试向原始数据添加截距,那么这可能就是您要寻找的
intercept = np.ones((3, 1))
raw_data = np.zeros((3, 3))
X = np.hstack([intercept, raw_data])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.