Home >Backend Development >Python Tutorial >Logistic regression example in Python
Python is a programming language widely used in the fields of data science and machine learning. Logistic regression is a common machine learning algorithm that can make predictions in the context of classification problems. In this article, we will implement logistic regression using Python and illustrate its application using an example. ”
1. Introduction to Logistic Regression
Logistic regression is a common machine learning algorithm that is usually used to make predictions in the context of classification problems. Its basis is to use a logistic function to transform data Fit it to a linear equation, and then map the result to between [0,1] to get the probability value. When the probability value is greater than or equal to a threshold, we predict the result as a positive class, otherwise we predict it as a negative class.
2. Implementation of logistic regression
In Python, we can use library functions such as NumPy, Pandas and Scikit-learn to implement logistic regression. Here is a sample code:
import numpy as np import pandas as pd from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split # 读取数据集 data = pd.read_csv('data.csv') # 划分数据集为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(data[['feature1', 'feature2', 'feature3']], data['target'], test_size=0.3, random_state=42) # 创建逻辑回归模型对象 logreg = LogisticRegression() # 训练模型 logreg.fit(X_train, y_train) # 预测测试集 y_pred = logreg.predict(X_test) # 输出模型准确度 print('模型准确度为:', (y_pred == y_test).mean())
3. Examples of logistic regression
In this example, we consider a binary classification problem: predicting whether a person is likely to purchase a product based on three feature values. Our data set contains We have some samples with known results. Use this data set to train our model, and then predict the test set to see the accuracy of the model.
The data set has three characteristics: purchase intention, purchasing power and purchase Habits. Each feature is a continuous value. The target variable is binary, indicating whether to purchase the item. Here is an example data set:
Feature1 | Feature2 | Feature3 | Target |
---|---|---|---|
3 | 4 | 1 | |
2 | 3 | 1 | |
3 | 1 | 0 | |
2 | 3 | 1 | |
3 | 4 | 1 | |
2 | 2 | 0 | ##1 |
1 | 0 | 1 | |
2 | 0 | 3 | |
4 | 1 | ##1 | |
1 | 0 | We can use the Scikit-learn library to read the data into a Pandas data frame and divide it into training Set and test set: |
from sklearn.linear_model import LogisticRegression # 创建逻辑回归模型对象 logreg = LogisticRegression() # 训练模型 logreg.fit(X_train, y_train)
Next, we use the test data to fit the model Make predictions and calculate the accuracy of the model on the test data:
# 预测测试集 y_pred = logreg.predict(X_test) # 输出模型准确度 print('模型准确度为:', accuracy_score(y_test, y_pred))
IV. Summary
In this article, we introduced the basic concepts of logistic regression and implemented logistic regression using Python . Experimental results show that logistic regression can fit and predict binary classification problems well. In practical applications, we can use logistic regression algorithms to predict and make decisions on similar binary classification problems.
The above is the detailed content of Logistic regression example in Python. For more information, please follow other related articles on the PHP Chinese website!