Logistic regression example in Python

王林
Release: 2023-06-10 09:42:20
Original
1085 people have browsed it

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())
Copy after login

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:

##234132311310##232##1210112032411We can use the Scikit-learn library to read the data into a Pandas data frame and divide it into training Set and test set:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# 读取数据集
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)
Copy after login
We can then create an object for our model and use the training data to fit the model.
Feature1Feature2Feature3Target
2 31
341
220
##1
10
from sklearn.linear_model import LogisticRegression

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

# 训练模型
logreg.fit(X_train, y_train)
Copy after login

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))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template