[FEAT] Preserve Trained Model #90

Open
opened 2025-05-29 18:22:14 +00:00 by nuluh · 0 comments
nuluh commented 2025-05-29 18:22:14 +00:00 (Migrated from github.com)

Problem Statement

The current implementation of the train_and_evaluate_model function does not preserve the trained model objects after training. This limits the ability to recall or reuse trained models later in the workflow.

Proposed Solution

Modify the train_and_evaluate_model function and the loop iterating through models to store trained models in a dictionary for later use. Here is the proposed solution:

# Define models for sensor1
models_sensor1 = {
    "SVM": SVC(),
    "XGBoost": XGBClassifier()
}

results_sensor1 = []
trained_models = {}  # Dictionary to store trained models for later use

for name, model in models_sensor1.items():
    res = train_and_evaluate_model(model, name, "sensor1", x_train1, y_train, x_test1, y_test)
    results_sensor1.append(res)
    trained_models[f"{name}_sensor1"] = res["trained_model"]  # Save the trained model
    print(f"{name} on sensor1: Accuracy = {res['accuracy']:.2f}%")

# Later in your code, you can recall the models for prediction:
# Example: making predictions with the trained SVM model
# new_predictions = trained_models["SVM_sensor1"].predict(new_data)

Alternatives Considered

None at the moment but alternatives might include using external serialization tools like joblib or pickle to save and load models.

Component

Python Source Code

Priority

High (significantly improves workflow)

Implementation Ideas

  • Use Python dictionaries to store trained models with identifiable keys.
  • Ensure models are serialized if needed for long-term storage.
  • Extend the train_and_evaluate_model function to optionally include serialization logic.

Expected Benefits

  • Enables reuse of trained models across multiple workflows.
  • Enhances modularity and maintainability of the code.
  • Improves the ability to experiment and test trained models without re-training.

Additional Context

N/A

### Problem Statement The current implementation of the `train_and_evaluate_model` function does not preserve the trained model objects after training. This limits the ability to recall or reuse trained models later in the workflow. ### Proposed Solution Modify the `train_and_evaluate_model` function and the loop iterating through models to store trained models in a dictionary for later use. Here is the proposed solution: ```python # Define models for sensor1 models_sensor1 = { "SVM": SVC(), "XGBoost": XGBClassifier() } results_sensor1 = [] trained_models = {} # Dictionary to store trained models for later use for name, model in models_sensor1.items(): res = train_and_evaluate_model(model, name, "sensor1", x_train1, y_train, x_test1, y_test) results_sensor1.append(res) trained_models[f"{name}_sensor1"] = res["trained_model"] # Save the trained model print(f"{name} on sensor1: Accuracy = {res['accuracy']:.2f}%") # Later in your code, you can recall the models for prediction: # Example: making predictions with the trained SVM model # new_predictions = trained_models["SVM_sensor1"].predict(new_data) ``` ### Alternatives Considered None at the moment but alternatives might include using external serialization tools like `joblib` or `pickle` to save and load models. ### Component Python Source Code ### Priority High (significantly improves workflow) ### Implementation Ideas - Use Python dictionaries to store trained models with identifiable keys. - Ensure models are serialized if needed for long-term storage. - Extend the `train_and_evaluate_model` function to optionally include serialization logic. ### Expected Benefits - Enables reuse of trained models across multiple workflows. - Enhances modularity and maintainability of the code. - Improves the ability to experiment and test trained models without re-training. ### Additional Context N/A
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nuluh/thesis#90