-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_inference.py
47 lines (40 loc) · 1.81 KB
/
model_inference.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
37
38
39
40
41
42
43
44
45
46
47
#####
# Patient state prediction model
# Algorithm uses pre-fitted Fedot models
# All models are in folder "fedot pipelines"
#
# Input - csv file "input.csv" with parameters of patinets
#
# Output - csv file "output.csv" with predictions
#
#####
import os
import pandas as pd
import numpy as np
from fedot.core.data.data import InputData
from fedot.core.repository.dataset_types import DataTypesEnum
from fedot.core.repository.tasks import Task, TaskTypesEnum
from fedot.core.pipelines.pipeline import Pipeline
class FedotCovidPredict:
def __init__(self, models_directory: str = "fedot_pipelines", window_length=1):
self.models = os.listdir(f"{models_directory}\\window_{window_length}")
self.pipelines = {} # here we store fitted Pipelines
self.window_length = window_length
for model in self.models:
pipeline = Pipeline()
pipeline.load(f"{models_directory}\\window_{window_length}\\{model}\\{model}.json")
self.pipelines[model] = pipeline
def predict(self, df: pd.DataFrame):
state_vector = df.groupby("case").tail(self.window_length) # Our models use only one vector
state_vector = np.array(state_vector).reshape(-1, self.window_length * 47)
input_data = InputData(idx=state_vector.index, # transform to InputData
features=state_vector,
data_type=DataTypesEnum.table,
task=Task(TaskTypesEnum.regression))
# sequentially run pipelines on all parameters
prediction_vector = {}
for model in self.models:
prediction_vector[model] = self.pipelines[model].predict(input_data).predict
# Round values of categorical features
result = pd.DataFrame(prediction_vector, index = df.index)
return result