Reputation: 15
I ran a linear regression model and have my coefficients. How do I print my variables next to my coefficients?
df = pd.read_csv('data', sep=";")
reg = linear_model.LinearRegression()
reg.fit(df[["age", "area", "bedrooms"]],df.price)
print(reg.coef_)
Output
[ 6.55199614e-02 -1.86317709e+00 2.20902007e-02]
I want the output to be
age coef: 6.55199614e-02
area coef: -1.86317709e+00
bedroom coef: 2.20902007e-02
Upvotes: 0
Views: 785
Reputation: 411
if you just want to print your variables next to your coefficients, you can use something like that:
df = pd.read_csv('data', sep=";")
reg = linear_model.LinearRegression()
colnames = ["age", "area", "bedrooms"]
reg.fit(df[colnames],df.price)
coefs_map = dict(zip(colnames, reg.coef_))
for k,v in coefs_map.items():
print(f"{k}: {v}")
Upvotes: 0
Reputation: 585
My preferred approach is pd.Series(reg.coef_,index=df.columns)
, then printing comes for free. Also it is easier to work with pd.Series
for other calculations, comparisons of models via pd.concat
etc.
Upvotes: 1
Reputation: 3591
I believe that
'\n'.join(col+' coef: '+str(coef) for (col, coef) in zip(df.columns, reg.coef_))
gets you what you asked for. However, I recommend the following, as it gives output that is easier to read:
pad_length = 8+max(len(col) for col in df.columns)
output = '\n'.join((col+' coef: ').ljust(pad_length)+str(coef) for
(col, coef) in zip(df.columns, reg.coef_))
Also, all of this assumes that your column names are all already strings. If they're not, you should replace col
with str(col)
.
Upvotes: 0