Views
No views yet
1import numpy as np
2import onnxruntime as ort
3import json
4import pandas as pd
5
6# Load model and scaler
7model = ort.InferenceSession("models/xgb_model.onnx")
8with open("models/scaler_stats.json", "r") as f:
9 scaler_data = json.load(f)
10
11# Function to preprocess input data
12def preprocess(data, scaler_data):
13 # Convert to numpy array
14 data_np = np.array(data, dtype=np.float32)
15
16 # Apply scaling using the saved scaler parameters
17 scaled_data = (data_np - scaler_data["mean"]) / scaler_data["scale"]
18
19 return scaled_data.astype(np.float32)
20
21# Function to make predictions
22def predict(features, model, scaler_data):
23 # Preprocess
24 processed_features = preprocess(features, scaler_data)
25
26 # Get input name
27 input_name = model.get_inputs()[0].name
28
29 # Run inference
30 pred_log = model.run(None, {input_name: processed_features.reshape(1, -1)})[0]
31
32 # Convert log prediction back to percentage
33 pred_pct = np.expm1(pred_log) * 100.0
34
35 return pred_pct[0][0] # Return as scalar
36
37# Example usage
38features = [...] # Array of features matching the model's expected input
39prediction = predict(features, model, scaler_data)
40print(f"Predicted max price growth: {prediction:.2f}%")