Logistic Regression using Statsmodels

Logistic Regression using Statsmodels

Statsmodels is a Python module that provides classes and functions for the estimation of many different statistical models. Here's how you can perform logistic regression using the statsmodels library:

1. Installation:

First, if you haven't already, you need to install statsmodels:

pip install statsmodels 

2. Logistic Regression Example:

Consider a dataset where you want to predict a binary outcome based on some predictor variables.

import statsmodels.api as sm import numpy as np import pandas as pd # Sample data data = { 'x1': [2, 4, 5, 6, 1, 3, 4, 5], 'x2': [5, 2, 4, 3, 3, 2, 1, 6], 'y': [0, 1, 0, 1, 0, 0, 1, 1] } df = pd.DataFrame(data) # Predictor variables (adding a constant for the intercept) X = df[['x1', 'x2']] X = sm.add_constant(X) # Response variable y = df['y'] # Fit the logistic regression model model = sm.Logit(y, X) result = model.fit() # Display the summary print(result.summary()) 

This will give you a summary of the logistic regression model, including the coefficients, odds ratios, p-values, and other statistical measures.

3. Making Predictions:

You can use the fitted model to make predictions:

# Making predictions predictions = result.predict(X) print(predictions) # If you want binary predictions (0 or 1) based on a threshold (e.g., 0.5) binary_predictions = (predictions > 0.5).astype(int) print(binary_predictions) 

Notes:

  • sm.Logit() fits a logistic regression model.

  • sm.add_constant() adds an intercept to the predictor variables. This is necessary because statsmodels does not add it by default, unlike some other libraries.

  • Always make sure you understand the assumptions and implications of logistic regression before interpreting the results.


More Tags

wtforms gs-conditional-formatting scala-compiler angular-material-5 formarray fscalendar plotly-python xcode9 zsh pycharm

More Programming Guides

Other Guides

More Programming Examples