简体   繁体   中英

How to separate the intercept and coefficient from params function in statsmodels

I'm trying to extract only the coefficient from statsmodels using the params function so I can put it into a data frame. When printing coef_and_intercept it gives me both the intercept and coefficient.

I tried adding params.index[1] which gives me the coef name but it does not display its value.

What could I add to params to only display the coefficient name and its value?

Here is the code.

df = pd.read_excel("dataset\Special_Proj.xlsx") 
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%y')
tickers = ['FDX', 'BRK', 'MSFT', 'NVDA', 'INTC', 'AMD', 'JPM', 'T', 'AAPL', 'AMZN', 'GS']

def rolling_reg():

    model = smf.ols('FDX ~ SP50', data=df).fit()
    coef_and_intercept = model.params
    print(coef_and_intercept)
    

rolling_reg()

Here is the output of model.params (Where I only need SP50)

Intercept    10.29
SP50          2.33
dtype: float64  

Found the answer. You have to illicit what parameter value you are looking for. Even though it does not display the name, it prints out the coef value.

Just need to add model.params['SP50'] to get the coef value.

def rolling_reg():

    model = smf.ols('FDX ~ SP50', data=df).fit()
    coef_and_intercept = model.params['SP50']
    print(coef_and_intercept)
    

rolling_reg()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM