Classification

This section contains documentation for the Classification module (Machine learning).

Settings (Classification)

stoneforge.machine_learning.classification.settings.settings(method='gaussian_naive_bayes', 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:
    • ’gaussian_naive_bayes’

    • ’decision_tree_classifier’

    • ’support_vector_machine’

    • ’logistic_regression’

    • ’k_neighbors_classifier’

    • ’random_forest_classifier’

  • 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="gaussian_naive_bayes", filepath="./nb_project", var_smoothing=1e-5)

Warning

This function is designed to save settings for specific machine learning methods. If the method is not supported,

Fit (Classification)

stoneforge.machine_learning.classification.fit.fit(X, y, method='gaussian_naive_bayes', 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:
    • ’gaussian_naive_bayes’

    • ’decision_tree_classifier’

    • ’support_vector_machine’

    • ’logistic_regression’

    • ’k_neighbors_classifier’

    • ’random_forest_classifier’

  • 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="gaussian_naive_bayes", filepath="./nb_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 (Classification)

stoneforge.machine_learning.classification.predict.predict(x, method='gaussian_naive_bayes', 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, label_encoder); 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 = "gaussian_naive_bayes", path = "./nb_project")

References