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

Python Logistic回归成本函数中的两种不同成本,python,numpy,machine-learning,logistic-regression,Python,Numpy,Machine Learning,Logistic Regression,我正在用两个特征x1和x2实现逻辑回归算法。我正在写逻辑回归中的成本函数代码 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属性确定。我的θ最初包含

我正在用两个特征x1和x2实现逻辑回归算法。我正在写逻辑回归中的成本函数代码

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.reformate(y,(-1,1))
时,我得到了这个正确的代价。 但我实际上不明白这种重塑是如何纠正我的成本的。
这里的m(训练集的数量)是100。

首先,以后不要简单地转储代码!你的帖子(代码+解释)应该尽可能具有描述性!(不要长篇大论,没有人会读)。下面是您的代码正在执行的操作!请在将来发布可读代码!否则很难阅读和回答

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)的预测向量

矩阵乘法:一个mxn矩阵和一个nxk矩阵的乘积就是一个mxk矩阵。新矩阵采用第一行和第二列的行

因此,y维度应该是(100,1)形状,而不是(100,)。巨大的差异!一个是[[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)。

首先,不要在将来简单地转储代码!你的帖子(代码+解释)应该尽可能具有描述性!(不要长篇大论,没有人会读)。下面是您的代码正在执行的操作!请在将来发布可读代码!否则很难阅读和回答

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)的预测向量

矩阵乘法:一个mxn矩阵和一个nxk矩阵的乘积就是一个mxk矩阵。新矩阵采用第一行和第二列的行

因此,y维度应该是(100,1)形状,而不是(100,)。巨大的差异!一个是[[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)。

但我要问的是,这两个答案之间为什么有区别。因为“*”只需乘以两个向量(y和np.log(1-predictions)),所以我对我的文章进行了进一步的改进,以供您理解!您的问题的具体答案是
维度不匹配这导致了误算!是的,
*
进行元素相乘,但您还应该注意正确相乘的维度向量!请仔细查看第3和第4个案例。!但我想问的是,为什么这两个答案之间存在差异。因为“*”只需乘以两个向量(y和np.log(1-predictions)),所以我对我的文章进行了进一步的改进,以供您理解!您的问题的具体答案是
维度不匹配这导致了误算!是的,
*
进行元素相乘,但您还应该注意正确相乘的维度向量!请仔细查看第3和第4个案例。!