Logistic 回归算法原理及python代码实现(二分类模型)

使用场景

通常面临的二分类问题可能是:一封邮件是否为垃圾邮件;那么可以抽象成数字标签:

  • y=0 表示负类->垃圾邮件
  • y=1 表示正类->正常邮件
模型的提出

这要求数学模型的结果取值要在[0,1]范围内,通常线性回归 h ( θ ) h(\theta) h(θ)的取值范围是实数域,很容易取到 h ( θ ) > 1 h(\theta)>1 h(θ)>1 h ( θ ) < 0 h(\theta)<0 h(θ)<0。需要一个函数将 h ( θ ) h(\theta) h(θ)的取值范围控制在[0,1]范围内。

sigmoid函数就具有这个良好性质:
在这里插入图片描述
将线性模型 w T x w^{^{T}}x wTx代入sigmoid函数,所以新的假设函数 h ( w T x ) = 1 1 + e − w T x h (w ^{^{T}}x)=\frac{1}{1+e^{-w ^{^{T}}x}} h(wTx)=1+ewTx1的取值范围是[0,1]。

  • h ( w T x ) h (w ^{^{T}}x) h(wTx)>0.5,数据被分为1类。
  • h ( w T x ) h (w^{^{T}}x) h(wTx)<0.5,数据被分为0类。
  • h ( w T x ) h (w ^{^{T}}x) h(wTx)可以看成是分类的概率有多大。
    这里的 w T w ^{^{T}} wT是每个特征的回归系数构成的向量, w T x = w 0 x 0 + w 1 x 1 + . . . w n x n w ^{^{T}}x=w_{0}x_{0}+w_{1}x_{1}+...w_{n}x_{n} wTx=w0x0+w1x1+...wnxn其中 x x x即为输入数据,这里共有n个特征,我们的目的是找到每个特征的最佳 w w w参数。

给定数据x,令其分为1类的概率 P ( Y = 1 ∣ x ) = h ( w T x ) = 1 1 + e − w T x

  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
逻辑回归是一种用于分类问题的机器学习算法,其原理是基于线性回归的思想,通过使用逻辑函数(也称为sigmoid函数)将预测结果映射到[0,1]之间的概率值。以下是一个使用Python实现逻辑回归的示例代码: ```python import numpy as np import matplotlib.pyplot as plt # 定义sigmoid函数 def sigmoid(z): return 1 / (1 + np.exp(-z)) # 定义损失函数 def loss(h, y): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() # 定义逻辑回归模型 class LogisticRegression: def __init__(self, lr=0.01, num_iter=100000, fit_intercept=True, verbose=False): self.lr = lr self.num_iter = num_iter self.fit_intercept = fit_intercept self.verbose = verbose def add_intercept(self, X): intercept = np.ones((X.shape[0], 1)) return np.concatenate((intercept, X), axis=1) def fit(self, X, y): if self.fit_intercept: X = self.add_intercept(X) # 初始化权重参数 self.theta = np.zeros(X.shape[1]) for i in range(self.num_iter): z = np.dot(X, self.theta) h = sigmoid(z) gradient = np.dot(X.T, (h - y)) / y.size self.theta -= self.lr * gradient if(self.verbose == True and i % 10000 == 0): z = np.dot(X, self.theta) h = sigmoid(z) print(f'loss: {loss(h, y)}') def predict_prob(self, X): if self.fit_intercept: X = self.add_intercept(X) return sigmoid(np.dot(X, self.theta)) def predict(self, X, threshold=0.5): return self.predict_prob(X) >= threshold # 示例:使用逻辑回归分类数据进行训练和预测 from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split # 生成分类数据集 X, y = make_classification(n_samples=100, n_features=2, n_informative=2, n_redundant=0, random_state=42) # 将数据集划分为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 创建逻辑回归模型并进行训练 model = LogisticRegression(lr=0.1, num_iter=300000) model.fit(X_train, y_train) # 在测试集上进行预测 y_pred = model.predict(X_test) # 计算准确率 accuracy = (y_pred == y_test).mean() print(f'Accuracy: {accuracy}') ``` 这段代码首先定义了sigmoid函数和损失函数,然后实现了一个LogisticRegression类,其中包含了fit方法用于训练模型,以及predict_prob和predict方法用于预测。最后,示例代码使用sklearn库生成了一个分类数据集,将数据集划分为训练集和测试集,并使用逻辑回归模型进行训练和预测。最后计算了预测的准确率。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值