# Explanation and example application of the principle of logistic regression in machine learning

2022-04-23 14:46:00

# AI learning path

### List of articles

Logical regression (Logistic Regression,LR) Also known as logistic regression analysis , It is one of the classification and prediction algorithms . Predict the probability of future results through the performance of historical data . for example , We can set the probability of purchase as a dependent variable , Attribute the user's characteristics , Such as gender , Age , Set the registration time as an independent variable . Predict the probability of purchase according to the characteristic attributes .

# Two 、 Logical regression LR

Suppose there are some data points now , We use a straight line to fit these points ( This line is called the best-fitting line ), This fitting process is called regression , As shown in the figure below ：

Logistic Regression is the classification method , It uses Sigmoid The function threshold is at [0,1] This feature .Logistic The main idea of regression classification is ： The regression formula of classification boundary line is established according to the existing data , This is used for classification . Actually ,Logistic It is essentially a discriminant model based on conditional probability (Discriminative Model).

## Sigmoid function

The picture below , It shows us Sigmoid What a function looks like

Logical regression is essentially linear regression , It just adds a layer to the feature to result mapping Sigmod Function mapping , That is, sum the characteristic lines first , then Use Sigmoid Function will be the most hypothetical function to solve the probability , And then classify .

# 3、 ... and 、 Logistic regression characteristics

Logical regression （Logistic Regression） Mainly solve the problem of two categories , Used to indicate the possibility of something happening .

• advantage ： It is suitable for the scene that needs to get a classification probability , Simple , Fast
• shortcoming ： It can only be used to deal with binary classification problems , It's not easy to deal with multi classification problems , Easy under fitting , Generally, the accuracy is not very high
• application ： Are you sick 、 Financial fraud 、 Whether there is a false account number, etc

# Four 、 Logical regression VS Linear regression

Linear regression and logistic regression are 2 A classic algorithm . Often used for comparison , Here are some differences between the two

# Application instance – Cancer case prediction

Core code

``````from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report

#  Data splitting
X_train, X_test, y_train, y_test = train_test_split(
breast.data, breast.target)

#  Data standardization
std = StandardScaler()
X_train = std.fit_transform(X_train)
X_test = std.transform(X_test)

#  Training prediction
lg = LogisticRegression()

lg.fit(X_train, y_train)

y_predict = lg.predict(X_test)

#  View training accuracy and forecast reports
print(lg.score(X_test, y_test))
print(classification_report(
y_test, y_predict, labels=[0, 1], target_names=[" Benign ", " Malignant "]))
``````

Running results

precision It means accuracy ;recall It means the recall rate ;f1-score Indicates the comprehensive index ;support Indicates the predicted number of people . The recall rate of this model , Benign achievement 0.97, Malignant to 0.96; This example is to detect cancer , We hope to find all people with cancer , Even if he's not cancer , You can also do further inspection , Therefore, we need a model with high recall rate .

# summary

Logistic regression is an extension of linear regression analysis , It maps the regression value into probability value through logic function , It realizes the processing of classification problems .

https://yzsam.com/2022/04/202204231425225682.html