dislib.regression.LinearRegression

class dislib.regression.linear.base.LinearRegression(arity=50)[source]

Bases: object

Simple linear regression using ordinary least squares.

model: y1 = alpha + beta*x_i + epsilon_i goal: y = alpha + beta*x

Parameters:

arity (int) – Arity of the reductions.

Variables:
  • coef (array, shape (n_features, )) – Estimated coefficients (beta) for the linear model.
  • intercept (float) – Estimated independent term (alpha) in the linear model.

Examples

>>> import numpy as np
>>> train_x = np.array([1, 2, 3, 4, 5])[:, np.newaxis]
>>> train_y = np.array([2, 1, 1, 2, 4.5])
>>> from dislib.data import load_data
>>> train_dataset = load_data(x=train_x, y=train_y, subset_size=2)
>>> from dislib.regression import LinearRegression
>>> reg = LinearRegression()
>>> reg.fit(train_dataset)
>>> # y = 0.6 * x + 0.3
>>> reg.coef_
0.6
>>> reg.intercept_
0.3
>>> test_x = np.array([3, 5])[:, np.newaxis]
>>> test_dataset = load_data(x=test_x, subset_size=2)
>>> reg.predict(test_dataset)
>>> test_dataset.labels
array([2.1, 3.3])
fit(dataset)[source]

Fit the linear model.

Parameters:dataset (Dataset) – Training dataset: x.shape (n_samples, 1), y.shape (n_samples, ).
predict(dataset)[source]

Predict using the linear model.

Parameters:dataset (Dataset) – Dataset with samples: x.shape (n_samples, 1). Predicted values are populated in the labels attribute.