linear_regression module#
- class melar.linear_regression.LinearRegression(initial_weights: ~numpy.ndarray | ~numpy.float64 | float = None, initial_bias: ~numpy.float64 | float = -0.18651400558339626, weights_amount: int = 1, cost_function=<function mse>, cost_function_deriv=<function mse_deriv>)[source]#
Bases:
objectLinear regression using gradient descent.
This class functions as an all-in-one object for linear regression using gradient descent. Each instance of this class is a fully functioning linear regression model. This class is designed to be used with numpy arrays, pandas DataFrames, and pandas Series.
- Parameters:
initial_weights – Initial weights of the model. If None, random weights are generated.
initial_bias – Initial bias of the model. Default is a random float between -1 and 1.
weights_amount – Amount of weights to generate. Default is 1.
cost_function – Cost function to be used. Default is Mean Squared Error.
cost_function_deriv – Derivative of the cost function. Default is the derivative of Mean Squared Error.
- adjust(x_training: ndarray, y_training: ndarray, y_predict: ndarray, learning_rate: float) None[source]#
Adjusts the weights and bias of the model using gradient descent.
This method updates the model’s weights and bias based on the training data, the predicted values, and the learning rate. It uses the derivative of the cost function to calculate the necessary adjustments.
- Parameters:
x_training (np.ndarray) – Training data.
y_training (np.ndarray) – Target values.
y_predict (np.ndarray) – Model-predicted values.
learning_rate (float) – Size of adjustment.
- bias#
Bias of the model instance. Default is a random float between -1 and 1.
- Type:
np.float64
- np_predict(x: ndarray) float64[source]#
Predict using the linear model.
This is a version of predict that only accepts numpy arrays. This is ever so slightly faster for regular use. Regular predict is also compatible with numpy.
- Parameters:
x (np.ndarray) – Input value(s) to be predicted.
- Returns:
Predicted values.
- Return type:
np.float64
- predict(x) float64[source]#
Predict using the linear model.
- Parameters:
x (np.ndarray | pd.DataFrame | pd.Series) – Input value(s) to be predicted.
- Returns:
Predicted values.
- Return type:
np.float64
- train(x, y, learning_rate: float, generations: int, do_print: bool = False) None[source]#
Trains the model.
This method iteratively adjusts the model’s weights and bias over a specified number of generations. It uses the training data and target values to minimize the cost function. Optionally, it can print the cost at each generation for easy tracking of progress.
- Parameters:
x (np.ndarray | pd.DataFrame | pd.Series) – Training data.
y (np.ndarray | pd.DataFrame | pd.Series) – Target values.
learning_rate (float) – Size of adjustment per generation.
generations (int) – Amount of times to adjust.
do_print (bool) – Whether to print the loss for every generation during training.
- weights#
Weights of the model instance. If there is only one weight, it is a float; otherwise, it is an array.
- Type:
np.float64 or np.ndarray