梦里花落0921
这是我拼凑起来的一个例子,以展示什么对我有用。(我使用了 statsmodels 中的示例,然后将摘要转换为 df,然后将框架输出为 csv)import numpy as npimport pandas as pd import statsmodels.api as smnsample = 100x = np.linspace(0, 10, 100)X = np.column_stack((x, x**2))beta = np.array([1, 0.1, 10])e = np.random.normal(size=nsample)X = sm.add_constant(X)y = np.dot(X, beta) + emodel = sm.OLS(y, X)results = model.fit()print(results.summary())我们想要转换为数据帧(仅供参考)的输出是results.summary(): OLS Regression Results ==============================================================================Dep. Variable: y R-squared: 1.000Model: OLS Adj. R-squared: 1.000Method: Least Squares F-statistic: 4.101e+06Date: Wed, 09 Sep 2020 Prob (F-statistic): 1.08e-239Time: 18:14:11 Log-Likelihood: -145.54No. Observations: 100 AIC: 297.1Df Residuals: 97 BIC: 304.9Df Model: 2 Covariance Type: nonrobust ============================================================================== coef std err t P>|t| [0.025 0.975]------------------------------------------------------------------------------const 0.6968 0.310 2.250 0.027 0.082 1.312x1 0.3067 0.143 2.143 0.035 0.023 0.591x2 9.9795 0.014 720.523 0.000 9.952 10.007==============================================================================Omnibus: 1.587 Durbin-Watson: 1.878Prob(Omnibus): 0.452 Jarque-Bera (JB): 1.271Skew: 0.055 Prob(JB): 0.530Kurtosis: 2.459 Cond. No. 144.==============================================================================以下是如何将其转换为数据框,然后最终转换为 csvdf1 = pd.DataFrame(results.summary().tables[1])df2 = pd.DataFrame(results.summary2().tables[1])df1.to_csv('summary.csv')df2.to_csv('summary2.csv')df1 0 1 2 3 4 5 60 coef std err t P>|t| [0.025 0.975]1 const 0.6968 0.310 2.250 0.027 0.082 1.3122 x1 0.3067 0.143 2.143 0.035 0.023 0.5913 x2 9.9795 0.014 720.523 0.000 9.952 10.007和 df2 Coef. Std.Err. t P>|t| [0.025 0.975]const 0.696846 0.309698 2.250083 2.670308e-02 0.082181 1.311510x1 0.306671 0.143135 2.142533 3.465348e-02 0.022588 0.590754x2 9.979496 0.013850 720.523099 1.175226e-182 9.952007 10.006985存储summary1.csv 和summary2.csv。注意:如果您想添加值或制作自定义框架,您可以查看结果目录以了解可用的内容。输出dir(results)['HC0_se', 'HC1_se', 'HC2_se', 'HC3_se', '_HCCM', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_cache', '_data_attr', '_get_robustcov_results', '_is_nested', '_use_t', '_wexog_singular_values', 'aic', 'bic', 'bse', 'centered_tss', 'compare_f_test', 'compare_lm_test', 'compare_lr_test', 'condition_number', 'conf_int', 'conf_int_el', 'cov_HC0', 'cov_HC1', 'cov_HC2', 'cov_HC3', 'cov_kwds', 'cov_params', 'cov_type', 'df_model', 'df_resid', 'diagn', 'eigenvals', 'el_test', 'ess', 'f_pvalue', 'f_test', 'fittedvalues', 'fvalue', 'get_influence', 'get_prediction', 'get_robustcov_results', 'initialize', 'k_constant', 'llf', 'load', 'model', 'mse_model', 'mse_resid', 'mse_total', 'nobs', 'normalized_cov_params', 'outlier_test', 'params', 'predict', 'pvalues', 'remove_data', 'resid', 'resid_pearson', 'rsquared', 'rsquared_adj', 'save', 'scale', 'ssr', 'summary', 'summary2', 't_test', 't_test_pairwise', 'tvalues', 'uncentered_tss', 'use_t', 'wald_test', 'wald_test_terms', 'wresid']