逻辑回归的成本函数实现 Cost Function for Logistic Regression

import numpy as np
import matplotlib.pyplot as plt

# 数据
X_train = np.array([[0.5, 1.5], [1, 1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]])
y_train = np.array([0, 0, 0, 1, 1, 1])

# 自定义绘图函数
def plot_data(X, y, ax):
    pos = y == 1
    neg = y == 0
    ax.scatter(X[pos, 0], X[pos, 1], c='red', marker='x', label="y=1 (Positive)")
    ax.scatter(X[neg, 0], X[neg, 1], c='blue', marker='o', label="y=0 (Negative)")
    ax.legend()
    
#自定义颜色
dlc = {
    "dlblue": "blue",
    "dlmagenta": "magenta"
}

# 绘图
fig, ax = plt.subplots(1, 1, figsize=(4, 4))
plot_data(X_train, y_train, ax)
ax.axis([0, 4, 0, 3.5])
ax.set_ylabel('$x_1$', fontsize=12)
ax.set_xlabel('$x_0$', fontsize=12)
plt.show()

def sigmoid(z):
    return 1/(1+np.exp(-z))

def compute_cost_logistic(X, y, w, b):
    """
    Computes cost

    Args:
    X (ndarray (m,n)): Data, m examples with n features
    y (ndarray (m,)): target values
    w (ndarray (n,)): model parameters
    b (scalar)       : model parameter

    Returns:
    cost (scalar): cost
    """
    """
   循环版本
    m = X.shape[0]
    cost = 0.0
    for  i in range(m):
        z_i = np.dot(X[i],w) + b 
        f_wb_xi = sigmoid(z_i)
        cost += -y[i]*np.log(f_wb_xi)-(1-y[i])*np.log(1-f_wb_xi)

    cost = cost/m
    return cost
    """
#矢量化版本
    m = X.shape[0]
    z = np.dot(X,w)+b
    f_wb_xi = sigmoid(z)
    cost = -(1/m)*np.sum( -y*np.log(f_wb_xi)+(1-y)*np.log(1-f_wb_xi)) 
    return cost

#测试
w_tmp = np.array([1, 1])  # 权重参数初始化为 [1, 1]
b_tmp = -3                # 偏置参数初始化为 -3
print(compute_cost_logistic(X_train, y_train, w_tmp, b_tmp))  # 输出损失值


x0 = np.arange(0, 6)
x1 = 3 - x0
x1_other = 4 - x0
fig, ax = plt.subplots(1, 1, figsize=(4, 4))
ax.plot(x0, x1, c=dlc["dlblue"], label="$b$=-3")
ax.plot(x0, x1_other, c=dlc["dlmagenta"], label="$b$=-4")
ax.axis([0, 4, 0, 4])
plot_data(X_train, y_train, ax)
ax.set_ylabel('$x_1$', fontsize=12)
ax.set_xlabel('$x_0$', fontsize=12)
plt.legend(loc="upper right")
plt.title("Decision Boundary")
plt.show()
w_array1 = np.array([1,1])
b_1 = -3
w_array2 = np.array([1,1])
b_2 = -4

print("Cost for b = -3 : ", compute_cost_logistic(X_train, y_train, w_array1, b_1))
print("Cost for b = -4 : ", compute_cost_logistic(X_train, y_train, w_array2, b_2))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容