def ols_coefficients_to_dataframe(coefs):
"""
Take a series containing OLS coefficients and convert it
to a data frame.
Parameters
----------
coefs : pandas Series
Series with feature names in the index and the coefficient
values as the data, obtained from a linear model trained
using `statsmodels.OLS`.
Returns
-------
df_coef : pandas DataFrame
Data frame with two columns, the first being the feature name
and the second being the coefficient value.
Note
----
The first row in the output data frame is always for the intercept
and the rest are sorted by feature name.
"""
# first create a sorted data frame for all the non-intercept features
non_intercept_columns = [c for c in coefs.index if c != 'const']
df_non_intercept = pd.DataFrame(coefs.filter(non_intercept_columns), columns=['coefficient'])
df_non_intercept.index.name = 'feature'
df_non_intercept = df_non_intercept.sort_index()
df_non_intercept.reset_index(inplace=True)
# now create a data frame that just has the intercept
df_intercept = pd.DataFrame([{'feature': 'Intercept',
'coefficient': coefs['const']}])
# append the non-intercept frame to the intercept one
df_coef = df_intercept.append(df_non_intercept, ignore_index=True)
# we always want to have the feature column first
df_coef = df_coef[['feature', 'coefficient']]
return df_coef
评论列表
文章目录