Scikit-Learn
33
coef_
:
array,
shape(n_features,)
or
(n_targets, n_features)
It is used to estimate the coefficients for the linear
regression problem. It would be a 2D
array of
shape (n_targets, n_features)
if multiple targets
are passed during fit. Ex. (y 2D). On the other
hand, it would be a 1D array of length (n_features)
if only one target is passed during fit.
Intercept_
: array
This is an independent term in this linear model.
Implementation Example
First, import the required packages:
import
numpy as np
from sklearn.linear_model import LinearRegression
Now, provide the values for independent variable X:
X = np.array([[1,1],[1,2],[2,2],[2,3]])
Next, the value of dependent variable y can be calculated as follows:
y = np.dot(X, np.array([1,2])) + 3
Now, create a linear regression object as follows:
regr = LinearRegression(fit_intercept=True, normalize = True, copy_X=True,
n_jobs=2).fit(X,y)
Use predict() method to predict using this linear model as follows:
regr.predict(np.array([[3,5]]))
Output
array([16.])
To get the coefficient of determination of the prediction we can use Score() method as
follows:
regr.score(X,y)
Output
1.0
We can estimate the coefficients by using attribute named ‘coef’ as follows:
regr.coef
_
Output
Scikit-Learn
34
array([1., 2.])
We can calculate the intercept i.e. the expected mean value of Y when all X = 0 by using
attribute named ‘intercept’ as follows:
In [24]: regr.intercept
_
Output
3.0000000000000018
Dostları ilə paylaş: