-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine_learning_code.py
73 lines (56 loc) · 2.34 KB
/
machine_learning_code.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
71
72
73
import pickle
import json
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# Load the synthetic dataset generated earlier
# Replace 'your_dataset.csv' with the path to your dataset CSV file
# If you're using the same dataset as generated in the previous code, you can skip this step.
df = pd.read_csv('credit_card_transactions.csv')
# Encode categorical features (transaction_type and card_type)
df_encoded = pd.get_dummies(
df, columns=['transaction_type', 'card_type'], drop_first=True)
# Split the dataset into features (X) and the target variable (y)
X = df_encoded.drop('is_fraud', axis=1)
# drop transaction date
X = X.drop('transaction_date', axis=1)
# drop merchant_id
X = X.drop('merchant_id', axis=1)
# drop customer_id
X = X.drop('customer_id', axis=1)
# READ ALL LOCATION AND MAKE IT AS INTEGER MAPPING
location = X['location'].unique()
location_mapping = dict(zip(location, range(0, len(location) + 1)))
X['location_id'] = X['location'].map(location_mapping).astype(int)
# save location mapping as json file
with open('location_mapping.json', 'w') as fp:
json.dump(location_mapping, fp)
# drop location
X = X.drop('location', axis=1) # drop location
# drop card_number
X = X.drop('card_number', axis=1)
y = df_encoded['is_fraud']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
# Standardize features (scaling)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Create and train a Logistic Regression model
model = LogisticRegression(random_state=42)
model.fit(X_train, y_train)
# save model to disk
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
# Make predictions on the test data
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)
print(f"Accuracy: {accuracy}")
print(f"Confusion Matrix:\n{conf_matrix}")
print(f"Classification Report:\n{class_report}")