What are Generalized Linear Models in the StatsModels library?

Using this recipe, you will learn about Generalized Linear Models in the StatsModels library.

Recipe Objective - What are Generalized Linear Models in the StatsModels library?

The generalized linear model currently supports estimation using the one-parameter exponential family.

For more related projects -

https://www.dezyre.com/projects/data-science-projects/deep-learning-projects
https://www.dezyre.com/projects/data-science-projects/neural-network-projects

Example:

# Importing libraries
import statsmodels.api as sm

# Importing heart dataset from statsmodels in the form of pandas dataframe
data = sm.datasets.heart.load_pandas()

# Storing data
X = data.data

# Fit and summarize GLM model with 'survival' as dependent variable and 'censors and age' as independent variables
# Instantiate a poisson family model
model = sm.GLM(X['survival'], X[['censors','age']],family=sm.families.Poisson())
model = model.fit()

# Model summary
model.summary()

Output - 
Generalized Linear Model Regression Results
Dep. Variable:	survival	No. Observations:	69
Model:	GLM	Df Residuals:	67
Model Family:	Poisson	Df Model:	1
Link Function:	log	Scale:	1.0000
Method:	IRLS	Log-Likelihood:	-25773.
Date:	Sat, 13 Nov 2021	Deviance:	51084.
Time:	11:12:42	Pearson chi2:	1.26e+05
No. Iterations:	6		
Covariance Type:	nonrobust		
coef	std err	z	P>|z|	[0.025	0.975]
censors	-1.6630	0.014	-118.807	0.000	-1.690	-1.636
age	0.1369	0.000	758.278	0.000	0.137	0.137

# Prediction
model.predict([[1,54]])

Output -
array([307.6487868])

In this way, we can use Generalized Linear Models in the StatsModel library.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.