亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 model.params 輸出到 pandas 中的 csv

將 model.params 輸出到 pandas 中的 csv

鳳凰求蠱 2023-07-27 16:22:21
我正在運行基本回歸:df = pd.DataFrame(playerdata, columns = cols_of_interest)target = pd.DataFrame(playerdata, columns = ['mins'])X = df[cols_of_interest]y = target["mins"]# Fit and make the predictions by the modelmodel = sm.OLS(y, X).fit()predictions = model.predict(X)model.params 似乎有我想要的信息,我想將回歸方程(系數、r^2 和 t/p 值)輸出到 csv。我似乎不知道如何做,我確實嘗試了 .to_csv 以及我在這里找到的鏈接
查看完整描述

1 回答

?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

這是我拼湊起來的一個例子,以展示什么對我有用。(我使用了 statsmodels 中的示例,然后將摘要轉換為 df,然后將框架輸出為 csv)

import numpy as np

import pandas as pd?

import statsmodels.api as sm


nsample = 100

x = 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) + e


model = sm.OLS(y, X)

results = model.fit()

print(results.summary())

我們想要轉換為數據幀(僅供參考)的輸出是results.summary():


? ? ?OLS Regression Results? ? ? ? ? ? ? ? ? ? ? ? ? ??

==============================================================================

Dep. Variable:? ? ? ? ? ? ? ? ? ? ? y? ?R-squared:? ? ? ? ? ? ? ? ? ? ? ?1.000

Model:? ? ? ? ? ? ? ? ? ? ? ? ? ? OLS? ?Adj. R-squared:? ? ? ? ? ? ? ? ? 1.000

Method:? ? ? ? ? ? ? ? ?Least Squares? ?F-statistic:? ? ? ? ? ? ? ? ?4.101e+06

Date:? ? ? ? ? ? ? ? Wed, 09 Sep 2020? ?Prob (F-statistic):? ? ? ? ? 1.08e-239

Time:? ? ? ? ? ? ? ? ? ? ? ? 18:14:11? ?Log-Likelihood:? ? ? ? ? ? ? ? -145.54

No. Observations:? ? ? ? ? ? ? ? ?100? ?AIC:? ? ? ? ? ? ? ? ? ? ? ? ? ? ?297.1

Df Residuals:? ? ? ? ? ? ? ? ? ? ? 97? ?BIC:? ? ? ? ? ? ? ? ? ? ? ? ? ? ?304.9

Df 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.312

x1? ? ? ? ? ? ?0.3067? ? ? 0.143? ? ? 2.143? ? ? 0.035? ? ? ?0.023? ? ? ?0.591

x2? ? ? ? ? ? ?9.9795? ? ? 0.014? ? 720.523? ? ? 0.000? ? ? ?9.952? ? ? 10.007

==============================================================================

Omnibus:? ? ? ? ? ? ? ? ? ? ? ? 1.587? ?Durbin-Watson:? ? ? ? ? ? ? ? ? ?1.878

Prob(Omnibus):? ? ? ? ? ? ? ? ? 0.452? ?Jarque-Bera (JB):? ? ? ? ? ? ? ? 1.271

Skew:? ? ? ? ? ? ? ? ? ? ? ? ? ?0.055? ?Prob(JB):? ? ? ? ? ? ? ? ? ? ? ? 0.530

Kurtosis:? ? ? ? ? ? ? ? ? ? ? ?2.459? ?Cond. No.? ? ? ? ? ? ? ? ? ? ? ? ?144.

==============================================================================

以下是如何將其轉換為數據框,然后最終轉換為 csv


df1 = 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? ? ? ? ? 6

0? ? ? ? ? ? ? ?coef? ? std err? ? ? ? ? t? ?P>|t|? ? ?[0.025? ? ?0.975]

1? const? ? ? 0.6968? ? ? 0.310? ? ? 2.250? ?0.027? ? ? 0.082? ? ? 1.312

2? ? ?x1? ? ? 0.3067? ? ? 0.143? ? ? 2.143? ?0.035? ? ? 0.023? ? ? 0.591

3? ? ?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.311510

x1? ? ?0.306671? 0.143135? ? 2.142533? ?3.465348e-02? 0.022588? ?0.590754

x2? ? ?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']



查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號