-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfer.py
47 lines (34 loc) · 1.18 KB
/
infer.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
import tensorflow as tf
import pandas as pd
import csv
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.losses import BinaryCrossentropy
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from sklearn.preprocessing import StandardScaler
model = tf.keras.models.load_model("./model2.hdf5", compile=False)
model.compile(loss=BinaryCrossentropy(), optimizer="adam", metrics="accuracy")
output = open("./output.csv", mode="w", newline="")
writer = csv.DictWriter(output, fieldnames=["Expected", "Predicted"])
writer.writeheader()
test_data = pd.read_csv("./card_test.csv")
test_x = test_data.drop(columns=["fraud"])
columns_to_transform = [
"distance_from_home",
"distance_from_last_transaction",
"ratio_to_median_purchase_price",
]
test_x[columns_to_transform] = StandardScaler().fit_transform(
test_x[columns_to_transform]
)
# print(test_x.head())
m = test_x.shape[0]
for i in range(m):
inp = test_x.iloc[i].values.reshape(-1, 7)
pred = model.predict(inp)
if pred[0][0] >= 0.5:
pred = 1
else:
pred = 0
writer.writerow({"Expected": test_data["fraud"].iloc[i], "Predicted": pred})