Regression
This section contains documentation for the Regression module in (machine learning).
Settings (Regression)
- stoneforge.machine_learning.regression.settings.settings(method='linear_regression', filepath='.', **kwargs)[source]
This saves the settings or hyperparameters for a machine learning method into the machine. The idea is to be reusable for distinct data.
- Parameters:
method (str, optional) –
- Name of the machine learning method to be used. Should be one of the following:
’linear_regression’
’decision_tree_regression’
’support_vector_regression’
’random_forest_regression’
’lightgbm_regression’
filepath (str, optional) – Path to the file where settings will be saved. If not provided, it defaults to the current directory (“.”). if False or None, it will return the settings as a serialized object in the current directory (“.”).
Example
>>> settings(method="linear_regression", filepath="./lr_project")
Warning
This function is designed to save settings for specific machine learning methods. If the method is not supported,
Fit (Regression)
- stoneforge.machine_learning.regression.fit.fit(X, y, method='linear_regression', filepath='.', gs=False, settings=False, **kwargs)[source]
Fits a machine learning model to the provided data and saves the model to a file Pedregosa et al.[1], Dias et al.[2].
- Parameters:
X (np.array) – Feature data for training the model.
y (np.array) – Target data for training the model.
method (str) – The machine learning method to be used for training. Should be one of the following: - ‘linear_regression’ - ‘decision_tree_regression’ - ‘support_vector_regression’ - ‘random_forest_regression’ - ‘lightgbm_regression’
filepath (str) – The path to the file where the trained model will be saved. If not provided, the model will be returned as a serialized object.
gs (bool, optional) – If True, performs grid search for hyperparameter tuning. Defaults to False.
settings (bool, optional) – If True, uses the provided settings for the model. If False, loads settings from a file if available.
**kwargs (dict) – Additional keyword arguments to be passed to the model’s fit method.
- Returns:
If filepath is not provided, returns the serialized model as a byte string. Otherwise, saves the model to the specified file and returns None.
- Return type:
np.array or None
Examples
>>> X_fit = np.array([[1, 2], [3, 4], [5, 6]]) >>> y_fit = np.array([0, 1, 0]) >>> fit(X_fit, y_fit, method="linear_regression", filepath="./lr_project", gs=True)
Warning
If the method is not supported, a ValueError will be raised. If gs is True, the model will be trained using grid search for hyperparameter. Filenames are standardized to include the method name and a suffix “_fit_property.pkl” for consistency.
Predict (Regression)
- stoneforge.machine_learning.regression.predict.predict(x, method='linear_regression', filepath='.', fit_info=False, scalers=False, **kwargs)[source]
Applies preprocessing and runs prediction using a previously trained model Pedregosa et al.[1].
- Parameters:
x (np.array) – Input feature data.
method (str) – ML method to use for prediction.
path (str) – Directory with saved model and scalers.
fit_info (bytes or False) – Serialized model if not using path.
scalers (tuple or False) – Optional tuple (scaler, scalerp); otherwise loaded from path.
kwargs (dict) – Extra arguments passed to the model’s .predict() method.
- Returns:
Predicted labels (in original label format).
- Return type:
np.array
Examples
>>> predict(X_predict, method = "linear_regression", path = "./lr_project")