Package: glimpy
Glimpyglimpy is a Python module for fitting generalized linear models. It's based on thescikit-learnAPI to facilitate use with other scikit-learn tools (pipelines, cross-validation, etc.). Models are fit using thestatsmodelspackage.Installationpip install glimpyImportant Notesglimpy makes a few important departures from the scikit-learn APIDon't Regularize by Defaultsklearn.linear_model.LogisticRegressionregularizes by default. Its
regularization paramaterCis modeled after the SVM regularization parameter
so lower values imply more regularization.Glimpy does use theCparameter to regularize, so lower values imply
more regularization.Glimpy does not regularize by default.Don't Penalize Intercept CoefficientScikit-Learn and statsmodels penalize the intercept coefficient.Glimpy does not penalize the intercept coefficientwhen fit withintercept=True. If you want the intercept coefficient to be penalized add an intercept term to your datasetXand fit withintercept=FalseGetting StartedHere is an example of a poisson GLM to help get you startedWe will simulate an experiment where we want to determine how an individual's age and weight influence the number of hospital visits they can expect to have in a given year.Start with basic imports and setup>>>importnumpyasnp>>>fromscipy.statsimportpoisson>>>fromglimpyimportGLM,Poisson>>>>>>np.random.seed(10)>>>n_samples=1000Now we will simulate some data where observed individuals have ages ranging from 30 to 70, and weights normally distributed centered around 150 lbs.>>>age=np.random.uniform(30,70,n_samples)>>>weight=np.random.normal(150,20,n_samples)Then we will have the expected number of hospital visits vary according to the following equation. We will sample from a poisson distribution with those means to get a sample of observed hospital visits>>>expected_visits=np.exp(-10+age*0.05+weight*0.08)>>>observed_visits=poisson.rvs(expected_visits)Now we can fit aGLMobject to try to recover the formula we specified above>>>X=np.vstack([age,weight]).T>>>y=observed_visits>>>pglm=GLM(fit_intercept=True,family=Poisson())>>>pglm.fit(X,y)>>>print(pglm.summary())GeneralizedLinearModelRegressionResults==============================================================================Dep.Variable:yNo.Observations:1000Model:GLMDfResiduals:997ModelFamily:PoissonDfModel:2LinkFunction:logScale:1.0000Method:IRLSLog-Likelihood:-3619.1Date:Thu,09Jan2020Deviance:967.43Time:22:31:35Pearsonchi2:961.No.Iterations:6CovarianceType:nonrobust==============================================================================coefstderrzP>|z|[0.0250.975]------------------------------------------------------------------------------const-10.01320.020-509.6010.000-10.052-9.975x10.04990.000301.1420.0000.0500.050x20.08010.000800.7200.0000.0800.080==============================================================================Scikit-Learn IntegrationThe upshot of glimpy is that you can use easily use your favorite scikit-learn tools with glimpy GLMs. For example, you can use the scikit-learncross_val_score>>>fromsklearn.model_selectionimportcross_val_score>>>print(cross_val_score(pglm,X,y,cv=4))[-263.11969239-288.58713533-205.7032204-220.68304592]The following example demonstrates how to use glimpy alongside scikit-learn to perform grid search over elastic-net hyperparameters>>>importstatsmodels.apiassm>>>fromglimpyimportGLM,Gamma>>>fromsklearn.preprocessingimportStandardScaler>>>fromsklearn.pipelineimportPipeline>>>fromsklearnimportdatasets>>>fromsklearn.model_selectionimportGridSearchCV>>>>>>diabetes=datasets.load_diabetes()>>>>>>scaler=StandardScaler()>>>gamma_glm=GLM(fit_intercept=True,family=Gamma(sm.families.links.log()),penalty='elasticnet')>>>gamma_pipeline=Pipeline([('scaler',scaler),('glm',gamma_glm)])>>>grid_search=GridSearchCV(gamma_pipeline,>>>param_grid=[{>>>'glm__C':[1e4,1e5,1e6],>>>'glm__l1_ratio':[0.1,0.5,0.9]>>>}],>>>cv=3>>>)>>>>>>grid_search.fit(diabetes['data'],diabetes['target'])>>>print(grid_search.best_params_){'glm__C':1000000.0,'glm__l1_ratio':0.1}