🚸 Examples
Simple
examples/simple/main.py
:
## Standard libraries
import sys
import logging
from typing import Any
## Third-party libraries
import numpy as np
from numpy.typing import NDArray
## Internal modules
from simple_model import SimpleModel
logger = logging.getLogger(__name__)
if __name__ == "__main__":
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# Pre-defined variables (for customizing and testing)
_model_dir = "./models"
_model_name = "linear_regression.v0.0.1-24"
_X_train = np.array([[1], [2], [3], [4], [5]])
_y_train = np.array([2, 4, 6, 8, 10])
_X_test = np.array([[6], [7], [8]])
_y_test = np.array([10, 14, 16])
# Create the model instance
_config = {"models_dir": _model_dir, "model_name": _model_name}
_model = SimpleModel(config=_config)
# Train or load the model
if not SimpleModel.is_model_files_exist(**_config):
_model.train(X=_X_train, y=_y_train)
else:
_model.load()
# Predict the target values
_y_pred: NDArray[Any] = _model.predict(X=_X_test)
logger.info(f"Predicted values for {_X_test.flatten()}: {_y_pred.flatten()}")
# Evaluate the model
_r2_score: float = _model.score(y_true=_y_test, y_pred=_y_pred)
logger.info(f"R^2 score: {_r2_score}")
_is_similar: bool = _model.is_similar(X=_X_test, y=_y_test)
logger.info(f"Is similar: {_is_similar}")
# Save the model
if _model.is_trained() and (not SimpleModel.is_model_files_exist(**_config)):
_model.save()
logger.info("Done!")