Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 651
andreea0597 Avatar asked May 08 '19 11:05

andreea0597


People also ask

What is weighted linear regression?

Weighted linear regression is a generalization of linear regression where the covariance matrix of errors is incorporated in the model. Hence, it can be beneficial when we are dealing with a heteroscedastic data. Here, we use the y ...

How do you express linear regression in statistics?

The linear regression model is expressed as where y is the response variable, x is the ( n +1) × 1 feature vector, w is ( n +1) × 1 vector containing the regression coefficients and e represents the observation error. Note that the first element of the vector x is 1 to represent the interception (or bias):

What is bias in OLS linear regression?

You can read more about OLS linear regression here, here, or here. A big p art of building the best models in machine learning deals with the bias-variance tradeoff. Bias refers to how correct (or incorrect) the model is. A very simple model that makes a lot of mistakes is said to have high bias.

Is there a robust errors option for the weighted linear regression tool?

Currently there is no robust errors option for the Weighted Linear Regression data analysis tool. There is a robust errors option for the Multiple Regression data analysis tool. I am still confused. Figure 3 shows that the independent variables (X) are D4:D12 while the Weights are H4:H12 and are required to be assigned to a separate input box.


1 Answers

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

like image 79
Sociopath Avatar answered Oct 16 '22 14:10

Sociopath