forked from pulical/MLOPS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
57 lines (35 loc) · 1.13 KB
/
test.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
import pandas as pd
from joblib import load
import json
import os
from sklearn.metrics import accuracy_score
# Set path for the input (model)
MODEL_DIR = os.environ["MODEL_DIR"]
model_file = 'logit_model.joblib'
model_path = os.path.join(MODEL_DIR, model_file)
# Set path for the input (test data)
PROCESSED_DATA_DIR = os.environ["PROCESSED_DATA_DIR"]
test_data_file = 'test.csv'
test_data_path = os.path.join(PROCESSED_DATA_DIR, test_data_file)
# Load model
logit_model = load(model_path)
# Load data
df = pd.read_csv(test_data_path, sep=",")
# Split data into dependent and independent variables
X_test = df.drop('income', axis=1)
y_test = df['income']
# Predict
logit_predictions = logit_model.predict(X_test)
# Compute test accuracy
test_logit = accuracy_score(y_test,logit_predictions)
# Test accuracy to JSON
test_metadata = {
'test_acc': test_logit
}
# Set output path
RESULTS_DIR = os.environ["RESULTS_DIR"]
test_results_file = 'test_metadata.json'
results_path = os.path.join(RESULTS_DIR, test_results_file)
# Serialize and save metadata
with open(results_path, 'w') as outfile:
json.dump(test_metadata, outfile)