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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This Python 3 environment comes with many helpful analytics libraries installed
|
| 2 |
+
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
|
| 3 |
+
# For example, here's several helpful packages to load
|
| 4 |
+
|
| 5 |
+
import numpy as np # linear algebra
|
| 6 |
+
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
|
| 7 |
+
|
| 8 |
+
# Input data files are available in the read-only "../input/" directory
|
| 9 |
+
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
for dirname, _, filenames in os.walk('/kaggle/input'):
|
| 13 |
+
for filename in filenames:
|
| 14 |
+
print(os.path.join(dirname, filename))
|
| 15 |
+
|
| 16 |
+
# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
|
| 17 |
+
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
import numpy as np
|
| 27 |
+
import pandas as pd
|
| 28 |
+
import matplotlib.pyplot as plt
|
| 29 |
+
import seaborn as sns
|
| 30 |
+
|
| 31 |
+
from sklearn.model_selection import train_test_split
|
| 32 |
+
from sklearn.preprocessing import StandardScaler
|
| 33 |
+
from sklearn.metrics import roc_auc_score, confusion_matrix, classification_report, RocCurveDisplay
|
| 34 |
+
|
| 35 |
+
from sklearn.linear_model import LogisticRegression
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
df = pd.read_csv("/kaggle/input/creditcardfraud/creditcard.csv")
|
| 42 |
+
df.head()
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
df.shape
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
df.info()
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
df.columns
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
df.isnull().sum().sort_values(ascending=False)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
df["Class"].value_counts()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
sns.countplot(x="Class", data=df)
|
| 67 |
+
plt.title("Class Distribution")
|
| 68 |
+
plt.show()
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
plt.figure(figsize=(8,4))
|
| 72 |
+
sns.histplot(df["Amount"], bins=50)
|
| 73 |
+
plt.title("Transaction Amount Distribution")
|
| 74 |
+
plt.show()
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
scaler = StandardScaler()
|
| 84 |
+
|
| 85 |
+
df["Amount"] = scaler.fit_transform(df[["Amount"]])
|
| 86 |
+
df["Time"] = scaler.fit_transform(df[["Time"]])
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
X = df.drop("Class", axis=1)
|
| 93 |
+
y = df["Class"]
|
| 94 |
+
|
| 95 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
model = LogisticRegression(max_iter=1000)
|
| 105 |
+
model.fit(X_train, y_train)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
y_pred_proba = model.predict_proba(X_test)[:, 1]
|
| 115 |
+
roc_auc_score(y_test, y_pred_proba)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
RocCurveDisplay.from_predictions(y_test, y_pred_proba)
|
| 122 |
+
plt.show()
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
y_pred = model.predict(X_test)
|
| 129 |
+
|
| 130 |
+
cm = confusion_matrix(y_test, y_pred)
|
| 131 |
+
|
| 132 |
+
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
|
| 133 |
+
plt.title("Confusion Matrix")
|
| 134 |
+
plt.show()
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
print(classification_report(y_test, y_pred))
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
import joblib
|
| 156 |
+
|
| 157 |
+
joblib.dump(model, "model.pkl")
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
np.save("X_test.npy", X_test)
|
| 161 |
+
np.save("y_test.npy", y_test)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|