![](/img/trans.png)
[英]ValueError: shapes (100,1) and (2,1) not aligned: 1 (dim 1) != 2 (dim 0)
[英]ValueError: shapes (2,100) and (2,1) not aligned: 100 (dim 1) != 2 (dim 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 适合我的训练集。 我做了一列火车 function 如下:
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)
当我用我的数据调用 function 火车时,它给了我以下错误:
ValueError:形状(2,100)和(2,1)未对齐:100(dim 1)!= 2(dim 0)
我不确定如何使参数适合我的数据集。 数据集是 100 x 3 dataframe。 前 2 列分别是 100 名学生在第一次和第二次考试中获得的成绩数据。 第三列显示他们是否被他们想要的大学录取,这取决于他们的成绩。 它由 0 或 1 表示。
当我用我的数据调用 function 火车时,它给了我以下错误:
ValueError:形状(2,100)和(2,1)未对齐:100(dim 1)!= 2(dim 0)
作为程序员,您必须记住的一件事是错误消息对于调试非常宝贵。 它们为您提供有价值的信息,说明您的逻辑或代码在哪里容易出错或已经失败。 如果您阅读错误消息,您可以注意以下事项:
到目前为止,您可能已经意识到错误指向特征矩阵X和权重向量θ的 Numpy 点积。
为了修复这个错误,你必须确保两件事:矩阵的形状与执行矩阵乘法兼容,并且乘法的顺序是正确的。 请记住,在逻辑回归中,对于特征矩阵中的每个观察,您都需要一个标量 output,它可以作为参数进一步传递到像sigmoid
function 这样的概率映射中,从而为您提供某个实例属于给定 ZCBB4A2F2ED4F8EBC40 的概率。
错误的解决方案
为了解决这个问题,将特征矩阵X转置,使其形状变为 (100,2)。 对特征矩阵进行转置后,点积应该变得可行,从而解决您遇到的错误。
建议创建一个单独的特征矩阵,矩阵X ,它只包含特征列而不包含目标列,目标列是数据中的最后一列。 还建议创建一个 label 向量y ,它只存储标签或目标 class 列。 如果我这样做,我会在 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)
如您所见,通过这种方式代码变得更具可读性,并且减少了遇到错误的机会。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.