updating imports for MlModel

This commit is contained in:
Khalim Conn-Kowlessar 2023-08-25 15:25:35 +01:00
parent 0e755626de
commit d6562bfab9
3 changed files with 24 additions and 27 deletions

1
.gitignore vendored
View file

@ -252,6 +252,7 @@ backend/.idea
open_uprn/.idea/ open_uprn/.idea/
conservation_areas/.idea/ conservation_areas/.idea/
model_data/.idea/ model_data/.idea/
model_data/simulation_system/.idea/
model_data/simulation_system/data* model_data/simulation_system/data*

View file

@ -13,15 +13,17 @@ from pathlib import Path
import pandas as pd import pandas as pd
from autogluon.tabular import TabularDataset, TabularPredictor from autogluon.tabular import TabularDataset, TabularPredictor
from sklearn.metrics import mean_absolute_percentage_error from sklearn.metrics import mean_absolute_percentage_error
from core.Logger import logger from model_data.simulation_system.core.Logger import logger
AUTOGLUON_HYPERPARAMETERS = ['problem_type', 'eval_metric', 'time_limit', 'presets', 'excluded_model_types'] AUTOGLUON_HYPERPARAMETERS = ['problem_type', 'eval_metric', 'time_limit', 'presets', 'excluded_model_types']
METRIC_FILENAME = "metrics.csv" METRIC_FILENAME = "metrics.csv"
class AutogluonModel: class AutogluonModel:
""" """
Autogluon model that implements the MLModel Protocol Autogluon model that implements the MLModel Protocol
""" """
def __init__(self, output_filepath: Path = None) -> None: def __init__(self, output_filepath: Path = None) -> None:
self.model = None self.model = None
self.output_filepath = output_filepath self.output_filepath = output_filepath
@ -40,10 +42,10 @@ class AutogluonModel:
logger.info("Using AutoGluon Model - Model saving already occured") logger.info("Using AutoGluon Model - Model saving already occured")
def train_model( def train_model(
self, self,
data: pd.DataFrame, data: pd.DataFrame,
target_column: str, target_column: str,
hyperparameters: dict = None) -> None: hyperparameters: dict = None) -> None:
""" """
For the given data and hyperparameters, a model is trained For the given data and hyperparameters, a model is trained
""" """
@ -62,13 +64,12 @@ class AutogluonModel:
path=self.output_filepath, path=self.output_filepath,
problem_type=hyperparameters['problem_type'], problem_type=hyperparameters['problem_type'],
eval_metric=hyperparameters['eval_metric'] eval_metric=hyperparameters['eval_metric']
).fit( ).fit(
AGdata, AGdata,
time_limit=hyperparameters['time_limit'], time_limit=hyperparameters['time_limit'],
presets=hyperparameters['presets'], presets=hyperparameters['presets'],
excluded_model_types=hyperparameters['excluded_model_types'] excluded_model_types=hyperparameters['excluded_model_types']
) )
def generate_predictions(self, data: pd.DataFrame) -> pd.DataFrame: def generate_predictions(self, data: pd.DataFrame) -> pd.DataFrame:
""" """
@ -84,12 +85,12 @@ class AutogluonModel:
return predictions return predictions
def model_evaluation( def model_evaluation(
self, self,
validation_data: pd.DataFrame, validation_data: pd.DataFrame,
target_column: str, target_column: str,
metrics_location: Path = None, metrics_location: Path = None,
metric_filename: str = METRIC_FILENAME metric_filename: str = METRIC_FILENAME
) -> pd.DataFrame: ) -> pd.DataFrame:
""" """
For any validation data, a set of predictions and metrics are return For any validation data, a set of predictions and metrics are return
""" """
@ -117,7 +118,7 @@ class AutogluonModel:
metrics_df = pd.DataFrame([performance]) metrics_df = pd.DataFrame([performance])
metrics_df.to_csv(metrics_location / metric_filename) metrics_df.to_csv(metrics_location / metric_filename)
markdown_filename = metric_filename.split(".")[0] + ".md" markdown_filename = metric_filename.split(".")[0] + ".md"
metrics_df.to_markdown(metrics_location/ markdown_filename) metrics_df.to_markdown(metrics_location / markdown_filename)
return metrics_df return metrics_df
@ -135,8 +136,3 @@ class AutogluonModel:
# This will return a string path of the location # This will return a string path of the location
return self.model.clone_for_deployment(deployment_path) return self.model.clone_for_deployment(deployment_path)

View file

@ -7,7 +7,7 @@ from typing import List
from model_data.simulation_system.core.Logger import logger from model_data.simulation_system.core.Logger import logger
from model_data.simulation_system.core.DataLoader import DataLoader from model_data.simulation_system.core.DataLoader import DataLoader
from model_data.simulation_system.core.FeatureProcessor import FeatureProcessor from model_data.simulation_system.core.FeatureProcessor import FeatureProcessor
from MLModel.Models import AutogluonModel from model_data.simulation_system.MLModel.Models import AutogluonModel
import pandas as pd import pandas as pd
from model_data.simulation_system.core.Settings import ( from model_data.simulation_system.core.Settings import (
MODEL_DIRECTORY, MODEL_DIRECTORY,