Scikit-Learn
20
Choose a class of model
Here, to compute a simple linear regression model, we need to import the linear regression
class as follows:
from sklearn.linear_model import LinearRegression
Choose model hyperparameters
Once we choose a class of model, we need to make some important choices which are
often represented as hyperparameters, or the parameters that must set before the model
is fit to data. Here, for this example of linear regression, we would like to fit the intercept
by using the
fit_intercept
hyperparameter as follows:
model = LinearRegression(fit_intercept=True)
model
Output
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
normalize=False)
Arranging the data
Now, as we know that our target variable
y
is in correct form i.e. a length
n_samples
array of 1-D. But, we need to reshape the feature matrix
X
to make it a matrix of size
[n_samples, n_features].
It can be done as follows:
X = x[:, np.newaxis]
X.shape
Output
(40, 1)
Model fitting
Once, we arrange the data, it is time to fit the model i.e. to apply our model to data. This
can be done with the help of
Dostları ilə paylaş: