如何显示 LinearRegression() 的权重和偏差?

and*_*597 6 python machine-learning linear-regression scikit-learn

我试图解决一个线性回归问题,我使用的是LinearRegression()从功能sklearn。是否可以显示权重和偏差?

Aks*_*kar 5

一旦fit模型使用coef_属性来检索权重并intercept_获得偏差项。

见下面的例子:

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
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅文档