Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重复错误:形状(1,3)和(100,)未对齐:3(尺寸1)!=100(尺寸0)_Python_Python 3.x_Jupyter Notebook - Fatal编程技术网

Python 重复错误:形状(1,3)和(100,)未对齐:3(尺寸1)!=100(尺寸0)

Python 重复错误:形状(1,3)和(100,)未对齐:3(尺寸1)!=100(尺寸0),python,python-3.x,jupyter-notebook,Python,Python 3.x,Jupyter Notebook,我在执行每个代码段后都会遇到这个错误,我无法解决这个问题。代码如下: 请解决此问题 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=compu

我在执行每个代码段后都会遇到这个错误,我无法解决这个问题。代码如下: 请解决此问题

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)
成本函数

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回溯(最近一次调用上次)
在里面
---->1 res=最小化(计算物流成本(θ,X,y),θ,方法='CG',
2 jac=计算成本梯度(θ,X,y),
3选项={'disp':True})
4.
5#将计算出的θ分配给变量
计算中的逻辑成本(θ,X,y)
22     """
23米=y尺寸
--->24小时=乙状结肠(X.dot(θ))
25
26 J=-1*(1/m)*(np.log(h.T.dot(y)+np.log(1-h.T.dot(1-y))
值错误:形状(3,3)和(2,)未对齐:3(尺寸1)!=2(尺寸0)

所有这些错误都是由于numpy数组的尺寸与您尝试的操作不兼容。例如,在第一节中,您试图将形状为(3,3)的数组分配到形状为(2,3)的数组中

请注意,这些数组的索引为0,因此
X[1:,:]
仅获取该矩阵的第2行和第3行。所有错误都是由于这些形状不匹配,在其他情况下是由于点积中的形状不兼容

如果您试图向原始数据添加截获,可能这就是您要查找的内容

intercept = np.ones((3, 1))

raw_data = np.zeros((3, 3))

X = np.hstack([intercept, raw_data])

我尝试过重塑它,并尝试了一些其他的变体来获得值,但没有弄清楚。首先,这两行的意图是什么?X=np.one((3,m)).X[1:,:]=X.T#第二列包含原始输入。是的,它包含来自.csv flieX的原始输入。这里不包含任何原始输入。它只是一个3x3数组,其中包含1,正如您在这两行中的第一行中定义的那样。我在上面的回答中添加了代码,这可能是您想要的。假设您希望在数据前面有一列1s用于截取?
---------------------------------------------------------------------------
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)
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)
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)
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)
 ----> 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)
intercept = np.ones((3, 1))

raw_data = np.zeros((3, 3))

X = np.hstack([intercept, raw_data])