LinearRegressor

regressors.LinearRegressor(self)

A class used to represent a Simple Linear Regressor.

\[ Y = \beta_0 + \beta_1 \cdot x + \epsilon \]

Attributes

Name Type Description
weights ndarray The weights of the linear regression model. Here, the weights are represented by the \(\beta\) vector which for univariate regression is a 1D vector of length two, \(\beta = [\beta_0, \beta_1]\), where \(\beta_0\) is the slope and \(\beta_1\) is the intercept.

Methods

Name Description
fit Trains the linear regression model using the given training data.
predict Makes predictions for input data.

fit

regressors.LinearRegressor.fit(X, y)

Trains the linear regression model using the given training data.

In other words, the fit method learns the weights, represented by the \(\beta\) vector. To learn the \(\beta\) vector, use:

\[ \hat{\beta} = \left( X^\top X \right)^{-1}X^\top y \]

Here, \(X\) is the so-called design matrix, which, to include a term for the intercept, has a column of ones appended to the input X matrix.

\[ X = \begin{bmatrix} 1 & x_{1} \\ 1 & x_{2} \\ \vdots & \vdots \\ 1 & x_{n} \end{bmatrix} \]

Parameters

Name Type Description Default
X ndarray The training data, which is a 2D array of shape (n_samples, 1) where each row is a sample and each column is a feature. required
y ndarray The target values, which is a 1D array of shape (n_samples, ). required

predict

regressors.LinearRegressor.predict(X)

Makes predictions for input data.

\[ \hat{y} = X \hat{\beta} \]

Parameters

Name Type Description Default
X ndarray Input data, a 2D array of shape (n_samples, 1), with which to make predictions. required

Returns

Type Description
ndarray The predicted target values as a 1D array with the same length as X.