值错误:形状 (2,100) 和 (2,1) 未对齐:100(暗淡 1)!= 2(暗淡 0)



我正在做机器学习作业,我正在制作逻辑回归下降梯度和逻辑回归成本。 我的函数是这样的:

def calcLogRegressionCost(X, y, theta):
#X is the feature vector
#Y is the target vector/ output vector
#theta is the weight vector 
observations = len(y)
predictions = sigmoid(np.dot(X, theta))
#Take the error when label=1
class1_cost = -y*np.log(predictions)
#Take the error when label=0
class2_cost = (1-y)*np.log(1-predictions)
#Take the sum of both costs
cost = class1_cost + class2_cost
#Take the average cost
cost = cost.sum() / observations
return cost

def logRegressionGradientDescent(X, y, theta0, alpha):
#X is the feature vector
#Y is the target vector/ output vector
#theta0 is the weight vector 
#alpha is the learning rate
#iteration is the steps you want to take 
#Start you code from here

N = len(X)
#1 - Get Predictions
predictions = sigmoid(np.dot(X, theta0))
#2 Transpose features from (100, 2) to (2, 100)
# So we can multiply w the (100,1)  cost matrix.
# Returns a (2,1) matrix holding 3 partial derivatives --
# one for each feature -- representing the aggregate
# slope of the cost function across all observations
gradient = np.dot(X.T,  predictions - y)
#3 Take the average cost derivative for each feature
gradient /= N
#4 - Multiply the gradient by our learning rate
gradient *= lr
#5 - Subtract from our weights to minimize cost
weights -= gradient
#you should return theta or loss or the both depending on your way
#of implementation
return weights

他们要求我运行梯度下降算法,使我的参数theta适合我的训练集。我做了一个训练功能,如下所示:

W1 = 0.0
W2 = 0.0
weights = np.array([
[W1],
[W2]
])

def train(features, labels, weights, lr, iters):
cost_history = []
for i in range(iters):
weights = logRegressionGradientDescent(features, labels, weights, lr)
#Calculate error for auditing purposes
cost = cost_function(features, labels, weights)
cost_history.append(cost)
# Log Progress
if i % 1000 == 0:
print ("iter: " +str(i) + " cost: "+str(cost))
return weights, cost_history

train([data['First Exam Score'], data['Second Exam Score']], data['Admitted'], weights, 0.00001, 1000)

当我用我的数据调用函数训练时,它给了我以下错误:

值错误:形状 (2,100( 和 (2,1( 未对齐:100(暗淡 1(!= 2(暗淡 0(

我不确定如何使参数适合我的数据集。数据集是一个 100 x 3 的数据帧。前 2 列是关于在第一次和第二次考试中重复获得的 100 年级学生的数据。第三列显示他们是否被他们想要的大学录取,具体取决于他们的成绩。它由 0 或 1 表示。

当我用我的数据调用函数训练时,它给了我以下内容 错误:

值错误:形状 (2,100( 和 (2,1( 未对齐:100(暗淡 1(!= 2 (暗淡 0(

作为程序员,您必须记住的一件事是,错误消息对于调试非常宝贵。它们为您提供有关逻辑或代码容易失败或已经失败的宝贵信息。 如果您阅读了错误消息,您可以注意以下事项:

  1. 由于错误提到了未对齐的形状,并且我们知道形状与向量和矩阵相关联,因此问题似乎与传递到逻辑回归函数的特征矩阵和权重矩阵的维度有关。
  2. 错误消息提到未对齐,这表明矩阵乘法
  3. 可能存在问题,因为如果矩阵的维度与乘法不兼容,或者它们的乘法顺序使操作不可行,则未对齐的矩阵预计会引发此错误。

到目前为止,您可能已经意识到该错误指向特征矩阵X和权重向量θ的 Numpy 点积。
为了修复此错误,您必须确保两件事:矩阵的形状与执行矩阵乘法兼容,并且乘法顺序正确。请记住,在逻辑回归中,特征矩阵中的每个观察都需要一个标量输出,该输出可以作为参数进一步传递到概率映射中,例如sigmoid函数,以为您提供属于给定类的特定实例的概率。

错误的解决方案

要解决此问题,请转置特征矩阵X,使其形状更改为 (100,2(。在对特征矩阵进行转置后,点积应该变得可行,从而解决您遇到的错误。

建议创建一个单独的特征矩阵,即矩阵X,该矩阵仅包含特征列,而不包含目标列,目标列是数据中的最后一列。还建议创建一个标签向量y,该向量仅存储标签或目标类列。如果我这样做,我会在 Pandas 中做所有事情,但既然你正在使用 Numpy,这里是你可以做到的。

X = np.transpose([(data['First Exam Score'], data['Second Exam Score']]) #Reshapes the feature matrix from (2,100) to (100,2)
y = data['Admitted']
train(X, y, weights, 0.00001, 1000)

正如您所注意到的,代码以这种方式变得更加可读,并且减少了遇到错误的机会。 希望这有帮助。

最新更新