-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_.py
74 lines (55 loc) · 2.23 KB
/
main_.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
68
69
70
from __future__ import print_function
import numpy as np
from sklearn.preprocessing import normalize
from sklearn.exceptions import NotFittedError
import pickle
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="sklearn")
def load_model_and_scaler():
try:
with open(r'pickle_files/model.pkl', 'rb') as f:
model = pickle.load(f)
with open(r'pickle_files/scaler.pkl', 'rb') as f:
scaler = pickle.load(f)
return model, scaler
except FileNotFoundError:
print("File not found. Please check the file path.")
except pickle.UnpicklingError:
print("Error in unpickling the file. The file might be corrupted or not a pickle file.")
except Exception as e:
print(f"An error occurred: {e}")
return None, None
def preprocess_input(amount, features, scaler):
try:
input_data = np.array([amount] + features).reshape(1, -1)
# Suppress specific warning
with warnings.catch_warnings():
warnings.simplefilter(action='ignore', category=UserWarning)
input_data = scaler.transform(input_data)
input_data = normalize(input_data, norm="l1")
return input_data
except NotFittedError as e:
print(f"Scaler is not fitted: {e}")
return None
def predict_fraud(amount, features):
model, scaler = load_model_and_scaler()
if model is None or scaler is None:
print("Failed to load model or scaler.")
return None
input_data = preprocess_input(amount, features, scaler)
if input_data is None:
print("Failed to preprocess input.")
return None
prediction = model.predict(input_data)
return prediction
if __name__ == "__main__":
# Example usage input
amount = 123.45
features = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] # 28 features
prediction = predict_fraud(amount, features)
if prediction is not None:
print(f"The transaction is {'fraudulent' if prediction == 1 else 'not fraudulent'}.")
else:
print("Prediction could not be made.")