9951 explained code solutions for 126 technologies


python-scikit-learnGet linear regression model coefficients


import numpy as np
from sklearn import datasets, linear_model

X, y = datasets.load_diabetes(return_X_y=True)

model = linear_model.LinearRegression()
model.fit(X, y)

coefs = model.coef_ctrl + c
from sklearn import

import module from scikit-learn

import numpy

import Numpy module

datasets.load_diabetes

loads sample diabetes database

linear_model.LinearRegression

initialize linear regression model

.fit(

train model with a given features and target variable dataset

coefs

list of trained coefficients


Usage example

import numpy as np
from sklearn import datasets, linear_model

X, y = datasets.load_diabetes(return_X_y=True)

model = linear_model.LinearRegression()
model.fit(X, y)

coefs = model.coef_

print(coefs)
output
Intercept: 152.13348416289597
Coefs: [ -10.0098663  -239.81564367  519.84592005  324.3846455  -792.17563855
  476.73902101  101.04326794  177.06323767  751.27369956   67.62669218]
R2: 0.5177484222203498