Spaces:
Runtime error
Runtime error
File size: 2,260 Bytes
2907194 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import os
import pandas as pd
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from urllib.parse import urlparse
import mlflow
import mlflow.sklearn
import numpy as np
import joblib
from mlProject.entity.config_entity import ModelEvaluationConfig
from mlProject.utils.common import save_json
from pathlib import Path
class ModelEvaluation:
def __init__(self, config: ModelEvaluationConfig):
self.config = config
def eval_metrics(self,actual, pred):
rmse = np.sqrt(mean_squared_error(actual, pred))
mae = mean_absolute_error(actual, pred)
r2 = r2_score(actual, pred)
return rmse, mae, r2
def log_into_mlflow(self):
test_data = pd.read_csv(self.config.test_data_path)
model = joblib.load(self.config.model_path)
test_x = test_data.drop([self.config.target_column], axis=1)
test_y = test_data[[self.config.target_column]]
mlflow.set_registry_uri(self.config.mlflow_uri)
tracking_url_type_store = urlparse(mlflow.get_tracking_uri()).scheme
with mlflow.start_run():
predicted_qualities = model.predict(test_x)
(rmse, mae, r2) = self.eval_metrics(test_y, predicted_qualities)
# Saving metrics as local
scores = {"rmse": rmse, "mae": mae, "r2": r2}
save_json(path=Path(self.config.metric_file_name), data=scores)
mlflow.log_params(self.config.all_params)
mlflow.log_metric("rmse", rmse)
mlflow.log_metric("r2", r2)
mlflow.log_metric("mae", mae)
# Model registry does not work with file store
if tracking_url_type_store != "file":
# Register the model
# There are other ways to use the Model Registry, which depends on the use case,
# please refer to the doc for more information:
# https://mlflow.org/docs/latest/model-registry.html#api-workflow
mlflow.sklearn.log_model(model, "model", registered_model_name="ElasticnetModel")
else:
mlflow.sklearn.log_model(model, "model")
|