-
Notifications
You must be signed in to change notification settings - Fork 8k
/
Copy pathpredict.py
36 lines (28 loc) · 1.12 KB
/
predict.py
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
import typing as t
import numpy as np
import pandas as pd
from regression_model import __version__ as _version
from regression_model.config.core import config
from regression_model.processing.data_manager import load_pipeline
from regression_model.processing.validation import validate_inputs
pipeline_file_name = f"{config.app_config.pipeline_save_file}{_version}.pkl"
_price_pipe = load_pipeline(file_name=pipeline_file_name)
def make_prediction(
*,
input_data: t.Union[pd.DataFrame, dict],
) -> dict:
"""Make a prediction using a saved model pipeline."""
data = pd.DataFrame(input_data)
validated_data, errors = validate_inputs(input_data=data)
results = {"predictions": None, "version": _version, "errors": errors}
if not errors:
predictions = _price_pipe.predict(
X=validated_data[config.model_config.features]
)
dtype = np.result_type(np.float32, predictions)
results = {
"predictions": [np.exp(pred).astype(dtype) for pred in predictions], # type: ignore
"version": _version,
"errors": errors,
}
return results