-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_predictor.py
More file actions
213 lines (171 loc) · 6.69 KB
/
Copy pathmodel_predictor.py
File metadata and controls
213 lines (171 loc) · 6.69 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pandas as pd
import numpy as np
import joblib
import json
import os
class ExoplanetPredictor:
"""Make predictions using the trained Random Forest model."""
def __init__(self, model_dir='./models'):
"""
Initialize the predictor by loading the trained model.
Parameters:
-----------
model_dir : str
Directory containing the trained model files
"""
self.model_dir = model_dir
self.model = None
self.label_encoder = None
self.feature_columns = None
self.load_model()
def load_model(self):
"""Load the trained model and associated artifacts."""
print("Loading trained model...")
# Load model
model_path = os.path.join(self.model_dir, 'random_forest_model.pkl')
self.model = joblib.load(model_path)
print(f"✓ Model loaded from {model_path}")
# Load label encoder
encoder_path = os.path.join(self.model_dir, 'label_encoder.pkl')
self.label_encoder = joblib.load(encoder_path)
print(f"✓ Label encoder loaded")
# Load feature columns
features_path = os.path.join(self.model_dir, 'feature_columns.json')
with open(features_path, 'r') as f:
self.feature_columns = json.load(f)
print(f"✓ Feature columns loaded ({len(self.feature_columns)} features)")
print(f"\nModel is ready! Can predict: {list(self.label_encoder.classes_)}")
def predict(self, data):
"""
Make predictions on new data.
Parameters:
-----------
data : pd.DataFrame or dict
Input data with the same features used for training
Returns:
--------
dict
Predictions with labels and probabilities
"""
# Convert dict to DataFrame if necessary
if isinstance(data, dict):
data = pd.DataFrame([data])
# Ensure all required features are present
missing_features = set(self.feature_columns) - set(data.columns)
if missing_features:
raise ValueError(f"Missing required features: {missing_features}")
# Select and order features correctly
X = data[self.feature_columns]
# Handle missing values (fill with median from training would be better)
X = X.fillna(X.median())
# Make predictions
predictions = self.model.predict(X)
probabilities = self.model.predict_proba(X)
# Decode predictions
predicted_labels = self.label_encoder.inverse_transform(predictions)
results = []
for i in range(len(data)):
result = {
'prediction': predicted_labels[i],
'probabilities': {
label: float(prob)
for label, prob in zip(self.label_encoder.classes_, probabilities[i])
},
'confidence': float(np.max(probabilities[i]))
}
results.append(result)
return results if len(results) > 1 else results[0]
def predict_from_csv(self, csv_path, output_path=None):
"""
Make predictions on data from a CSV file.
Parameters:
-----------
csv_path : str
Path to CSV file with input data
output_path : str, optional
Path to save predictions (if None, only returns results)
Returns:
--------
pd.DataFrame
DataFrame with predictions
"""
print(f"Loading data from {csv_path}...")
data = pd.read_csv(csv_path)
print(f"Loaded {len(data)} samples")
# Make predictions
print("Making predictions...")
results = self.predict(data)
# If single result, convert to list
if not isinstance(results, list):
results = [results]
# Create results DataFrame
predictions_df = pd.DataFrame([
{
'prediction': r['prediction'],
'confidence': r['confidence'],
**{f'prob_{label}': r['probabilities'][label]
for label in self.label_encoder.classes_}
}
for r in results
])
# Combine with original data
output_df = pd.concat([data, predictions_df], axis=1)
# Save if output path provided
if output_path:
output_df.to_csv(output_path, index=False)
print(f"Predictions saved to {output_path}")
return output_df
def predict_single_example():
"""Example of making a single prediction."""
print("=" * 60)
print("SINGLE PREDICTION EXAMPLE")
print("=" * 60)
predictor = ExoplanetPredictor()
# Example data point
example = {
'koi_score': 0.9,
'koi_depth': 1000.0,
'koi_model_snr': 50.0,
'koi_period': 10.5,
'koi_duration': 3.2,
'koi_prad': 2.1,
'koi_srad': 1.0,
'koi_kepmag': 14.5,
'koi_teq': 500.0
}
print("\nInput data:")
for key, value in example.items():
print(f" {key}: {value}")
result = predictor.predict(example)
print("\n" + "=" * 60)
print("PREDICTION RESULT")
print("=" * 60)
print(f"Predicted class: {result['prediction']}")
print(f"Confidence: {result['confidence']:.4f} ({result['confidence']*100:.2f}%)")
print("\nClass probabilities:")
for label, prob in result['probabilities'].items():
print(f" {label}: {prob:.4f} ({prob*100:.2f}%)")
def predict_from_file(input_csv, output_csv='./predictions/predictions.csv'):
"""Example of making predictions from a CSV file."""
print("=" * 60)
print("BATCH PREDICTION FROM CSV")
print("=" * 60)
predictor = ExoplanetPredictor()
results = predictor.predict_from_csv(input_csv, output_csv)
print("\n" + "=" * 60)
print("PREDICTION SUMMARY")
print("=" * 60)
print(f"\nTotal predictions: {len(results)}")
print(f"\nPrediction distribution:")
print(results['prediction'].value_counts())
print(f"\nAverage confidence: {results['confidence'].mean():.4f}")
return results
if __name__ == "__main__":
# Run single prediction example
predict_single_example()
print("\n" + "=" * 60)
print("\nTo make predictions on new data:")
print(" 1. For single prediction: use predict_single_example()")
print(" 2. For batch predictions: use predict_from_file('your_data.csv')")
print(" 3. Or use the ExoplanetPredictor class directly in your code")
print("=" * 60)