IEEE-CIS Fraud Detection. Machine learning algorithms help to... | by Sohail Hosseini | Towards Data Science

In this work, we are going to classify whether an online transaction is fraudulent or not. It is a binary target, named: isFraud. A lot of us have been in a situation where a transaction has been canceled without our consideration. To have a better algorithm that only cancels the fraudulent transaction and doesn't just result in being embarrassed in a store. While it is often embarrassing at the moment, but this system is truly sparing consumers millions of dollars every year.
The dataset is provided by Vesta’s real-world e-commerce transactions that includes a wide range of features from the country to the recipient email domain. With this dataset, we apply the Lightgbm algorithm on a challenging large e-scale dataset. Improving the effectiveness of fraudulent transaction warnings will save lots of people the trouble of false positive. If you would like to see an implementation in PySpark, read the next article.
But how can we analyze these sorts of problems?
The first step would be to download the dataset. The training set is used here, which has two sections. One is train_transcation and the other is train_identity.
train_identity = pd.read_csv(‘../input/ieee-fraud-detection/train_identity.csv’)train_transaction = pd.read_csv(‘../input/ieee-fraud-detection/train_transaction.csv’)
I am going to merge these two DataFrames on a column named “TransactionID”. But not all transactions have corresponding identity information.
train_transaction_identity = train_transaction.merge(train_identity, on=’TransactionID’,how=’left’ )
The dataset contains several features that I only mention a few of them below.
TransactionDT: timedelta from a given reference datetime (It is not actual time stamp)
TransactionAMT: transaction payment amount in US Dollar (some of them exchanged from other currencies).
card1 — card6: payment card information, such as card type.
addr: address
Please look at the data source link if you want to have a better understanding of features.
As it is displayed below, memory usage is over 1.9+ GB. We can reduce memory usage without losing any data.
train_transaction_identity.info()
First, I have found out different types of features include Object, float, and Integer. By using .info in the Pandas library, you can find out the size of memory usage. The majority is occupied by float64. I have changed those columns from float64 to float32. As you can see memory usage is reduced by almost 50%.
Most of the transactions are not fraud so in this case, we have unbalanced data. Machine learning algorithms do not work properly with an unbalanced dataset. A snippet of code is written below shows that only 3.5% are fraudulent.
They are several methods to deal with an unbalanced dataset as oversampling and undersampling. Fortunately, Lightgbm can solve this matter for you. By default, the model thinks that the dataset is balanced. So in your case, we would need to change to the parameter as:
is_unbalance = True
By doing this, the “unbalance” mode uses the values of the target to automatically adjust weights inversely proportional to class frequencies in the input data.
If we look at each column, we can see that if we have a lot of missing values.
(np.sum(pd.isnull(train_transaction_identity)).sort_values(ascending=False)/len(train_transaction_identity))*100
The following figure shows that we have features with about 99% missing values. These columns do not add any values to our model. So we can remove them without losing much data.
From all features, I have removed columns with more than 30% missing values. The code is written below for removing these unimportant features.
Categorical data are the data that need only a limited number of possible values. Machine learning algorithms cannot work directly with categorical data and to use them, categories must be transformed into numbers first, before you can apply the learning algorithm on them.
There several techniques to apply including OneHotEncoder, Replacing values. Here I am using Pandas get_dummy function to convert categorical variables into dummy/indicator variables.
train_dummy = pd.get_dummies(train_transaction_identity, drop_first=True)
There are several methods to deal with missing values like imputation, erase them. By default, Lightgbm has an option to deal with this problem. It ignores missing values during split finding, then allocates them to whichever side the decreases the loss the most. If you do not want to use option and apply other techniques, you would need to its parameter as:
use_missing=false
Feature scaling is one of the most important steps in machine learning algorithms during the pre-processing of data before creating a machine learning model. Most of the time, your dataset will include features highly varying in magnitudes, units, and range. But for most of the machine learning, using unscaled features create a problem.
For instance, machine learning methods like linear regression, and neural networks that use gradient descent as an optimization algorithm need the dataset to be scaled. A similar problem happens when we use distance algorithms like K-means and SVM that are most affected by the range of features. The reason is that they are using distances between data points to determine their similarity.
Fortunately, we do not need to be worried about its feature scaling. Tree-based algorithms are quite insensitive to the scale of the features.
I have used 25% of the data for validation and the rest for training. There are a lot of hyperparameters involved here. So your job is like an architect to find the best values in order to maximize accuracy.
There are several techniques that are really helpful including GridSearchCV and RandomizedSearchCV.
As you can see there are a lot of parameters involved, but you want to understand what each parameter is please look at this link.
As we can see below, we have got AUC for training data almost 98% and 94% for the validation dataset. It took around 5 minutes. You can increase “Number of boosting iterations” to have better AUC for the training/validation dataset. You can consider that or change other parameters to trade off accuracy and speed.
You would be able to run Lightgbm on CPU. In order to do that, you just need to set the following parameter “device_type”:”gpu”.
All the code used in this article can be accessed from my GitHub. I look forward to hearing feedback or questions.