Spaces:
Paused
Paused
Update inference.py
Browse files- inference.py +63 -63
inference.py
CHANGED
@@ -1,63 +1,63 @@
|
|
1 |
-
# inference.py
|
2 |
-
import joblib
|
3 |
-
import numpy as np
|
4 |
-
import os
|
5 |
-
|
6 |
-
class EagleBlendPredictor:
|
7 |
-
def __init__(self, model_dir=
|
8 |
-
"""
|
9 |
-
Load fitted scaler, PCA transformer, and trained XGBoost multioutput model.
|
10 |
-
"""
|
11 |
-
self.model_dir = model_dir
|
12 |
-
|
13 |
-
# Load scaler
|
14 |
-
scaler_path = os.path.join(model_dir, "scaler.joblib")
|
15 |
-
self.scaler = joblib.load(scaler_path)
|
16 |
-
|
17 |
-
# Load PCA
|
18 |
-
pca_path = os.path.join(model_dir, "pca.joblib")
|
19 |
-
self.pca = joblib.load(pca_path)
|
20 |
-
|
21 |
-
# Load trained model
|
22 |
-
model_path = os.path.join(model_dir, "xmodel.joblib")
|
23 |
-
self.model = joblib.load(model_path)
|
24 |
-
|
25 |
-
def predict_all(self, X_new):
|
26 |
-
"""
|
27 |
-
Make predictions on new data using the trained scaler, PCA, and model.
|
28 |
-
|
29 |
-
Parameters:
|
30 |
-
- X_new: array-like of shape (n_samples, n_features)
|
31 |
-
|
32 |
-
Returns:
|
33 |
-
- predictions: numpy array of shape (n_samples, n_outputs)
|
34 |
-
"""
|
35 |
-
# Convert input to NumPy array
|
36 |
-
X_new = np.array(X_new)
|
37 |
-
|
38 |
-
# Step 1: Scale data
|
39 |
-
X_scaled = self.scaler.transform(X_new)
|
40 |
-
|
41 |
-
# Step 2: PCA transform
|
42 |
-
X_pca = self.pca.transform(X_scaled)
|
43 |
-
|
44 |
-
# Step 3: Predict
|
45 |
-
predictions = self.model.predict(X_pca)
|
46 |
-
|
47 |
-
return predictions
|
48 |
-
|
49 |
-
|
50 |
-
# if __name__ == "__main__":
|
51 |
-
# # Example usage
|
52 |
-
# # Create the inference object
|
53 |
-
# predictor = EagleBlendPredictor(model_dir="models")
|
54 |
-
|
55 |
-
# # Example new data (must have same number of features as training data)
|
56 |
-
# sample_data = [
|
57 |
-
# [0.5, 1.2, 3.3, 4.1, 5.5], # Replace with actual feature values
|
58 |
-
# [1.5, 2.1, 0.3, 4.5, 2.5]
|
59 |
-
# ]
|
60 |
-
|
61 |
-
# # Get predictions
|
62 |
-
# preds = predictor.predict_all(sample_data)
|
63 |
-
# print("Predictions:\n", preds)
|
|
|
1 |
+
# inference.py
|
2 |
+
import joblib
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
|
6 |
+
class EagleBlendPredictor:
|
7 |
+
def __init__(self, model_dir="Models"):
|
8 |
+
"""
|
9 |
+
Load fitted scaler, PCA transformer, and trained XGBoost multioutput model.
|
10 |
+
"""
|
11 |
+
self.model_dir = model_dir
|
12 |
+
|
13 |
+
# Load scaler
|
14 |
+
scaler_path = os.path.join(model_dir, "scaler.joblib")
|
15 |
+
self.scaler = joblib.load(scaler_path)
|
16 |
+
|
17 |
+
# Load PCA
|
18 |
+
pca_path = os.path.join(model_dir, "pca.joblib")
|
19 |
+
self.pca = joblib.load(pca_path)
|
20 |
+
|
21 |
+
# Load trained model
|
22 |
+
model_path = os.path.join(model_dir, "xmodel.joblib")
|
23 |
+
self.model = joblib.load(model_path)
|
24 |
+
|
25 |
+
def predict_all(self, X_new):
|
26 |
+
"""
|
27 |
+
Make predictions on new data using the trained scaler, PCA, and model.
|
28 |
+
|
29 |
+
Parameters:
|
30 |
+
- X_new: array-like of shape (n_samples, n_features)
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
- predictions: numpy array of shape (n_samples, n_outputs)
|
34 |
+
"""
|
35 |
+
# Convert input to NumPy array
|
36 |
+
X_new = np.array(X_new)
|
37 |
+
|
38 |
+
# Step 1: Scale data
|
39 |
+
X_scaled = self.scaler.transform(X_new)
|
40 |
+
|
41 |
+
# Step 2: PCA transform
|
42 |
+
X_pca = self.pca.transform(X_scaled)
|
43 |
+
|
44 |
+
# Step 3: Predict
|
45 |
+
predictions = self.model.predict(X_pca)
|
46 |
+
|
47 |
+
return predictions
|
48 |
+
|
49 |
+
|
50 |
+
# if __name__ == "__main__":
|
51 |
+
# # Example usage
|
52 |
+
# # Create the inference object
|
53 |
+
# predictor = EagleBlendPredictor(model_dir="models")
|
54 |
+
|
55 |
+
# # Example new data (must have same number of features as training data)
|
56 |
+
# sample_data = [
|
57 |
+
# [0.5, 1.2, 3.3, 4.1, 5.5], # Replace with actual feature values
|
58 |
+
# [1.5, 2.1, 0.3, 4.5, 2.5]
|
59 |
+
# ]
|
60 |
+
|
61 |
+
# # Get predictions
|
62 |
+
# preds = predictor.predict_all(sample_data)
|
63 |
+
# print("Predictions:\n", preds)
|