How can I display the weights and bias from LinearRegression()?
Asked Answered
T

1

8

I'm trying to solve a linear regression problem and I'm using the LinearRegression() function from sklearn. Is it possible to display the weights and bias?

Triiodomethane answered 8/5, 2019 at 11:10 Comment(0)
G
8

Once you fit the model use coef_ attribute to retrive weights and intercept_ to get bias term.

See below example:

import numpy as np
from sklearn.linear_model import LinearRegression 

a = np.array([[5,8],[12,24],[19,11],[10,15]])

## weights
w = np.array([0.2, 0.5])

## bias  
b = 0.1  

y = np.matmul(w, a.T) + b

lr = LinearRegression()
lr.fit(a, y)

print(lr.coef_)
# array([0.2, 0.5])

print(lr.intercept_)
# 0.099

For more details refer documentation

Genia answered 8/5, 2019 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.