ols_regressor.regressor

Module Contents

Classes

LinearRegressor

Ordinary Least Squares Linear Regressor

class ols_regressor.regressor.LinearRegressor[source]

Ordinary Least Squares Linear Regressor

LinearRegressor will fit a linear model with coefficients w = (w1, w2, …, wn) to minimize Residual Sum of Squares (RSS) between the observed targets values in the dataset, and the targets predicted by the linear approximation for the examples in the dataset.

fit(X, y, lambda_reg=0.1)[source]

Fits the linear regression model.

Parameters:
  • X (array-like matrix of shape (n_samples, n_features)) – Feature values that will be used to fit the linear regression model.

  • y (array-like matrix of shape (n_samples,)) – Target values associated with each sample in X.

  • lambda_reg (float, optional) – The regularization strength (L2 penalty). Must be a positive float. Larger values specify stronger regularization. The default value is 0.1.

Returns:

self – Returns the instance itself. The fitted linear regression model coefficients, the first element is the intercept, followed by the coefficients for each feature in the dataset.

Return type:

object

Example

X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) y = np.array([10, 11, 12]) lr = LinearRegressor() lr.fit(X, y) lr.coef

predict(X)[source]

Predicts target values using the fitted linear model.

Parameters:

X (array-like matrix of shape (n_samples, n_features)) – Feature values that will be used to make predictions.

Returns:

predictions – Predicted target values for the input feature values.

Return type:

array-like matrix of shape (n_samples, n_targets)

Example

X_new = np.array([[2, 4, 6], [8, 10, 12]]) lr.predict(X_new)

score(X, y)[source]

Calculates the coefficient of determination R^2 for the prediction.

Parameters:
  • X (array-like matrix, shape (n_samples, n_features)) – Feature dataset.

  • y (array-like matrix, shape (n_samples, )) – True target values.

Returns:

r2_score – Coefficient of determination R^2.

Return type:

float

Example

X_test = np.array([[1, 3, 5], [7, 9, 0], [2, 4, 6]]) y_test = np.array([8, 10, 1]) lr.score(X_test, y_test)