In-depth analysis of the principle of Logistic Regression

Principle of Logistic Regression

Logistic regression is an algorithm based on linear regression, but its output is the probability value processed by the sigmoid function, which is used to represent the probability that the sample belongs to a certain category. The goal of logistic regression is to find a best fitting curve that minimizes the fitting error between the input features and the target variable.

The core idea of ​​logistic regression is to use the logistic function (also known as the sigmoid function) to map the output of the linear equation to the probability range of [0, 1]. The mathematical expression of the logic function is:

sigmoid(z) = 1 / (1 + e^(-z))

where , zrepresents the output of the linear equation.

The linear equation for logistic regression can be expressed as:

z = b0 + b1*x1 + b2*x2 + ... + bn*xn

where b0, b1, b2, ..., bnare the coefficients of the model and x1, x2, ..., xnare the input features.

After being processed by the logistic function, the output of the linear equation will be mapped to the range [0, 1], representing the probability that the sample belongs to a certain category. Generally, when the probability is greater than or equal to a threshold (usually 0.5), the sample is classified as a positive class; when the probability is less than the threshold, the sample is classified as a negative class.

Logistic Regression Code Example

import numpy as np
from sklearn.linear_model import LogisticRegression

# 构造输入特征和目标变量
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([0, 0, 1, 1, 1])

# 创建逻辑回归模型对象
model = LogisticRegression()

# 训练模型
model.fit(X, y)

# 输出模型参数
print("系数:", model.coef_)
print("截距:", model.intercept_)

# 预测新样本
new_samples = np.array([[6], [7]])
predictions = model.predict(new_samples)
print("预测结果:", predictions)

In the code, a simple data set is constructed first, where the Xinput features yare the corresponding target variables. Then, a logistic regression model object is created and fitthe method is used to train the model. Finally, output the parameters of the model (coefficients and intercepts), and use the trained model to predict new samples.

Guess you like

Origin blog.csdn.net/weixin_43749805/article/details/131310700