File size: 1,013 Bytes
b4f3263
 
1c1651d
b4f3263
 
 
 
 
67bae95
 
1c1651d
 
 
b4f3263
 
1c1651d
 
09418f5
 
1c1651d
b4f3263
 
0729a48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import joblib
import numpy as np
import pandas as pd
from typing import List


class RandomForestModel:
    def __init__(self):
        self.scaler = joblib.load("scalers/rf_scaler.joblib")
        self.model = joblib.load("models/random_forest.joblib")
        self.secondary_model_features = [
            "machine_probability", "backspace_count_normalized", "typing_duration_normalized", "letter_discrepancy_normalized"
        ]

    def preprocess_input(self, secondary_model_features: List[float]) -> np.ndarray:
        features_df = pd.DataFrame([secondary_model_features], columns=[
                                   self.secondary_model_features])
        features_df[self.secondary_model_features] = self.scaler.transform(
            features_df[self.secondary_model_features])
        return features_df.values.astype(np.float32).reshape(1, -1)

    def predict(self, secondary_model_features: List[float]):
        return int(self.model.predict(self.preprocess_input(secondary_model_features))[0])