A fraud detector must find rare fraudulent transactions without repeatedly blocking legitimate customers. Class imbalance means the target classes contain substantially different numbers of observations. When legitimate transactions dominate, a classifier can achieve impressive overall accuracy while missing the events it was built to detect. Credit defaults and significant market moves create similar, though not necessarily equally severe, imbalances. section 7.3 section 5.3.2 section 10.4.1
Treat the class distribution as an early modeling check:
- Count each target label and inspect its proportion before training. A target-distribution chart makes a dominant class immediately visible. section 8.2.1
- Check what the labels mean and how they were obtained. Fraud labels may come from chargebacks, human reviews, or validated alerts; reliable examples of fraud remain scarce. section 8.1.1
- Choose a response suited to the task. Options include oversampling rare cases, undersampling common ones, or assigning higher misclassification costs to rare cases. For skewed credit targets, also consider class weights, decision-threshold adjustments, and cost-based evaluation. section 8.1.1 section 5.3.5
- Evaluate errors against operational costs, not accuracy alone. Missed fraud causes loss; incorrectly blocked legitimate payments create customer friction. In credit scoring, compare default detection and acceptance rates when assessing a challenger model. section 7.3 section 5.3.5
A high-accuracy fraud detector that detects nothing
A credit-card dataset contains 284,807 transactions, only 492 of them fraudulent—about 0.17%. A classifier that calls every transaction legitimate would therefore appear highly accurate while catching no fraud. Inspect class counts first, then judge detection with measures tied to flagged transactions and missed cases rather than raw accuracy. section 8.2.1 section 8.1.1
Listing 8.1 Loading and basic exploration of the credit card fraud dataset
# To save space, the related library import sections are omitted.
# Please refer to the complete code uploaded on GitHub for detailed information.
# Define the path to the dataset
DATA_PATH = "creditcard.csv" #1
try:
df = pd.read_csv(DATA_PATH)
print("Credit Card Fraud dataset loaded successfully.")
print("--------------------------------------------------")
print("First 5 rows of the dataset:")
display(df.head()) #2
print("\n--------------------------------------------------")
print(f"Dataset Shape (rows, columns): {df.shape}") #3
print("--------------------------------------------------")
print("Class distribution (0: Legitimate, 1: Fraud):")
class_counts = df['Class'].value_counts() #4
class_proportion = df['Class'].value_counts(normalize=True)
print(class_counts)
print("\nProportion of total:")
display(class_proportion.reset_index().rename(
columns={'index': 'Class_Label', 'Class': 'Proportion'}
))
print("--------------------------------------------------")
plt.figure(figsize=(7, 5)) #5
custom_palette = {0: "skyblue", 1: "salmon"}
sns.countplot(x='Class', data=df, hue='Class', palette=custom_palette, legend=False)
plt.title('Class Distribution: Legitimate (0) vs. Fraud (1)', fontsize=14)
plt.xlabel('Transaction Class', fontsize=12)
plt.ylabel('Number of Transactions', fontsize=12)
plt.xticks([0, 1], ['Legitimate (0)', 'Fraud (1)'])
plt.show()
except FileNotFoundError:
print(f"Error: '{DATA_PATH}' was not found. Please ensure the file is in the correct path relative to your notebook.")
df = None
The dataset-loading check prints target counts and proportions alongside the dataset shape, making the fraud-class shortage visible before modeling. section 8.2.1
Imbalance can reflect the decision you want to make
For next-day SPY direction, a ±1% return threshold labels roughly 75% of days neutral, 14% bullish, and 11% bearish. Neutral dominates partly because the target deliberately filters out smaller moves. An always-neutral prediction could look 75% accurate yet offer no trading value. Review both the threshold choice and the class distribution before interpreting performance. section 10.4.1 section 10.5.3
Imbalance does not have one universal correction. Synthetic fraud examples, removed legitimate examples, and higher error penalties change training in different ways; threshold changes alter which predictions trigger action. Choose with the business cost of each error in view. For market data, evaluation must also respect time order rather than relying on randomly shuffled folds. section 8.1.1 section 5.3.5 section 10.5.3










