-
Notifications
You must be signed in to change notification settings - Fork 0
/
3D_scatter_plot.py
36 lines (28 loc) · 1.08 KB
/
3D_scatter_plot.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
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import StandardScaler
def generate_3d_scatter_plot():
# Load the CSV data
df = pd.read_csv('./data/credit_card_transactions.csv')
# Extract features and labels
X = df[['transaction_amount', 'transaction_time']].values
y = df['is_fraud'].values
# Standardize the features
scaler = StandardScaler()
X = scaler.fit_transform(X)
# Create a 3D scatter plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(X[:, 0], X[:, 1], y, c=y,
cmap='RdYlBu', edgecolor='k')
ax.set_xlabel('Transaction Amount')
ax.set_ylabel('Transaction Time')
ax.set_zlabel('Fraud')
ax.set_title('3D Scatter Plot of Transactions')
plt.colorbar(scatter, label='Fraud')
# Save plot as an image file
plt.savefig('./app/static/3d_scatter_plot.png')
plt.close()
print("3D scatter plot successfully saved as '3d_scatter_plot.png'")
generate_3d_scatter_plot()