forked from mastermindML/mastermind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction.py
67 lines (52 loc) · 1.83 KB
/
prediction.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Prediction Start
import os
import joblib
import pandas as pd
def load_model(model_dir):
"""
Load a trained machine learning model from the specified directory.
Args:
model_dir: The directory containing the trained model.
Returns:
The loaded machine learning model.
"""
model_filename = os.path.join(model_dir, 'trained_model.pkl')
clf = joblib.load(model_filename)
return clf
def predict(model, features):
"""
Make predictions using a trained machine learning model.
Args:
model: The trained machine learning model.
features: A Pandas DataFrame containing the features of new data.
Returns:
A Pandas Series containing the predicted labels.
"""
predictions = model.predict(features)
return pd.Series(predictions, name='predicted_label')
def main(input_dir, model_dir, output_dir):
"""
Main function to make predictions using a trained model and save the results.
Args:
input_dir: The directory containing the new data features.
model_dir: The directory containing the trained machine learning model.
output_dir: The directory where the prediction results will be saved.
Returns:
None
"""
# Load the trained model
model = load_model(model_dir)
# Load new data features
features = pd.read_csv(os.path.join(input_dir, 'new_data_features.csv'))
# Make predictions
predictions = predict(model, features)
# Save the prediction results
output_filename = os.path.join(output_dir, 'predictions.csv')
predictions.to_csv(output_filename, index=False)
if __name__ == "__main__":
# Example usage when running this script directly
input_dir = "data/new_data"
model_dir = "models"
output_dir = "results"
main(input_dir, model_dir, output_dir)
# Prediction End