Python中sm.Logit逻辑回归:提取.summary更多结果及模型统计信息
Got it, let's walk through how to get the standard error, z-statistic, p-values, and that top section of model-level statistics from your sm.Logit results in statsmodels.
First: Variable-Level Statistics (std err, z, p-value)
Once you've fit your model (let's assume you have a results object from model.fit()), you can directly access these stats via built-in attributes—no need to parse the summary text:
import statsmodels.api as sm import pandas as pd # Example model fit (adjust with your X/y data) model = sm.Logit(y, sm.add_constant(X)) results = model.fit()
- Standard Error: Use
results.bse— this returns a pandas Series with variable names mapped to their standard errors, matching the order ofresults.params. - Z-Statistic: Use
results.tvalues(note: even though it's namedtvalues, Logit models use z-statistics here; statsmodels just reuses the attribute name for consistency). - P-Value: Use
results.pvalues— this gives the p-value for each variable's coefficient significance test.
To combine all these into a clean, easy-to-read DataFrame (along with your existing beta values and confidence intervals):
summary_table = pd.DataFrame({ 'Beta': results.params, 'Std Err': results.bse, 'Z': results.tvalues, 'P-Value': results.pvalues, 'CI Lower': results.conf_int()[0], 'CI Upper': results.conf_int()[1] }) print(summary_table)
Second: Model-Level Summary Statistics (the top section of .summary())
The first table in the .summary() output has overall model metrics like log-likelihood, pseudo R-squared, AIC, etc. You can access each metric individually, or pull the entire formatted table as text:
Access individual model metrics:
- Log-Likelihood:
results.llf - Null Log-Likelihood (model with only intercept):
results.llnull - Pseudo R-squared:
results.prsquared - AIC:
results.aic - BIC:
results.bic - Chi-squared statistic (for model vs. null comparison):
results.chi2 - P-value for chi-squared test:
results.chi2pvalue
Get the full top summary table as text:
If you want the exact same formatted table as .summary() outputs, use:
top_summary_table = results.summary().tables[0].as_text() print(top_summary_table)
This will print the entire first section, including headers and all model stats, just like you see in the .summary() output.
内容的提问来源于stack exchange,提问作者Joe

