IoTPy.modules.ML.LinearRegression package¶
Submodules¶
IoTPy.modules.ML.LinearRegression.LinearRegressionStream module¶
-
class
IoTPy.modules.ML.LinearRegression.LinearRegressionStream.
LinearRegressionStream
(draw, output, incremental=True, alpha=0.01, figsize=(1000, 500))[source]¶ Helper class for linear regression.
This class provides train and predict functions for using linear regression with Stream_Learn.
Parameters: draw : boolean
Describes whether the data is to be plotted (data must have 1 dimension).
output : boolean
Describes whether debug info is to be printed. Info includes average error and current error.
incremental : boolean, optional
Describes whether the linear regression algorithm is run incrementally or not (the default is True). If incremental, then the algorithm uses incremental calculations for matrix inversion and matrix multiplication if the data has 1 feature, or stochastic gradient descent if the data has more than 1 feature. Otherwise, the algorithm uses linear algebra.
alpha : float, optional
Learning rate for stochastic gradient descent (the default is 0.01). Ignored if incremental is False or if incremental is True and data has 1 feature.
figsize : tuple, optional
A tuple containing the width and height of the plot for the map (the default is (15, 8)).
Attributes
train (function) The train function with signature as required by Stream_Learn. predict (function) The predict function with signature as required by Stream_Learn. w (tuple) The learned weight vector. avg_error (float) The average error per window of data trained. Methods
reset
()Resets the KMeans functions and average values.
IoTPy.modules.ML.LinearRegression.linear_regression module¶
-
IoTPy.modules.ML.LinearRegression.linear_regression.
evaluate_error
(X, y, w)[source]¶ Returns the mean squared error.
- X : numpy.ndarray
- Numpy array of data.
- y : numpy.ndarray
- Numpy array of outputs. Dimensions are n * 1, where n is the number of rows in X.
- w : numpy.ndarray
- Numpy array with dimensions (m + 1) * 1, where m is the number of columns in X.
Returns: float
The mean squared error
-
IoTPy.modules.ML.LinearRegression.linear_regression.
init_plot
(figsize=(1000, 500))[source]¶ Initializes the plot.
Parameters: figsize : tuple, optional
A tuple containing the width and height of the plot (the default is (1000, 800)).
-
IoTPy.modules.ML.LinearRegression.linear_regression.
plot
(X, y, w, source, step_size, max_window_size)[source]¶ Plot X data, the actual y output, and the prediction line.
Parameters: X : numpy.ndarray
Numpy array of data with 1 column.
y : numpy.ndarray
Numpy array of outputs. Dimensions are n * 1, where n is the number of rows in X.
w : numpy.ndarray
Numpy array with dimensions 2 * 1.
source : list
List of ColumnDataSource
step_size : int
The step size
max_window_size : int
The max window size
-
IoTPy.modules.ML.LinearRegression.linear_regression.
predict
(X, w)[source]¶ Returns the prediction for one data point.
Parameters: X : numpy.ndarray
Numpy array of data
w : numpy.ndarray
Numpy array with dimensions (m + 1) * 1, where m is the number of columns in X.
Returns: float
The mean squared error
-
IoTPy.modules.ML.LinearRegression.linear_regression.
train
(X, y)[source]¶ Trains a linear regression model using linear algebra.
Parameters: X : numpy.ndarray
Numpy array of data
y : numpy.ndarray
Numpy array of outputs. Dimensions are n * 1, where n is the number of rows in X.
Returns: w : numpy.ndarray
Trained vector with dimensions (m + 1) * 1, where m is the number of columns in X.
-
IoTPy.modules.ML.LinearRegression.linear_regression.
train_sgd
(X, y, alpha, w=None)[source]¶ Trains a linear regression model using stochastic gradient descent.
Parameters: X : numpy.ndarray
Numpy array of data
y : numpy.ndarray
Numpy array of outputs. Dimensions are n * 1, where n is the number of rows in X.
alpha : float
Describes the learning rate.
w : numpy.ndarray, optional
The initial w vector (the default is zero).
Returns: w : numpy.ndarray
Trained vector with dimensions (m + 1) * 1, where m is the number of columns in X.