Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Antisource/FFD/llms.txt

Use this file to discover all available pages before exploring further.

The raw credit card transaction dataset requires several cleaning and transformation steps before it can be used to train fraud detection models. This page walks through each preprocessing stage — from loading the CSV to producing a PCA scatter plot that reveals how genuine and fraudulent transactions cluster in two-dimensional space.

Loading the Dataset

The dataset is a CSV file containing 50,492 transaction rows and 31 columns (including the Class label). Load it with pandas and take an initial look at its structure:
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

# Load the raw transaction dataset
data = pd.read_csv('Creditcard_dataset.csv')

print(data.shape)   # (50492, 31)
data.head()
The Class column is the target label: 0 for genuine transactions and 1 for fraudulent ones. The remaining 30 columns are either raw transaction features (Time, Amount) or PCA-transformed anonymised features (V1V28).

Cleaning Steps

1

Remove rows with NaN values

Drop any row that contains at least one missing value to ensure every sample is complete before training.
2

Drop the Time column

The Time column records elapsed seconds since the first transaction and carries no predictive signal once the dataset is shuffled for training. It is removed entirely.
3

Scale the Amount column

Transaction amounts vary across several orders of magnitude. Applying StandardScaler brings Amount onto the same scale as the anonymised V1V28 features, preventing large raw values from dominating distance-based and gradient-based learners.
4

Split features and labels

Separate the cleaned DataFrame into a feature matrix x (all columns except Class) and a label vector y (Class).
The complete preprocessing block:
# 1. Remove incomplete rows (in-place)
data.dropna(inplace=True)

# 2. Drop the Time column
data = data.drop(axis=1, columns='Time')

# 3. Scale the Amount column
scaler = StandardScaler()
data['Amount'] = scaler.fit_transform(data[['Amount']])

# 4. Split genuine and fraud records into separate dataframes
data_fraud = data[data.Class == 1]
data_genuine = data[data.Class == 0]

# 5. Split into features and labels
x = data.drop("Class", axis=1)
y = data.Class

PCA Visualization

After cleaning, a two-component PCA projection gives an intuitive visual check of how separable the two classes are before any model is trained.
import plotly.express as px

# Project down to 2 dimensions for visualization
pca = PCA(2)
transformed_data = pca.fit_transform(x)

df = pd.DataFrame(transformed_data)
df['label'] = y

px.scatter(df, x=0, y=1, color=df.label.astype(str))
The resulting scatter plot reveals that fraudulent transactions tend to occupy a distinct, compact region of the feature space, while genuine transactions form a much broader cloud. This separation confirms that the downstream classifiers have a meaningful signal to learn from.
Why drop Time and scale Amount? Time is an absolute counter that leaks chronological ordering — a model trained on early transactions and tested on later ones could learn spurious temporal patterns rather than genuine fraud signals. Dropping it makes the pipeline robust to any re-ordering of the data.Amount is the only feature left in its original monetary scale after Time is removed. Without scaling, its raw magnitude (which can reach thousands) would dominate algorithms that rely on Euclidean distances (KNN) or gradient magnitudes (neural networks in the GAN), leading to slower convergence and biased decision boundaries.

Build docs developers (and LLMs) love