利用梯度下降法进行多变量进行二分类

当使用梯度下降法进行多变量二分类时,我们通常会使用逻辑回归(Logistic Regression)模型,因为它可以将线性模型的输出转换为概率,从而适用于二分类问题。逻辑回归使用 Sigmoid 函数将线性模型的输出映射到 (0, 1) 区间内,表示为正类的概率。

以下是使用梯度下降法进行多变量逻辑回归的基本步骤:

1.定义模型

逻辑回归模型可以表示为:

hθ​(x)=σ(θTx)

其中,σ(z)=1+e−z1​ 是 Sigmoid 函数,θ 是参数向量,x 是特征向量。

2.定义损失函数

对于逻辑回归,我们通常使用交叉熵损失函数(Cross-Entropy Loss):

J(θ)=−m1​i=1∑m​[y(i)log(hθ​(x(i)))+(1−y(i))log(1−hθ​(x(i)))]

其中,m 是训练样本的数量,y(i) 是第 i 个样本的真实标签(0 或 1)。

3.初始化参数

随机初始化参数 θ。

4.计算梯度

为了进行梯度下降,我们需要计算损失函数关于每个参数 θj​ 的偏导数。这些偏导数组成了损失函数的梯度向量。

∂θj​∂​J(θ)=m1​i=1∑m​(hθ​(x(i))−y(i))xj(i)​

5.进行梯度下降

在每一步迭代中,使用计算出的梯度更新参数:

θj​:=θj​−αm1​i=1∑m​(hθ​(x(i))−y(i))xj(i)​

其中,α 是学习率。

6.迭代直到满足停止条件

重复步骤 4 和 5,直到损失函数的值收敛到足够小的值,或者达到预定的迭代次数。

7.使用模型进行预测

一旦得到了优化后的参数 θ,就可以使用模型 hθ​(x) 对新的样本进行预测。通常,如果 hθ​(x)>0.5,则预测为正类;否则,预测为负类。

8.代码演示

import numpy as np  
  
# Sigmoid函数  
def sigmoid(z):  
    return 1 / (1 + np.exp(-z))  
  
# 损失函数(交叉熵损失)  
def compute_cost(X, y, theta):  
    m = len(y)  
    h = sigmoid(np.dot(X, theta))  
    cost = -1/m * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))  
    return cost  
  
# 计算梯度  
def compute_gradient(X, y, theta):  
    m = len(y)  
    h = sigmoid(np.dot(X, theta))  
    gradient = 1/m * np.dot(X.T, (h - y))  
    return gradient  
  
# 梯度下降  
def gradient_descent(X, y, theta, alpha, iterations):  
    m = len(y)  
    J_history = np.zeros(iterations)  
    for iter in range(iterations):  
        gradient = compute_gradient(X, y, theta)  
        theta = theta - alpha * gradient  
        J_history[iter] = compute_cost(X, y, theta)  
    return theta, J_history  
  
# 示例数据  
# 假设我们有2个特征和一个截距项(即,X应该包括一列全为1的列)  
X = np.array([[1, 4, 2], [1, 6, 3], [1, 8, 5], [1, 10, 7], [1, 12, 9]])  # 添加一列1作为截距项  
y = np.array([0, 0, 1, 1, 1])  # 二分类标签  
  
# 添加截距项(如果X中还没有)  
if X.shape[1] == len(y.shape):  
    X = np.insert(X, 0, 1, axis=1)  
  
# 初始化参数  
theta = np.zeros(X.shape[1])  
  
# 设置梯度下降参数  
alpha = 0.1  
iterations = 1000  
  
# 执行梯度下降  
theta, J_history = gradient_descent(X, y, theta, alpha, iterations)  
  
print("训练得到的参数:", theta)  
print("损失函数历史:", J_history)  
  
# 使用模型进行预测  
def predict(X, theta):  
    h = sigmoid(np.dot(X, theta))  
    y_pred = (h >= 0.5).astype(int)  
    return y_pred  
  
y_pred = predict(X, theta)  
print("预测结果:", y_pred)

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值