1
+ import pandas as pd
2
+ import numpy as np
3
+ from sklearn .model_selection import train_test_split
4
+ from sklearn .preprocessing import StandardScaler
5
+ from sklearn .ensemble import RandomForestClassifier
6
+ from sklearn .metrics import classification_report , confusion_matrix , roc_auc_score
7
+ import joblib # Speichern
8
+
9
+ # Lade den Datensatz
10
+ df = pd .read_csv ('creditcard.csv' )
11
+
12
+ # Ersten 5 Zeilen
13
+ print (df .head ())
14
+
15
+ X = df .drop ('Class' , axis = 1 )
16
+ y = df ['Class' ]
17
+
18
+ X_train , X_test , y_train , y_test = train_test_split (X , y , test_size = 0.2 , random_state = 42 , stratify = y )
19
+
20
+ scaler = StandardScaler ()
21
+ X_train ['Amount' ] = scaler .fit_transform (X_train [['Amount' ]])
22
+ X_test ['Amount' ] = scaler .transform (X_test [['Amount' ]])
23
+
24
+ model = RandomForestClassifier (n_estimators = 100 , random_state = 42 , class_weight = 'balanced' )
25
+
26
+ # Trainiere das Modell
27
+ model .fit (X_train , y_train )
28
+
29
+ # Vorhersagen auf den Testdaten
30
+ y_pred = model .predict (X_test )
31
+ y_pred_proba = model .predict_proba (X_test )[:, 1 ] # Wahrscheinlichkeiten für die ROC-AUC-Bewertung
32
+
33
+
34
+ # Klassifikationsbericht
35
+ print ("Classification Report:" )
36
+ print (classification_report (y_test , y_pred ))
37
+
38
+ # Confusion Matrix
39
+ print ("Confusion Matrix:" )
40
+ print (confusion_matrix (y_test , y_pred ))
41
+
42
+ # ROC-AUC-Score
43
+ print ("ROC-AUC Score:" , roc_auc_score (y_test , y_pred_proba ))
44
+
45
+ # Speichere das Modell
46
+ joblib .dump (model , 'fraud_detection_model.pkl' )
47
+
48
+ # Speichere den Scaler (falls du neue Daten skalieren möchtest)
49
+ joblib .dump (scaler , 'scaler.pkl' )
50
+
51
+ from sklearn .metrics import precision_score , recall_score , roc_auc_score
52
+
53
+ # Berechne Precision, Recall und ROC-AUC
54
+ precision = precision_score (y_test , y_pred )
55
+ recall = recall_score (y_test , y_pred )
56
+ roc_auc = roc_auc_score (y_test , y_pred_proba )
57
+
58
+ print ("Precision:" , precision )
59
+ print ("Recall:" , recall )
60
+ print ("ROC-AUC:" , roc_auc )
61
+
62
+
63
+ # Lade das Modell und den Scaler
64
+ model = joblib .load ('fraud_detection_model.pkl' )
65
+ scaler = joblib .load ('scaler.pkl' )
66
+
67
+ # Beispielhafte Transaktionsdaten (ersetze dies mit den Daten aus deinem UI)
68
+ new_data = {
69
+ 'Time' : [150000 ], # Ungewöhnliche Zeit (z. B. nachts)
70
+ 'V1' : [3.0 ], # Extrem hoher Wert
71
+ 'V2' : [- 2.5 ], # Extrem niedriger Wert
72
+ 'V3' : [2.8 ], # Extrem hoher Wert
73
+ 'V4' : [- 3.0 ], # Extrem niedriger Wert
74
+ 'V5' : [1.5 ], # Ungewöhnlicher Wert
75
+ 'V6' : [- 1.8 ], # Ungewöhnlicher Wert
76
+ 'V7' : [2.0 ], # Ungewöhnlicher Wert
77
+ 'V8' : [- 2.2 ], # Ungewöhnlicher Wert
78
+ 'V9' : [1.7 ], # Ungewöhnlicher Wert
79
+ 'V10' : [- 1.9 ], # Ungewöhnlicher Wert
80
+ 'V11' : [2.3 ], # Ungewöhnlicher Wert
81
+ 'V12' : [- 2.1 ], # Ungewöhnlicher Wert
82
+ 'V13' : [1.8 ], # Ungewöhnlicher Wert
83
+ 'V14' : [- 2.0 ], # Ungewöhnlicher Wert
84
+ 'V15' : [1.9 ], # Ungewöhnlicher Wert
85
+ 'V16' : [- 1.7 ], # Ungewöhnlicher Wert
86
+ 'V17' : [2.1 ], # Ungewöhnlicher Wert
87
+ 'V18' : [- 1.6 ], # Ungewöhnlicher Wert
88
+ 'V19' : [1.5 ], # Ungewöhnlicher Wert
89
+ 'V20' : [- 1.4 ], # Ungewöhnlicher Wert
90
+ 'V21' : [1.3 ], # Ungewöhnlicher Wert
91
+ 'V22' : [- 1.2 ], # Ungewöhnlicher Wert
92
+ 'V23' : [1.1 ], # Ungewöhnlicher Wert
93
+ 'V24' : [- 1.0 ], # Ungewöhnlicher Wert
94
+ 'V25' : [0.9 ], # Ungewöhnlicher Wert
95
+ 'V26' : [- 0.8 ], # Ungewöhnlicher Wert
96
+ 'V27' : [0.7 ], # Ungewöhnlicher Wert
97
+ 'V28' : [- 0.6 ], # Ungewöhnlicher Wert
98
+ 'Amount' : [5000.0 ] # Sehr hoher Betrag
99
+ }
100
+
101
+ # Konvertiere die Daten in ein DataFrame
102
+ new_df = pd .DataFrame (new_data )
103
+
104
+ # Skaliere den 'Amount'-Wert
105
+ new_df ['Amount' ] = scaler .transform (new_df [['Amount' ]])
106
+
107
+ # Mache eine Vorhersage
108
+ prediction = model .predict (new_df )
109
+ prediction_proba = model .predict_proba (new_df )[:, 1 ]
110
+
111
+ # Finde einen Betrugsfall im Testdatensatz
112
+ # fraud_sample = X_test[y_test == 1].iloc[0:1]
113
+
114
+ # Mache eine Vorhersage für diesen Fall
115
+ prediction = model .predict (new_df )
116
+ prediction_proba = model .predict_proba (new_df )[:, 1 ]
117
+
118
+ print ("Vorhersage:" , "Betrug" if prediction [0 ] == 1 else "Kein Betrug" )
119
+ print ("Wahrscheinlichkeit für Betrug:" , prediction_proba [0 ])
0 commit comments