Skip to content

Credit Card Fraud

This dataset consists of credit card transactions in the western United States. It includes information about each transaction including customer details, the merchant and category of purchase, and whether or not the transaction was a fraud.

In this project we will perform exploratory data analysis and data vizualization to better understand the data and we will create a model to predict instances of credit card fraud.

Source of dataset. The data was partially cleaned and adapted by DataCamp.

Data Dictionary

transdatetrans_timeTransaction DateTime
merchantMerchant Name
categoryCategory of Merchant
amtAmount of Transaction
cityCity of Credit Card Holder
stateState of Credit Card Holder
latLatitude Location of Purchase
longLongitude Location of Purchase
city_popCredit Card Holder's City Population
jobJob of Credit Card Holder
dobDate of Birth of Credit Card Holder
trans_numTransaction Number
merch_latLatitude Location of Merchant
merch_longLongitude Location of Merchant
is_fraudWhether Transaction is Fraud (1) or Not (0)
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('darkgrid')

ccf = pd.read_csv('credit_card_fraud.csv') 
ccf.head(5)

Clean Data

In this section we will check for missing values and duplicates and deal with them accordingly.

print(ccf.isna().sum())
print('Number of duplicates: ' + str(ccf.duplicated().sum()))

Our data has no missing values and no duplicates.

Add age and hour columns

It is possible that credit card fraud could correlate with the age of the card holder and with the hour of the day of the transaction, so we will add columns for these feature.

ccf['trans_date_trans_time'] = pd.to_datetime(ccf['trans_date_trans_time'])
ccf['dob'] = pd.to_datetime(ccf['dob'])
ccf['age_at_trans'] = ((ccf['trans_date_trans_time'] - ccf['dob']).dt.days / 365.25).astype(int)
ccf['hour_of_trans'] = ccf['trans_date_trans_time'].dt.hour

print(ccf['trans_date_trans_time'].dtype)
print(ccf['dob'].dtype)
print(ccf['age_at_trans'].head())
print(ccf['hour_of_trans'].unique())

The output shows the dates were converted to the correct type and the age and hour columns were successfully created.

Exploring the data

First we filter the data to contain only the fraudulent transactions

frauds = ccf[ccf['is_fraud'] == 1]
frauds_by_cat = frauds['category'].value_counts()

Fraud by merchant category:

Fraud may occur at different rates between merchant types. We can plot the number of instances of fraud by merchant type.

sns.barplot(frauds_by_cat)
plt.title('Number of Frauds vs. Merchant Category')
plt.xticks(rotation=90)
plt.xlabel('Merchant Category')
plt.ylabel('Number of Credit Card Frauds')
plt.show()