This blog teaches you how to create and select relevant features for fraud detection using scikit-learn and featuretools, two popular Python libraries for machine learning.
1. Introduction
Fraud detection is a challenging and important problem in many domains, such as banking, e-commerce, insurance, and healthcare. Fraudsters are constantly evolving their techniques to evade detection, and fraud prevention systems need to keep up with them. One of the key components of any fraud detection system is the feature engineering process, which involves creating and selecting relevant and informative features from raw data that can help identify fraudulent behavior.
In this blog, you will learn how to perform feature engineering and feature selection for fraud detection using two popular Python libraries: scikit-learn and featuretools. Scikit-learn is a machine learning library that provides various tools and algorithms for data analysis and modeling. Featuretools is an automated feature engineering library that can generate hundreds of features from complex and relational data with minimal effort.
By the end of this blog, you will be able to:
- Understand what feature engineering and feature selection are and why they are important for fraud detection.
- Perform feature engineering using scikit-learn, including data preprocessing, transformation, extraction, generation, scaling, and normalization.
- Perform feature selection using scikit-learn, including filter, wrapper, and embedded methods.
- Perform automated feature engineering using featuretools, including defining entities and relationships, generating and selecting features.
- Apply the learned techniques to a real-world fraud detection dataset and evaluate the results.
Ready to dive into the world of feature engineering and selection for fraud detection? Let’s get started!
2. What is Feature Engineering and Why is it Important for Fraud Detection?
Feature engineering is the process of creating and selecting features from raw data that can help a machine learning model learn and make predictions. Features are the attributes or variables that describe the data and influence the outcome of the model. For example, if you want to predict whether a credit card transaction is fraudulent or not, some possible features are the amount, the location, the time, the merchant, the customer, etc.
Feature engineering is important for fraud detection because it can improve the performance and accuracy of the machine learning model, as well as make it more interpretable and explainable. By creating and selecting relevant and informative features, you can help the model capture the patterns and anomalies that indicate fraudulent behavior, and reduce the noise and redundancy that might confuse the model. Feature engineering can also help you understand the factors and relationships that contribute to fraud, and provide insights for fraud prevention and mitigation.
However, feature engineering is not an easy task, especially for fraud detection. It requires domain knowledge, creativity, and experimentation. You need to know what kind of features and data sources are available and useful for fraud detection, and how to transform and combine them to create new features. You also need to know how to select the best features that can enhance the model without compromising its complexity and efficiency. Moreover, you need to deal with the challenges and limitations of fraud detection, such as imbalanced data, dynamic fraud patterns, privacy and security issues, etc.
In this blog, you will learn how to perform feature engineering and feature selection for fraud detection using scikit-learn and featuretools, two powerful Python libraries that can simplify and automate the process. But before we dive into the details, let’s first review some basic concepts and terminology related to feature engineering and feature selection.
2.1. Types of Features and Data Sources for Fraud Detection
In this section, you will learn about the different types of features and data sources that are commonly used for fraud detection, and how they can help you identify and prevent fraud. You will also see some examples of how to create and use these features using scikit-learn and featuretools.
Features can be classified into two main categories: static and dynamic. Static features are the ones that do not change over time, such as the customer’s name, age, gender, address, etc. Dynamic features are the ones that change over time, such as the transaction amount, time, location, device, etc. Static features can help you profile the customer and verify their identity, while dynamic features can help you detect anomalies and patterns in the transaction behavior.
Data sources can be classified into three main categories: transactional, behavioral, and external. Transactional data sources are the ones that contain information about the transactions, such as the amount, date, time, merchant, product, etc. Behavioral data sources are the ones that contain information about the customer’s actions and interactions, such as the device, browser, IP address, clickstream, keystrokes, mouse movements, etc. External data sources are the ones that contain information from third-party sources, such as social media, credit bureaus, blacklists, etc. Transactional data sources can help you measure the risk and value of the transactions, while behavioral and external data sources can help you enrich the transactional data and provide additional context and evidence.
Some examples of features and data sources for fraud detection are:
- Amount: The amount of the transaction, which can indicate the size and impact of the fraud. This is a dynamic and transactional feature.
- Location: The location of the transaction, which can indicate the origin and destination of the fraud. This can be derived from the IP address, GPS coordinates, zip code, etc. This is a dynamic and behavioral feature.
- Time: The time of the transaction, which can indicate the frequency and seasonality of the fraud. This can be derived from the date, hour, minute, second, etc. This is a dynamic and transactional feature.
- Device: The device used for the transaction, which can indicate the type and identity of the fraudster. This can be derived from the device ID, model, operating system, browser, etc. This is a dynamic and behavioral feature.
- Customer: The customer involved in the transaction, which can indicate the profile and history of the fraudster. This can be derived from the customer ID, name, age, gender, address, email, phone, etc. This is a static and transactional feature.
- Merchant: The merchant involved in the transaction, which can indicate the target and reputation of the fraud. This can be derived from the merchant ID, name, category, rating, etc. This is a static and transactional feature.
- Social Media: The social media activity of the customer or the merchant, which can indicate the trustworthiness and sentiment of the fraud. This can be derived from the social media accounts, posts, likes, comments, followers, etc. This is a dynamic and external feature.
- Credit Score: The credit score of the customer or the merchant, which can indicate the financial status and risk of the fraud. This can be derived from the credit bureaus, credit reports, credit history, etc. This is a static and external feature.
- Blacklist: The blacklist status of the customer or the merchant, which can indicate the previous involvement and evidence of the fraud. This can be derived from the fraud databases, fraud reports, fraud alerts, etc. This is a dynamic and external feature.
As you can see, there are many types of features and data sources that can be used for fraud detection, and each one can provide a different perspective and insight into the fraud problem. However, not all features and data sources are equally useful and available, and some may require more effort and resources to obtain and process. Therefore, you need to carefully select the most relevant and informative features and data sources for your fraud detection problem, and use them effectively to create and train your machine learning model.
In the next section, you will learn about the challenges and best practices of feature engineering for fraud detection, and how to overcome them using scikit-learn and featuretools.
2.2. Challenges and Best Practices of Feature Engineering for Fraud Detection
Feature engineering for fraud detection is not a trivial task. It involves many challenges and difficulties that need to be addressed and overcome. Some of the common challenges are:
- Data quality: The data used for fraud detection may be incomplete, inconsistent, noisy, or outdated. This can affect the quality and reliability of the features and the model. Therefore, you need to perform data cleaning and validation to ensure the data is accurate and up-to-date.
- Data imbalance: The data used for fraud detection may be highly imbalanced, meaning that the number of fraudulent transactions is much smaller than the number of legitimate transactions. This can affect the performance and evaluation of the model, as it may be biased towards the majority class and fail to detect the minority class. Therefore, you need to perform data resampling or weighting to balance the data and avoid overfitting or underfitting.
- Data complexity: The data used for fraud detection may be complex and heterogeneous, meaning that it may come from different sources, formats, and structures. This can affect the integration and compatibility of the features and the model. Therefore, you need to perform data transformation and standardization to unify the data and make it compatible with the model.
- Data privacy: The data used for fraud detection may contain sensitive and personal information, such as the customer’s name, address, email, phone, etc. This can affect the privacy and security of the data and the model, as it may be exposed to unauthorized access or misuse. Therefore, you need to perform data anonymization and encryption to protect the data and the model from potential threats.
To overcome these challenges, you need to follow some best practices and guidelines for feature engineering for fraud detection. Some of the best practices are:
- Understand the problem and the data: Before you start creating and selecting features, you need to have a clear understanding of the fraud detection problem and the data that you are working with. You need to know the objective, the scope, the assumptions, and the limitations of the problem and the data. You also need to know the characteristics, the distribution, the relationships, and the patterns of the data. This will help you choose the most appropriate and relevant features and data sources for the problem and the data.
- Explore and visualize the data: Before you start creating and selecting features, you need to explore and visualize the data that you are working with. You need to perform exploratory data analysis (EDA) and data visualization to gain insights and intuition about the data. You also need to identify and remove outliers, missing values, duplicates, and errors in the data. This will help you improve the quality and reliability of the data and the features.
- Create and select features iteratively: Feature engineering for fraud detection is an iterative and experimental process. You need to create and select features iteratively, based on the feedback and results of the model. You also need to evaluate and compare the features and the model using appropriate metrics and methods. This will help you optimize the performance and accuracy of the model and the features.
- Document and communicate the features and the model: Feature engineering for fraud detection is not only a technical but also a communicative process. You need to document and communicate the features and the model that you have created and selected, using clear and consistent terminology and notation. You also need to explain and justify the rationale and the evidence behind the features and the model, using visual and verbal aids. This will help you make the model and the features more interpretable and explainable.
In the next section, you will learn how to perform feature engineering using scikit-learn, a powerful Python library that provides various tools and algorithms for data analysis and modeling.
3. How to Perform Feature Engineering using Scikit-learn
Scikit-learn is a popular and powerful Python library that provides various tools and algorithms for data analysis and modeling. It supports many aspects of feature engineering, such as data preprocessing, transformation, extraction, generation, scaling, and normalization. In this section, you will learn how to use scikit-learn to perform feature engineering for fraud detection, and how to apply it to a real-world fraud detection dataset.
The dataset that you will use is the Credit Card Fraud Detection dataset from Kaggle, which contains transactions made by credit cards in September 2013 by European cardholders. The dataset contains 284,807 transactions, of which 492 are fraudulent. The dataset is highly imbalanced, as the fraudulent transactions account for only 0.17% of the total transactions. The dataset contains 30 numerical features, which are the result of a principal component analysis (PCA) transformation. The only features that have not been transformed are Time and Amount. The feature Time contains the seconds elapsed between each transaction and the first transaction in the dataset. The feature Amount contains the transaction amount. The feature Class is the target variable and it takes value 1 in case of fraud and 0 otherwise.
To perform feature engineering using scikit-learn, you need to follow these steps:
- Load and explore the data: You need to load the data into a pandas dataframe and explore its basic statistics and distribution. You also need to check for missing values, duplicates, and outliers in the data.
- Split the data into train and test sets: You need to split the data into two subsets: one for training the model and one for testing the model. You also need to stratify the split to preserve the class distribution in both subsets.
- Perform data preprocessing and transformation: You need to perform data preprocessing and transformation to improve the quality and compatibility of the data. You also need to handle the imbalanced data problem using resampling or weighting techniques.
- Perform feature extraction and generation: You need to perform feature extraction and generation to create new and informative features from the existing data. You also need to handle the high-dimensional data problem using dimensionality reduction techniques.
- Perform feature scaling and normalization: You need to perform feature scaling and normalization to standardize the range and distribution of the features. You also need to handle the skewed data problem using transformation techniques.
In the following sections, you will learn how to perform each of these steps using scikit-learn, and see the code and output examples.
3.1. Data Preprocessing and Transformation
Data preprocessing and transformation are the first steps of feature engineering, where you prepare the raw data for further analysis and modeling. Data preprocessing and transformation involve cleaning, formatting, and modifying the data to make it suitable and consistent for the machine learning model. Some common tasks of data preprocessing and transformation are:
- Handling missing values: Missing values are the values that are not recorded or available in the data. Missing values can affect the performance and accuracy of the machine learning model, as they can introduce bias and uncertainty. To handle missing values, you can either remove them, impute them, or ignore them, depending on the context and the amount of missing data.
- Handling outliers: Outliers are the values that are significantly different from the rest of the data. Outliers can also affect the performance and accuracy of the machine learning model, as they can distort the distribution and the statistics of the data. To handle outliers, you can either remove them, replace them, or keep them, depending on the context and the type of outliers.
- Handling categorical variables: Categorical variables are the variables that have a finite number of discrete values, such as gender, color, or type. Categorical variables cannot be directly used by most machine learning models, as they require numerical inputs. To handle categorical variables, you can either encode them, group them, or binarize them, depending on the context and the number of categories.
- Handling text data: Text data are the data that contain natural language, such as reviews, comments, or descriptions. Text data can provide valuable information for fraud detection, such as the sentiment, the tone, or the keywords of the text. To handle text data, you can either extract features, tokenize, or vectorize them, depending on the context and the goal of the analysis.
To perform data preprocessing and transformation using scikit-learn, you can use various classes and functions that are available in the preprocessing and feature_extraction modules. For example, you can use the SimpleImputer
class to impute missing values, the StandardScaler
class to scale the data, the OneHotEncoder
class to encode categorical variables, and the TfidfVectorizer
class to vectorize text data. You can also use the Pipeline
class to combine multiple preprocessing and transformation steps into a single object that can be applied to the data.
In the next section, you will learn how to perform feature extraction and generation using scikit-learn, where you create new features from the existing data that can improve the machine learning model.
3.2. Feature Extraction and Generation
Feature extraction and generation are the next steps of feature engineering, where you create new features from the existing data that can improve the machine learning model. Feature extraction and generation involve transforming, combining, or creating new features that can capture more information, patterns, or relationships from the data. Some common tasks of feature extraction and generation are:
- Transforming numerical variables: Numerical variables are the variables that have continuous or discrete numerical values, such as age, income, or score. Numerical variables can be transformed to change their distribution, scale, or range, to make them more suitable for the machine learning model. Some common transformations are logarithmic, exponential, polynomial, or power transformations.
- Combining numerical variables: Numerical variables can also be combined to create new features that can represent the interaction, ratio, or difference between them. For example, if you have two numerical features, such as the amount and the time of a transaction, you can create a new feature that represents the average amount per time unit, which can indicate the speed or the intensity of the transaction.
- Creating categorical variables: Categorical variables can also be created from numerical or text data, by grouping, binning, or labeling them. For example, if you have a numerical feature, such as the age of a customer, you can create a new categorical feature that represents the age group, such as young, middle-aged, or old. Or, if you have a text feature, such as the product description, you can create a new categorical feature that represents the product category, such as electronics, clothing, or books.
- Creating text features: Text features can also be created from numerical or categorical data, by converting, concatenating, or summarizing them. For example, if you have a numerical feature, such as the amount of a transaction, you can create a new text feature that represents the amount in words, such as “one hundred dollars”. Or, if you have a categorical feature, such as the customer name, you can create a new text feature that represents the initials, such as “J.D.”.
To perform feature extraction and generation using scikit-learn, you can use various classes and functions that are available in the preprocessing and feature_extraction modules. For example, you can use the PolynomialFeatures
class to generate polynomial features, the KBinsDiscretizer
class to bin numerical variables, the LabelEncoder
class to label categorical variables, and the CountVectorizer
class to create text features. You can also use the FeatureUnion
class to combine multiple feature extraction and generation steps into a single object that can be applied to the data.
In the next section, you will learn how to perform feature scaling and normalization using scikit-learn, where you adjust the range and the distribution of the features to make them more comparable and compatible for the machine learning model.
3.3. Feature Scaling and Normalization
Feature scaling and normalization are the final steps of feature engineering, where you adjust the range and the distribution of the features to make them more comparable and compatible for the machine learning model. Feature scaling and normalization involve changing the scale or the shape of the features, to make them more uniform and standardized. Some common tasks of feature scaling and normalization are:
- Scaling numerical variables: Scaling numerical variables is the process of changing the range or the magnitude of the numerical values, to make them more similar and consistent. Scaling numerical variables can help the machine learning model converge faster and perform better, as it reduces the effect of outliers and large differences between the features. Some common scaling methods are min-max scaling, standard scaling, or robust scaling.
- Normalizing numerical variables: Normalizing numerical variables is the process of changing the distribution or the shape of the numerical values, to make them more symmetric and bell-shaped. Normalizing numerical variables can help the machine learning model handle non-linear relationships and assumptions, as it reduces the skewness and the kurtosis of the features. Some common normalization methods are log-normalization, box-cox transformation, or quantile transformation.
- Normalizing text variables: Normalizing text variables is the process of changing the form or the structure of the text values, to make them more simple and consistent. Normalizing text variables can help the machine learning model extract and compare features from text data, as it reduces the noise and the variability of the text. Some common normalization methods are lowercasing, stemming, lemmatizing, or removing stopwords.
To perform feature scaling and normalization using scikit-learn, you can use various classes and functions that are available in the preprocessing and feature_extraction modules. For example, you can use the MinMaxScaler
class to scale numerical variables to a given range, the PowerTransformer
class to normalize numerical variables using a power transformation, and the TfidfTransformer
class to normalize text variables using term frequency-inverse document frequency.
Now that you have learned how to perform feature engineering using scikit-learn, you are ready to move on to the next step: feature selection. In the next section, you will learn how to perform feature selection using scikit-learn, where you select the best features that can optimize the machine learning model.
4. How to Perform Feature Selection using Scikit-learn
Feature selection is the process of selecting a subset of features from the original feature set that can provide the best performance and efficiency for the machine learning model. Feature selection can help reduce the dimensionality, complexity, and overfitting of the model, as well as improve its interpretability and explainability. Feature selection can also help save computational time and resources, as well as avoid the curse of dimensionality.
There are many methods and criteria for feature selection, but they can be broadly classified into three categories: filter methods, wrapper methods, and embedded methods. Filter methods use statistical measures to rank and select features based on their relevance and importance for the target variable. Wrapper methods use a search algorithm to evaluate and select features based on the performance of the model. Embedded methods use the model itself to select features based on their coefficients or importance scores.
In this section, you will learn how to perform feature selection using scikit-learn, a machine learning library that provides various tools and algorithms for data analysis and modeling. Scikit-learn offers several classes and functions for feature selection, such as SelectKBest
, SelectFromModel
, RFE
, and VarianceThreshold
. You will learn how to use these tools to apply filter, wrapper, and embedded methods for feature selection, and compare their results.
Before you start, you need to have a dataset with features and labels, and a machine learning model to use for feature selection. You can use the same dataset and model that you used for feature engineering in the previous section, or you can use a different one. For this tutorial, we will use the same dataset and model as before, which are the credit card fraud detection dataset from Kaggle and the logistic regression model from scikit-learn.
4.1. Filter Methods
Filter methods are feature selection methods that use statistical measures to rank and select features based on their relevance and importance for the target variable. Filter methods do not involve the machine learning model in the feature selection process, and they are independent of the model performance. Filter methods are usually fast and simple to implement, but they may not capture the interactions and dependencies among features and the model.
Some common statistical measures that filter methods use are:
- Correlation coefficient: This measures the linear relationship between two variables, and it ranges from -1 to 1. A high absolute value indicates a strong correlation, and a low value indicates a weak correlation. For feature selection, you can use the correlation coefficient to select features that have a high correlation with the target variable, and a low correlation with other features.
- Chi-square test: This tests the independence of two categorical variables, and it returns a p-value that indicates the probability of observing the data under the null hypothesis of independence. A low p-value indicates a high dependence, and a high p-value indicates a low dependence. For feature selection, you can use the chi-square test to select features that have a low p-value with the target variable, and a high p-value with other features.
- ANOVA test: This tests the difference of means among two or more groups of a numerical variable, and it returns a p-value that indicates the probability of observing the data under the null hypothesis of no difference. A low p-value indicates a high difference, and a high p-value indicates a low difference. For feature selection, you can use the ANOVA test to select features that have a low p-value with the target variable, and a high p-value with other features.
- Mutual information: This measures the amount of information that one variable provides about another variable, and it ranges from 0 to infinity. A high value indicates a high mutual information, and a low value indicates a low mutual information. For feature selection, you can use the mutual information to select features that have a high mutual information with the target variable, and a low mutual information with other features.
To perform filter methods using scikit-learn, you can use the SelectKBest
class, which selects the k best features based on a given scoring function. The scoring function can be one of the statistical measures mentioned above, or a custom function that you define. For example, if you want to select the 10 best features based on the chi-square test, you can use the following code:
from sklearn.feature_selection import SelectKBest, chi2 selector = SelectKBest(chi2, k=10) selector.fit(X, y) X_new = selector.transform(X)
In the next section, you will learn how to perform wrapper methods using scikit-learn.
4.2. Wrapper Methods
Wrapper methods are feature selection methods that use a search algorithm to evaluate and select features based on the performance of the machine learning model. Wrapper methods involve the machine learning model in the feature selection process, and they are dependent on the model performance. Wrapper methods are usually more accurate and comprehensive than filter methods, but they may also be more computationally expensive and prone to overfitting.
Some common search algorithms that wrapper methods use are:
- Forward selection: This starts with an empty feature set and iteratively adds features that improve the model performance until no further improvement is possible.
- Backward elimination: This starts with the full feature set and iteratively removes features that decrease the model performance until no further improvement is possible.
- Recursive feature elimination: This recursively eliminates features based on the model coefficients or importance scores, and selects the best feature subset based on the cross-validation score.
- Exhaustive search: This evaluates all possible feature subsets and selects the best one based on the model performance.
To perform wrapper methods using scikit-learn, you can use the SelectFromModel
class, which selects features based on a threshold of the model coefficients or importance scores. You can also use the RFE
class, which implements the recursive feature elimination algorithm. For example, if you want to select features based on the logistic regression model coefficients, you can use the following code:
from sklearn.feature_selection import SelectFromModel from sklearn.linear_model import LogisticRegression model = LogisticRegression() selector = SelectFromModel(model) selector.fit(X, y) X_new = selector.transform(X)
In the next section, you will learn how to perform embedded methods using scikit-learn.
4.3. Embedded Methods
Embedded methods are another type of feature selection methods that involve learning the optimal features as part of the model training process. Unlike filter and wrapper methods, embedded methods do not require a separate feature selection step, but rather incorporate the feature selection criteria into the model objective function. This makes embedded methods more efficient and less prone to overfitting than wrapper methods, and more accurate and adaptive than filter methods.
One of the most common and popular embedded methods is regularization, which is a technique that adds a penalty term to the model objective function to reduce the complexity and variance of the model. Regularization can help the model select the most relevant features by shrinking the coefficients of the less important features to zero, effectively removing them from the model. There are different types of regularization methods, such as Lasso, Ridge, and Elastic Net, that differ in how they penalize the coefficients.
To perform feature selection using regularization with scikit-learn, you can use the following steps:
- Import the regularization model of your choice from the
sklearn.linear_model
module. For example, you can import theLasso
model, which applies L1 regularization. - Create an instance of the model with the desired parameters. For example, you can specify the regularization strength with the
alpha
parameter, which controls how much the model penalizes the coefficients. A higher alpha value means more regularization and fewer features. - Fit the model to the training data and labels using the
fit
method. - Get the selected features by checking which coefficients are non-zero using the
coef_
attribute of the model. You can also use theget_support
method of thesklearn.feature_selection.SelectFromModel
class, which returns a boolean mask of the selected features. - Transform the training and test data to the selected features using the
transform
method of theSelectFromModel
class. - Evaluate the performance of the model on the test data using the appropriate metrics.
The following code snippet shows an example of how to perform feature selection using Lasso regularization with scikit-learn:
# Import the Lasso model and the SelectFromModel class from sklearn.linear_model import Lasso from sklearn.feature_selection import SelectFromModel # Create an instance of the Lasso model with alpha=0.01 lasso = Lasso(alpha=0.01) # Fit the model to the training data and labels lasso.fit(X_train, y_train) # Get the selected features by checking which coefficients are non-zero selected_features = lasso.coef_ != 0 print(f"Number of selected features: {sum(selected_features)}") # Alternatively, you can use the get_support method of the SelectFromModel class selector = SelectFromModel(lasso) selector.fit(X_train, y_train) selected_features = selector.get_support() print(f"Number of selected features: {sum(selected_features)}") # Transform the training and test data to the selected features X_train_selected = selector.transform(X_train) X_test_selected = selector.transform(X_test) # Evaluate the performance of the model on the test data y_pred = lasso.predict(X_test_selected) print(f"Test accuracy: {accuracy_score(y_test, y_pred)}")
5. How to Perform Automated Feature Engineering using Featuretools
Featuretools is an open-source Python library that can automate the feature engineering process for machine learning. Featuretools can generate hundreds of features from complex and relational data with minimal effort and code. Featuretools uses a technique called Deep Feature Synthesis (DFS), which can automatically create new features by applying various mathematical and logical operations on the existing data. DFS can also handle different types of data, such as categorical, numerical, temporal, and spatial, and create features that capture the interactions and relationships among them.
To perform automated feature engineering using featuretools, you need to follow these steps:
- Install featuretools using pip or conda.
- Import featuretools using
import featuretools as ft
. - Define the entities and relationships of your data using the
ft.EntitySet
class. An entity is a table or a dataframe that contains the data and the features. A relationship is a link between two entities that defines how they are connected. For example, if you have a transaction entity and a customer entity, you can define a relationship between them based on the customer ID. - Run the DFS algorithm using the
ft.dfs
function. This function takes the entity set, the target entity, and some optional parameters as inputs, and returns a feature matrix and a list of feature definitions as outputs. The feature matrix is a dataframe that contains the generated features for each instance of the target entity. The feature definitions are objects that describe how the features are created and can be reused for new data. - Select the best features from the feature matrix using the
ft.selection
module. This module provides various functions to remove the redundant, irrelevant, or highly correlated features from the feature matrix, and keep only the most useful and informative ones for the machine learning model. - Split the feature matrix into training and test sets, and train and evaluate the machine learning model using scikit-learn or any other library of your choice.
The following code snippet shows an example of how to perform automated feature engineering using featuretools:
# Install featuretools using pip pip install featuretools # Import featuretools import featuretools as ft # Load the fraud detection dataset data = pd.read_csv("fraud_detection_data.csv") # Define the entities and relationships of the data es = ft.EntitySet(id="fraud") # Add the transaction entity es = es.entity_from_dataframe(entity_id="transactions", dataframe=data, index="transaction_id", time_index="transaction_time", variable_types={"transaction_type": ft.variable_types.Categorical, "amount": ft.variable_types.Numeric, "location": ft.variable_types.Categorical, "merchant": ft.variable_types.Categorical, "customer": ft.variable_types.Categorical, "fraud_label": ft.variable_types.Boolean}) # Add the customer entity es = es.normalize_entity(base_entity_id="transactions", new_entity_id="customers", index="customer", additional_variables=["age", "gender", "income"]) # Define the relationship between transactions and customers relationship = ft.Relationship(es["customers"]["customer"], es["transactions"]["customer"]) # Add the relationship to the entity set es = es.add_relationship(relationship) # Run the DFS algorithm to generate features feature_matrix, feature_defs = ft.dfs(entityset=es, target_entity="transactions", agg_primitives=["count", "mean", "std", "max", "min", "mode"], trans_primitives=["month", "weekday", "hour", "is_weekend"], max_depth=2) # Print the number and names of the generated features print(f"Number of features: {len(feature_defs)}") print(f"Feature names: {feature_defs}") # Select the best features from the feature matrix feature_matrix = ft.selection.remove_low_information_features(feature_matrix) feature_matrix = ft.selection.remove_highly_correlated_features(feature_matrix) feature_matrix = ft.selection.remove_single_value_features(feature_matrix) # Print the number and names of the selected features print(f"Number of features: {feature_matrix.shape[1]}") print(f"Feature names: {feature_matrix.columns}") # Split the feature matrix into training and test sets X = feature_matrix.drop("fraud_label", axis=1) y = feature_matrix["fraud_label"] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train and evaluate a logistic regression model using scikit-learn from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score # Create an instance of the logistic regression model log_reg = LogisticRegression() # Fit the model to the training data and labels log_reg.fit(X_train, y_train) # Predict the labels for the test data y_pred = log_reg.predict(X_test) # Evaluate the accuracy of the model on the test data print(f"Test accuracy: {accuracy_score(y_test, y_pred)}")
5.1. What is Featuretools and How Does it Work?
Featuretools is an open-source Python library that automates the feature engineering process. It can generate hundreds of features from complex and relational data with minimal effort and code. Featuretools is based on the concept of deep feature synthesis, which is a method of creating new features by applying various mathematical and logical operations on existing features across different tables or entities.
Featuretools works by taking a set of entities and relationships as input, and producing a set of feature matrices as output. An entity is a table or a dataframe that contains information about a specific type of object, such as customers, transactions, products, etc. A relationship is a link between two entities that defines how they are connected, such as a foreign key or a join key. A feature matrix is a table or a dataframe that contains the generated features for each instance of a target entity, such as the customer ID and the features that describe their behavior.
To use Featuretools, you need to follow these steps:
- Define the entities and relationships that represent your data.
- Create an entity set, which is a collection of entities and relationships.
- Specify the target entity and the cutoff time, which is the latest time for which you want to generate features.
- Run the deep feature synthesis algorithm, which will create a feature matrix for the target entity.
- Select the best features from the feature matrix using various criteria, such as relevance, importance, correlation, etc.
In the next sections, you will learn how to apply these steps to a real-world fraud detection dataset and see how Featuretools can help you create and select relevant and informative features for fraud detection.
5.2. How to Define Entities and Relationships for Fraud Detection
In this section, you will learn how to define the entities and relationships that represent your fraud detection data using Featuretools. You will use a publicly available dataset from Kaggle, called Credit Card Fraud Detection, which contains transactions made by credit cards in September 2013 by European cardholders. The dataset contains 284,807 transactions, of which 492 are fraudulent. The transactions are labeled as either 0 (normal) or 1 (fraudulent). The dataset also contains 28 anonymized numerical features, which are the result of a principal component analysis (PCA) transformation, as well as the time and the amount of each transaction.
To use Featuretools, you need to define the entities and relationships that represent your data. An entity is a table or a dataframe that contains information about a specific type of object, such as customers, transactions, products, etc. A relationship is a link between two entities that defines how they are connected, such as a foreign key or a join key.
In this case, you have only one entity, which is the transaction entity. The transaction entity contains the following columns:
- index: The index of the transaction in the dataset.
- Time: The seconds elapsed between each transaction and the first transaction in the dataset.
- V1-V28: The anonymized numerical features obtained from PCA.
- Amount: The transaction amount.
- Class: The transaction class, either 0 (normal) or 1 (fraudulent).
To define the transaction entity, you need to import the Featuretools library and use the entity_from_dataframe function. This function takes the following arguments:
- entity_id: A unique name for the entity.
- dataframe: The dataframe that contains the entity data.
- index: The name of the column that uniquely identifies each row of the entity.
- time_index: The name of the column that indicates the time order of the entity.
- variable_types: A dictionary that maps the column names to their data types. Featuretools supports various data types, such as numeric, categorical, boolean, datetime, etc. You can also specify custom data types, such as natural language, latitude, longitude, etc.
The following code shows how to define the transaction entity using Featuretools:
# Import Featuretools library import featuretools as ft # Load the dataset data = pd.read_csv("creditcard.csv") # Define the transaction entity transaction_entity = ft.entity_from_dataframe( entity_id = "transaction", # A unique name for the entity dataframe = data, # The dataframe that contains the entity data index = "index", # The column that uniquely identifies each row of the entity time_index = "Time", # The column that indicates the time order of the entity variable_types = { # A dictionary that maps the column names to their data types "V1": ft.variable_types.Numeric, # The anonymized numerical features are numeric "V2": ft.variable_types.Numeric, ... "V28": ft.variable_types.Numeric, "Amount": ft.variable_types.Numeric, # The transaction amount is numeric "Class": ft.variable_types.Boolean # The transaction class is boolean } )
Since you have only one entity, you do not need to define any relationships. However, if you had more than one entity, you would need to use the Relationship class to define how they are connected. For example, if you had a customer entity and a transaction entity, you could define a relationship between them as follows:
# Define a relationship between customer and transaction customer_transaction_relationship = ft.Relationship( customer_entity["customer_id"], # The parent entity and its primary key transaction_entity["customer_id"] # The child entity and its foreign key )
After defining the entities and relationships, you need to create an entity set, which is a collection of entities and relationships. You can use the EntitySet class and its add_entity and add_relationship methods to create an entity set. For example, the following code shows how to create an entity set with the transaction entity:
# Create an entity set es = ft.EntitySet(id = "credit_card") # A unique name for the entity set # Add the transaction entity to the entity set es = es.add_entity(transaction_entity) # If you had more than one entity, you would also add them and their relationships to the entity set # For example, if you had a customer entity and a relationship between customer and transaction, you would do the following: # es = es.add_entity(customer_entity) # es = es.add_relationship(customer_transaction_relationship)
Now you have defined the entities and relationships that represent your fraud detection data using Featuretools. In the next section, you will learn how to generate and select features using Featuretools.
5.3. How to Generate and Select Features using Featuretools
Now that you have learned how to define entities and relationships for fraud detection using featuretools, you are ready to generate and select features using this powerful library. Featuretools can automatically create hundreds of features from your data by applying various mathematical and logical operations, such as aggregation, transformation, and combination. These features can capture complex and deep patterns and interactions that might be difficult or impossible to create manually.
To generate features using featuretools, you need to use the ft.dfs
function, which stands for deep feature synthesis. This function takes the following arguments:
entityset
: the entity set that you have defined in the previous section.target_entity
: the name of the entity for which you want to generate features. In our case, this is thetransactions
entity, since we want to predict whether a transaction is fraudulent or not.agg_primitives
: a list of aggregation functions that featuretools can apply to the related entities. For example,["mean", "max", "min", "std", "count"]
.trans_primitives
: a list of transformation functions that featuretools can apply to the target entity or the related entities. For example,["percentile", "is_weekend", "hour", "day", "month", "year"]
.max_depth
: the maximum depth of the feature tree that featuretools can generate. A higher depth means more complex and longer features, but also more computational cost and potential overfitting.
The output of the ft.dfs
function is a feature matrix and a list of feature definitions. The feature matrix is a pandas dataframe that contains the generated features as columns and the target entity’s index as rows. The feature definitions are a list of feature objects that describe how each feature was created.
Here is an example of how to use the ft.dfs
function to generate features for fraud detection:
# Import featuretools import featuretools as ft # Define the entity set es = ft.EntitySet(id="fraud") # Add the transactions entity es = es.entity_from_dataframe(entity_id="transactions", dataframe=transactions, index="transaction_id", time_index="transaction_time", variable_types={"transaction_id": ft.variable_types.Id, "amount": ft.variable_types.Numeric, "merchant_id": ft.variable_types.Id, "customer_id": ft.variable_types.Id, "fraud": ft.variable_types.Boolean}) # Add the merchants entity es = es.entity_from_dataframe(entity_id="merchants", dataframe=merchants, index="merchant_id", variable_types={"merchant_id": ft.variable_types.Id, "merchant_name": ft.variable_types.Categorical, "merchant_category": ft.variable_types.Categorical}) # Add the customers entity es = es.entity_from_dataframe(entity_id="customers", dataframe=customers, index="customer_id", variable_types={"customer_id": ft.variable_types.Id, "customer_name": ft.variable_types.Categorical, "customer_age": ft.variable_types.Numeric, "customer_gender": ft.variable_types.Categorical}) # Add the relationships r_transactions_merchants = ft.Relationship(es["merchants"]["merchant_id"], es["transactions"]["merchant_id"]) r_transactions_customers = ft.Relationship(es["customers"]["customer_id"], es["transactions"]["customer_id"]) es = es.add_relationships([r_transactions_merchants, r_transactions_customers]) # Generate features using deep feature synthesis feature_matrix, feature_defs = ft.dfs(entityset=es, target_entity="transactions", agg_primitives=["mean", "max", "min", "std", "count"], trans_primitives=["percentile", "is_weekend", "hour", "day", "month", "year"], max_depth=3)
The feature matrix will look something like this:
transaction_id | amount | fraud | … | COUNT | customers.MEAN | customers.MAX | … |
---|---|---|---|---|---|---|---|
1 | 100.0 | False | … | 1 | 150.0 | 200.0 | … |
2 | 200.0 | False | … | 1 | 150.0 | 200.0 | … |
3 | 300.0 | True | … | 1 | 300.0 | 300.0 | … |
4 | 400.0 | True | … | 1 | 400.0 | 400.0 | … |
5 | 500.0 | False | … | 1 | 500.0 | 500.0 | … |
As you can see, featuretools has created many features from the original data, such as the mean, max, min, std, and count of the amount for each customer and merchant, the percentile of the amount for each transaction, the hour, day, month, and year of the transaction time, and many more. Some of these features might be useful for fraud detection, while others might be irrelevant or redundant.
To select the best features from the feature matrix, you can use the ft.selection.remove_low_information_features
function, which removes features that have the same value for all or almost all of the instances, such as a constant or a boolean feature with only one value. You can also use the ft.selection.remove_highly_correlated_features
function, which removes features that have a high correlation with another feature, such as a linear or nonlinear relationship. These functions can help you reduce the dimensionality and complexity of the feature matrix, and avoid multicollinearity and overfitting issues.
Here is an example of how to use these functions to select features for fraud detection:
# Import featuretools import featuretools as ft # Remove low information features feature_matrix = ft.selection.remove_low_information_features(feature_matrix) # Remove highly correlated features feature_matrix, features_to_drop = ft.selection.remove_highly_correlated_features(feature_matrix, features=feature_defs)
The feature matrix will now have fewer features, but hopefully more relevant and informative ones. You can then use this feature matrix as the input for your machine learning model, and compare the results with the features that you have created and selected using scikit-learn.
In this section, you have learned how to generate and select features using featuretools, an automated feature engineering library that can create hundreds of features from complex and relational data with minimal effort. You have seen how to use the ft.dfs
function to perform deep feature synthesis, and how to use the ft.selection
module to remove low information and highly correlated features. You have also applied these techniques to a real-world fraud detection dataset and obtained a feature matrix that can be used for machine learning modeling.
6. Conclusion
In this blog, you have learned how to perform feature engineering and feature selection for fraud detection using scikit-learn and featuretools, two popular Python libraries for machine learning. You have seen how to use scikit-learn to perform data preprocessing, transformation, extraction, generation, scaling, and normalization, as well as filter, wrapper, and embedded methods for feature selection. You have also seen how to use featuretools to perform automated feature engineering, including defining entities and relationships, and generating and selecting features using deep feature synthesis.
By applying these techniques, you have created and selected relevant and informative features from raw data that can help a machine learning model learn and make predictions for fraud detection. You have also reduced the noise and redundancy of the data, and improved the performance and accuracy of the model. Moreover, you have gained insights into the factors and relationships that contribute to fraud, and provided value for fraud prevention and mitigation.
Feature engineering and feature selection are essential steps in any machine learning project, especially for fraud detection. They require domain knowledge, creativity, and experimentation, as well as the use of appropriate tools and methods. Scikit-learn and featuretools are two powerful libraries that can simplify and automate the process, and help you achieve better results and understanding.
We hope you have enjoyed this blog and learned something useful. If you want to learn more about feature engineering and feature selection, you can check out the following resources:
- Scikit-learn documentation on feature selection
- Featuretools documentation
- Kaggle course on feature engineering
- Machine Learning Mastery blog on feature engineering
Thank you for reading and happy feature engineering!