Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize python Logistic regression

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize python Logistic regression". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to achieve python Logistic regression.

Background introduction

Don't be confused by its name! It is a classification rather than a regression algorithm. It is used to estimate discrete values based on a given set of independent variables (binary values, such as 0pqq1reyespandnotruth _ false). Simply put, it predicts the probability of an event by fitting the data to the logit function. Therefore, it is also called logit regression. Because it predicts probability, its output value is between 0 and 1 (as expected).

Third, let's try to understand this through a simple example.

Suppose your friend gives you a problem to solve. There are only two result scenarios-either you solve it, or you don't. Now imagine that you are getting all kinds of puzzles / tests in an attempt to understand which subjects you are good at. The results of this study will be like this-if you are given a tenth-grade problem based on triangulation, you have a 70% chance of solving it. On the other hand, if it is a fifth-level historical question, the probability of getting the answer is only 30%. This is what Logistic regression provides for you.

In the field of mathematics, the logarithmic probability of the result is modeled as a linear combination of predictive variables:

Odds= p / (1murp) = probability of event occurrence / probability of not event occurrence

Ln (odds) = ln (p / (1murp))

Logit (p) = ln (p / (1murp)) = b0+b1X1+b2X2+b3X3....+bkXk

Above, p is the probability of the existence of characteristics of interest. It selects parameters that maximize the possibility of observing the sample value, rather than minimizing the sum of squares of errors (such as general regression).

Now, you might ask, why use the log function? For simplicity, we just say that this is one of the best mathematical ways to copy step functions. I can explain it in detail, but this will go beyond the purpose of this article.

Take a look at the Logistic regression case using python's scikit-learn:

Code block # use Scikit-learn 's LogisticRegression to complete the test case # In [30]: import pandas as pdfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import accuracy_score# read training data and test data set # In [31]: train_data = pd.read_csv ('train-data.csv') test_data = pd.read_csv (' test-data.csv') print (train_data.head ()) # Print training data and test data shape # In [32]: print ('Shape of training data:' Train_data.shape) print ('Shape of testing data:', test_data.shape) # In [33]: # now We need to predict the missing target variable # target variable-Survived# separates independent variable and target variable on training data train_x = train_data.drop (columns= ['Survived'], axis=1) train_y = train_data [' Survived'] # separate independent variable and target variable test_x = test_data.drop on test data (columns= ['Survived']) Axis=1) test_y = test_data ['Survived'] model = LogisticRegression (solver='liblinear') model.fit (train_x,train_y) # In [34]: # coefficient of training model print (' Coefficient of model:', model.coef_) # interception model print ('Intercept of model',model.intercept_) # In [35]: # Predictive training dataset predict_train = model.predict (train_x) # training dataset score accuracy_train = accuracy_score (train_y Predict_train) print ('accuracy_score on train dataset:', accuracy_train) # In [36]: # Forecast test dataset predict_test = model.predict (test_x) # Test dataset score accuracy_test = accuracy_score (test_y,predict_test) print ('accuracy_score on test dataset:', accuracy_test) so far I believe you have a deeper understanding of "how to achieve python Logistic regression". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report