{'fraction_non_alphanumeric': 0.030129697372796808, 'fraction_numerical': 0.009045560359161955, 'mean_word_length': 5.011995201919232, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Clustering of Bank Customers using LSTM-based encoder-decoder and Dynamic Time Warping

Clustering is an unsupervised data mining technique that can be employed to segment customers. The efficient clustering of customers enables banks to design and make offers based on the features of the target customers. The present study uses a real-world financial dataset (Berka, 2000) to cluster bank customers by an encoder-decoder network and the dynamic time warping (DTW) method. The customer features required for clustering are obtained in four ways: Dynamic Time Warping (DTW), Recency Frequency and Monetary (RFM), LSTM encoder-decoder network, and our proposed hybrid method. Once the LSTM model was trained by customer transaction data, a feature vector of each customer was automatically extracted by the encoder.Moreover, the distance between pairs of sequences of transaction amounts was obtained using DTW. Another vector feature was calculated for customers by RFM scoring. In the hybrid method, the feature vectors are combined from the encoder-decoder output, the DTW distance, and the demographic data (e.g., age and gender). Finally, feature vectors were introduced as input to the k-means clustering algorithm, and we compared clustering results with Silhouette and Davies-Bouldin index. As a result, the clusters obtained from the hybrid approach are more accurate and meaningful than those derived from individual clustering techniques. In addition, the type of neural network layers had a substantial effect on the clusters, and high network error does not necessarily worsen clustering performance.


Introduction
Banks seek to obtain competitive advantages in today's devastating competition and globalization [Moin and Ahmed, 2012]. It is essential for the banking sector to identify advanced big data analysis methods, e.g., data mining techniques, in order to extract valuable information from a massive amount of data and improve strategic management and customer satisfaction [Hassani et al., 2018].
Service marketing research has shown that companies should not offer the same services for all customers in most cases. Therefore, customer clustering and customer relationship management are determinants of business survival [Ansari and Riasi, 2016]. Efficient customer clustering enables the effective segmentation of customers. Clustering arXiv:2110.11769v1 [cs.LG] 22 Oct 2021 Figure 1: Architecture of encoder-decoder [Flores, 2019].
classifies customers with similar features and demands into the same group. Through customer clustering, banks can better identify customer behavior and develop more effective marketing strategies. Also, banks take a step toward data-driven decision-making by customer clustering, enhancing their knowledge of customer behavior. Clustering is typically the initial step of customer segmentation. Thus, the present work seeks to extract efficient features to cluster bank customers based on their transactions.

Literature review
Previous studies clustered customers based on customer equity through the k-means and k-medoids techniques, comparing the performances of the two approaches. They found that k-means clustering outperformed k-medoids clustering based on both the average within-cluster (AWC) distance and the Davies-Bouldin index [Aryuni et al., 2018]. A relatively recent work employed self-organizing maps and k-means to cluster customers. The variables used are grouped into three as demographical variables, categorical consumption variables, and summary consumption variables [Yanık and Elmorsy, 2019]. Customers were clustered based on their three-month consumption and demographic data.
Although earlier works exploited either customer transaction data or demographic data, Davood et al. [Dawood et al., 2019] utilized a combination of transaction and demographic data to obtain more accurate results. Therefore, banks can achieve their business objectives by finding different groups of customers with similar financial behavior. The present study proposes an intelligent model of bank customer clustering based on customer transactions. The proposed model converts customer transactions and demographic data (e.g., gender and age) into a vector in a latent space. This vector representation of customer data helps cluster customers with similar transaction behavior in the same group. Clusters of higher accuracy can be obtained by using vector representation and customer features of higher optimality.

Theoretical background
Transaction data refer to the dataset of an event such as a financial transaction or online payment. Each transaction involves at least a time dimension and the transition amount. Here, transactions refer to bank transactions, such as payments or money transfers.
An artificial neural network (ANN) is a set of algorithms that attempt to detect fundamental relationships in a data set through a human brain-inspired process. An autoencoder is an unsupervised learning method in which ANNs are employed for representation learning. In particular, a neural network architecture is designed to impose a bottleneck to force a compressed representation of the primary input. Compression and reconstruction are complicated. However, a data structure (i.e., a correlation between input features) can be trained and used when forcing through the bottleneck.  [Tavenard, 2021].
This group of ANNs is employed to reduce dimensionality and diminish processing time and memory costs. These concepts were introduced by Hinton (1980) and the PDP Research Group. Autoencoders were considered restricted Boltzmann machines (RBMs) for deep architecture in the 2000s.
Auto-Encoder is a neural network that attempts to reconstruct its input at its output [Shi et al., 2018]. An autoencoder consists of two parts: an encoder and a decoder (See Figure 1), which generally are implemented by neural networks. The encoder and decoder can be viewed as two functions z = f (x) and r = g(z), the f (x) maps data point x from data space to feature space, while g(z) produces a reconstruction of data point x by mapping z from feature space to data space. In modern autoencoders, the two functions z = f (x) and r = g(z) usually are stochastic functions P encoder = (z|x) and P decoder = (r|z) , where r is the reconstruction of x. From the view of applications, it is important to note that one does not wish autoencoders to simply learn to copy of the input x. In other words, autoencoders are usually restricted in some ways that allow them to approximately learn the copy of the inputs [Zhai et al., 2018].
The present study also evaluated dynamic time warping (DTW). DTW measures the dependency or similarity of two time-series that may differ in time for time series. For example, DTW can find the similarity of two walking patterns, even when the waking speeds or accelerations are not the same in time intervals. DTW has analyzed time series of audio, video, and image data.

Proposed customer clustering framework
Customer data is typically stored in a raw form in the databases of banks, without labels of valuable or uncreditworthy customers. Thus, unsupervised approaches are more efficient in the extraction of customer features through transaction data.
The present study adopted a multilayer LSTM-based encoder-decoder to extract customer features from customer transactions. The input and output are two lists of the transaction sequences of customers. The output of the encoder is known as the latent space. Thus, each input transaction has a vector representation in the latent space that contains most characteristics of the transaction sequence. The LSTM layer was employed to help the network better learn the transaction time-series [Yu et al., 2019].
The performance of this encoder-decoder model was improved by incorporating the attention mechanism [Haghani et al., 2018]. This mechanism is used to tackle initial input sequence element information forgotten in the coded vector when the input sequence is long. In each output step, the last decoder latent state is utilized to generate an attention vector in the encoder for downsizing and disseminating information from the encoder to the decoder.
Customer transactions are introduced as inputs in chronological order to the proposed neural network. To this end, transactions are classified based on the account numbers of customers. Each transaction involves four aspects: (1) type, (2) timestamp, (3) amount, and (4) account balance. In order to equalize the dimensions of transactions, two-dimensional zero arrays are added to the list of transactions with a length less than the maximum length of transactions. A matrix of all customer transactions was obtained. The following equations represent the transaction feature vector, the feature matrix of a customer, and the feature vector of all customers: The data should be normalized before training. Normalization was carried out using the z-score as: Where Z is the final value, x is the initial value, µ is the mean, and σ is the standard deviation of the data. The average of the z-scores is zero. A Start-of-Sequence (SOS) label is applied to indicate the start of the transaction sequence so that the teacher forcing model is used. Teacher forcing is a strategy of training recurrent neural networks in which the output of a time step is used as the input of the next time step. The input of the decoder has only a start, and the output of the decoder has only an end. Thus, the input is shifted by a time step. Thus, it is required to use [ −1 −1 −1 −1 ] for the start of the sequence. Also, [ −2 −2 −2 −2 ] is used for the end of the sequence. For example, Once training is completed, it is required to define the decoder as a distinct model to receive customer transaction inputs and assign a feature vector to each customer to represent helpful information on the customer's transactions. The dimensionality of the latent space is recommended to be lower than the maximum number of transactions. The output of the decoder for each customer is a feature vector as follows: Where f i denotes feature i in the latent representation and dimension l is the latent layer. The final decoder output, which contains the features of all the customers, is obtained as a matrix: Decoder Output : Additionally, the DTW distance was employed to extract customer features to measure the similarity of pairs of customers. DTW obtains the distance between the transaction amounts of the two customers. Each customer's transaction amount and time are extracted and converted into a two-dimensional sequence in chronological order. Then, DTW was applied to measure the distance between the two transaction sequences. It is the minimum difference between the two transaction sequences under certain conditions. The transaction sequences of the customers are stored in a matrix whose rows and columns are bank transactions, and each entry denotes the DTW distance of the two transactions.
There are as many distances as customers. These distances can be considered as a new feature vector. Then, the DWT features and LSTM features are concatenated. Moreover, the demographic features of the customers (e.g., age, gender, longitude, and latitude) are included. Thus, three feature matrices are concatenated to construct a new matrix. The concatenated matrix has a feature with a size of m+n+d for each customer, in which m denotes the LSTM feature-length, n denotes the DTW distance, and d stands for the demographic data.
Indeed, it is required to reduce the dimensionality of the feature vector to improve clustering performance and visualize the results. In order to reduce feature vector dimensionality, the present study employed principal component analysis (PCA).
Finally, the dimension-reduced feature matrix was clustered using the k-means algorithm in light of its satisfactory performance for a large amount of data. Although different augmentations of K-means clustering have been introduced, the present study adopted the elbow method to find the efficient number of customer clusters.

Results and discussion
A shortage of publically available data due to customer privacy protection reasons was a significant challenge. The present study employed an enhanced variant of the Beka Dataset -the original database was published by Berka (2000). The Beka Database is the financial dataset of a bank in the Czech Republic. It contains data of over 5300 customers with nearly 1,000,000 transactions. Also, the bank granted 700 loans and issued approximately 900 credit cards (provided in the dataset) [Berka et al., 2000]. This study focuses on transactions and customer tables, as shown in Table.1. Figure 3: Optimal DTW path to calculate the distance between two customer transaction amount series. The present study has utilized 70% of the data as the training dataset, 20% as the test dataset, and the remaining 10% as the validation dataset for the encoder-decoder neural network. ReLU and sigmoid activation functions were utilized. The average loss was calculated to be 0.3509.
Then, customer clustering was implemented based on the customer distance matrix through DTW. The customers were divided into three clusters using the k-means method. A customer was randomly selected from each cluster by searching the dataset, plotting their transaction sequences. As you see in Figure.4 Customer Red had lower transaction amounts than Customer Blue. Moreover, Customer Yellow had a larger distance in their transactions. Therefore, it can be said that these clusters were significantly distinct.
Once neural network learning and DTW customer distance calculations are completed, we used another traditional clustering method to compare the results better. This method, called RFM, scores users based on three values: recency, frequency, and monetary. the customers were clustered in four scenarios: • LSTM features • DTW distances • RFM score • Hybrid Table.3 compares the clustering approaches. The Silhouette Coefficient (SC) and Davies-Bouldin Index (DBI) were utilized to evaluate the clusters.

Conclusion
After analyzing the results, some points can be summarized as follows: 1. The attention mechanism enhanced the accuracy of the proposed neural network and this clustering performance.
2. A low error does not necessarily lead to high clustering performance -the opposite was the case with most cases.
3. The training and testing of the model showed that the final clusters would be more unrealistic when the dimensionality of the latent space was larger than the maximum number of transactions of a customer.
4. The DTW-extracted features had high continuity. Therefore, the individual DTW approach did not yield significant clustering performance.
5. The proposed hybrid model was found to have higher performance evaluation indices than the two individual approaches in most cases.
6. Pre-concatenation dimensionality reduction led to higher clustering performance than post-concatenation dimensionality reduction.