Logistic回归成本函数中的两种不同成本

2024-04-26 11:50:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用两个特征x1和x2实现logistic回归算法。我正在写logistic回归中的成本函数代码。你知道吗

def computeCost(X,y,theta):
    J =((np.sum(-y*np.log(sigmoid(np.dot(X,theta)))-(1-y)*(np.log(1-sigmoid(np.dot(X,theta))))))/m)
    return J

这里我的X是训练集矩阵,y是输出。 X的形状是(100,3),y的形状是(100,),由numpy库的shape属性决定。我的θ最初包含所有的零项,形状为(3,1)。当我用这些参数计算成本时,我得到了69.314的成本。但这是不正确的。正确的成本是0.69314。实际上,当我把y向量重塑成y = numpy.reshape(y,(-1,1))时,我得到了这个正确的代价。 但我真的不明白这种重塑是如何修正我的成本的。 这里m(训练集的数目)是100。你知道吗


Tags: 函数numpy算法lognp特征dot成本
1条回答
网友
1楼 · 发布于 2024-04-26 11:50:28

首先,不要在将来简单地转储你的代码!你的文章(代码+解释)应该尽可能的描述性!(不要啰嗦,没人会看的)。下面是你的代码在做什么!请在以后张贴可读的代码!否则很难阅读和回答!你知道吗

def computeCost(X,y,theta):
    '''
     Using Mean Absolute Error

     X:(100,3)
     y: (100,1)
     theta:(3,1)
     Returns 1D matrix of predictions
     Cost = ( log(predictions) + (1-labels)*log(1-predictions) ) / len(labels)
     '''
    m = len(y)
    # calculate the prediction
    predictions = sigmoid(np.dot(X,theta))

    # error for when label is of class1
    class1_cost= -y * np.log(predictions)
    # error for when label is of class1
    class2_cost= (1-y)*np.log(1-predictions)
    # total cost
    cost = class1_cost-class2_cost
    # averaging cost
    cost =cost.sum() / m
    return cost

你应该首先了解点积在数学中是如何工作的,以及你的算法将采用什么形式的输入来给你正确的答案!不要乱丢形状!你的特征向量是形状(100,3),当乘以θ时,哪个形状(3,1)会输出形状(100,1)的预测向量。你知道吗

Matrix multiplication: The product of an M x N matrix and an N x K matrix is an M x K matrix. The new matrix takes the rows of the 1st and columns of the 2nd

所以,你的y尺寸应该是(100,1)的形状,而不是(100,1)。巨大的差异!一个是[[3],[4],[6],[7],[9],…],另一个是[3,4,6,7,9,…]。 你的尺寸应该匹配正确的输出!你知道吗

问这个问题的一个更好的方法是,如何使用我的标签的正确尺寸来计算逻辑回归中的误差/成本。!你知道吗

更多了解!你知道吗

import numpy as np

label_type1= np.random.rand(100,1)
label_type2= np.random.rand(100,)
predictions= np.random.rand(100,1)
print(label_type1.shape, label_type2.shape, predictions.shape)

# When you mutiply (100,1) with (100,1)  > (100,1)
print((label_type1 * predictions).shape)

# When you do a dot product (100,1) with (100,1)  > Error, for which you have to take a transpose which isn't relavant to the context!
# print( np.dot(label_type1,predictions).shape) # error: shapes (100,1) and (100,1) not aligned: 1 (dim 1) != 100 (dim 0)
print( np.dot(label_type1.T,predictions).shape) # 
print('*'*5)

# When you mutiply (100,) with (100,1)  > (100,100) !
print((label_type2 * predictions).shape) # 

# When you  do a dot product (100,) with (100,1)  > (1,) !
print(np.dot(label_type2, predictions).shape) 
print('*'*5)

# what you are doin
label_type1_addDim = np.reshape(label_type2,(-1,1))
print(label_type1_transpose.shape)

所以,直截了当地说,你想要达到的目标是用dim(100,1)计算成本!所以要么你先做你不做的事!或者你做第五步,在不知情的情况下给你的y增加一个维度 从(100,)到(100,1)并执行与第一种情况相同的*操作!变暗(100,1)。

相关问题 更多 >