In this notebook, we introduce the ENSIGN Python API and demonstrate how to use tensor decomposition to analyze data. Most, but not all Python API features are used, and we highly recommend referring to the Python API HTML documentation for additional capabilities. Further example calls to the Python API are also available in the ENSIGN User Guide and ENSIGN Tensor Guidebook.

The overview uses a dataset of Domain Name System (DNS) requests from systems on the Reservoir Labs network. The dataset has been anonymized by masking actual internal Internet Protocol (IP addresses). Typically, we analyze DNS data to find trends and anomalies in the domain names that clients are looking up. These trends and anomalies can be useful for discovering malware, discovering misconfigured devices, and defining network baseline traffic. This is but one possible application of tensor decomposition, and we recommend viewing the ENSIGN Use Case Notebooks, which apply tensor decomposition to geospatial analysis, cyber security, fake news detection, COVID-19 tracking, and bioinformatics.

In [1]:

```
import os
import pandas as pd
#--------
#-------- BEGIN ENSIGN imports
#--------
from ensign.comp_top_k import get_top_k
import ensign.cp_decomp as cpd
import ensign.csv2tensor as c2t
from ensign.query_decomp import query_decomp
import ensign.sptensor as spt
import ensign.visualize as viz # Must be imported before matplotlib
#--------
#-------- END ENSIGN imports
#--------
import matplotlib.pyplot as plt
# To use matplotlib in a Jupyter notebook
%matplotlib inline
# Limit the number of threads the decomposition algorithm uses
os.environ['OMP_NUM_THREADS'] = '16'
```

ENSIGN is built to "play nicely" with the widely-used pandas data analysis package. Here, we read a CSV file of information on DNS requests into a pandas dataframe. The data file contains one line per DNS connection. The dataframe will be converted to a tensor for decomposition and analysis.

Further information on the meaning of the fields in this file can be found in the documentation for Zeek, an open source network monitoring tool.

In [2]:

```
# Path to input data
CSV_FILE = 'data/dns_example.csv'
data = pd.read_csv(CSV_FILE)
data.tail()
```

Out[2]:

Tensor decomposition algorithms do not operate directly on CSV files but on tensors formed from the CSV. A tensor is a multidimensional array in which each dimension, or mode, corresponds to a different feature in the data and the indices in a given mode correspond to the possible discrete values of the feature. We convert the data into a tensor with the ENSIGN ETL tool `csv2tensor()`

, which creates a sparse tensor from a selection of features in the CSV. By default, the tensor is a count tensor, whose entries are the number of CSV entries that have the combination of features specified by the indices, but ENSIGN also supports Boolean and value tensors. A Boolean tensor indicates the existence of a CSV entry, and a value tensor uses another feature of the data for the tensor entries. The sparse tensor structure is explored more below by examining the fields of the sparse tensor object. The basic arguments used by the tensor construction function are:

`filepaths`

: The path to one or more input files.`columns`

: The columns from data that are included in the tensor. Each column maps to one mode of the tensor.`types`

: The type of each column. The type of the first column is`timestamp`

and it is formatted as seconds since the epoch. Any date format compatible with Python's`strftime()`

and`strptime()`

can also be used along with the`datetime`

type. The rest of the columns are treated as strings (`str`

). Other available types 64-bit integer (`int64`

), 64-bit float (`float64`

), and IPv4/IPv6 addresses (`ip`

).`binning`

: How we discretize the data in each column. This describes how we map the given data from its representation in the CSV file/dataframe to the indices 0, 1, ... used in the tensor representation. We use`minute`

to specify that all timestamps in a minute are binned together. In other words, timestamps representing 00:00:23, 00:00:26, and 00:45 would all map to the minute 00:00 and the index 0. Different binning strategies are available for`datetime`

,`timestamp`

,`int64`

,`float64`

, and`ip`

types. See the`csv2tensor()`

documentation for more details.

In our running DNS example, we choose columns corresponding to time, source and destination IP addresses, and destination port. These features are chosen because they encapsulate who is making requests and when. As the IP addresses and ports are discrete entities, we only bin the timestamp (to the nearest minute) so that close timestamps are considered semantically the same.

In [3]:

```
tensor = c2t.csv2tensor(
CSV_FILE,
columns=['ts', 'id.orig_h', 'id.resp_h', 'id.resp_p'],
types=['timestamp', 'str', 'str', 'str'],
binning=['minute', 'none', 'none', 'none'],
gen_backtrack=True
)
```

We now explore the fields in the sparse tensor object produced by `csv2tensor()`

.

**Order**: A tensor is a multidimensional array. The number of dimensions is referred to as the "order" of a tensor. For the tensor we just created, the order is 4.

In [4]:

```
tensor.order
```

Out[4]:

**Mode Names**: Each of those dimensions corresponds to one column of the original CSV file. In the language of tensors, a dimension is referred to as a "mode."

In [5]:

```
tensor.mode_names
```

Out[5]:

**Mode Sizes**: Each of the modes has a size determined by the number of unique data values in the original CSV file for that column after they have been binned.

In [6]:

```
tensor.mode_sizes
```

Out[6]:

**Labels**: Each mode has a number of labels equal to its size. These labels correspond to the values from the original CSV after binning. We print the first three labels of each mode below. Note that the labels for the `'ts'`

mode have been binned by minute.

In [7]:

```
for mode_id in range(4):
print(tensor.mode_names[mode_id] + " labels: " + str(tensor.labels[mode_id][:3]))
```

**Sparse Tensor and Entries**: As shown through the other fields, we have an order 4 tensor (i.e., a 4-dimensional array) of size 61 x 70 x 21 x 4. Each dimension of this array can be addressed by an integer or, equivalently, a label. Each value in the tensor (i.e., each value in the multidimensional array) can be addressed by a tuple of integers or, equivalently, a tuple of labels. Storing every value of the tensor would waste memory, so we choose to only store elements with nonzero values. This collection of values, and the tuples that index them, are collectively called a "sparse tensor." A tuple and its corresponding value are referred to as an "entry" of the sparse tensor.

We show the first five entries of the sparse tensor we created below. Note the following:

- Each row represents an entry of the tensor. The first four columns form a tuple e.g.,
`(0,0,0,0)`

for the zeroeth row below. The last entry is the value of the tensor at that tuple e.g., 2 for the zeroeth row below. - The tuple can be converted to the binned data values from the original CSV using labels. For example,
`(0,0,0,0)`

can be converted to`('2014-07-27 17:59:00', 'x.x.x.17', 'x.x.x.21', '53')`

- The value of an entry corresponds to the number of times that tuple was seen in the binned data from the original CSV.

In [8]:

```
tensor.entries.head()
```

Out[8]:

**Number of Nonzeroes (NNZ)**: In a sparse tensor, we only represent those elements that have a nonzero value. To get a count of how many entries there are in a tensor, we look at the `nnz`

attribute of the tensor.

In [9]:

```
tensor.nnz
```

Out[9]:

Having constructed a tensor that encapsulates features of our original data, we can now decompose our sparse tensor. The ENSIGN API supports several flavors of CANDECOMP/PARAFAC (CP) decomposition, which decomposes a tensor into sums of outer products of vectors. Each outer product is called a "component" and in practice represents subsets of correlated indices in each mode. Components typically represent a semantically coherent pattern-of-activity in the original tensor. The number of components is called the "rank" of the decomposition.

The varieties of CP decomposition in ENSIGN are CP-ALS, CP-ALS-NN, CP-APR, CP-APR-PDNR, and CP-APR-PQNR, which all make different assumptions of tensor data type and how to perform the optimization. In our running DNS example, we use the CP-APR decomposition for count tensors.

In [10]:

```
rank = 50
decomposition = cpd.cp_apr(tensor, rank, gen_backtrack=True)
```

The decomposition can be written as a set of factor matrices and a set of weights.

**Factors:** There is one factor matrix per mode, and each has dimensions of mode size by rank. Each column of the factor matrix is that mode's vector in the outer product in one component. Each row represents a label in that mode. Factor matrices are accessed via the list `decomposition.factors`

and each factor matrix is a NumPy `ndarray`

.

In [11]:

```
decomposition.factors[0] # Factor matrix for ts mode
```

Out[11]:

**Weights:** Each component has a weight, which represents the approximate number of original CSV lines represented by that component. Weights are are accessed via the list `decomposition.weights`

and each weight is a 64-bit `float`

.

In [12]:

```
decomposition.weights[0] # Weight of Component 0
```

Out[12]:

**Metrics**: Statistics on the quality of the decomposition can be found in the `decomposition.metrics`

dictionary. The available data are:

- time: How long the decomposition algorithm ran (in seconds)
- fit: The final fit, a measure of how well the decomposition reconstructs the tensor
- cosine_sim: The cosine similarity between the tensor and the original decomposition
- norm_scaling: An alternative to fit
- coverage: The percentage of tensor entries that are nontrivially reconstructed by the decomposition
- cp_total_iter: The number of CP iterations performed by the algorithm

The high fit and complete coverage indicate that this decomposition represents the original data well. A near-perfect fit is not necessary to obtain good results.

In [13]:

```
decomposition.metrics
```

Out[13]:

Because decompositions can be long running operations, we frequently wish to save the results to disk. The `write_cp_decomp_dir()`

function does this by saving the factor matrices and weights of a decomposition as text files. The tensor that was decomposed is also written if requested. For later analysis, tensors and their decomposition can be read from disk.

In [14]:

```
cpd.write_cp_decomp_dir('decomposition', decomposition, write_tensor=True)
```

An effective method for analyzing the results of a decomposition is plotting components. We use the `plot_component()`

function below to produce a visual representation of one component in a decomposition. Recall that a component is a scaled outer product of columns of the factor matrices, so we plot each mode of a component.

The subplots below each represent one column of the corresponding factor matrix. The x-axis represent the labels of the mode, while the y-axis represents the score for each label. As a component is a pattern of related tensor entries, the score represents how dominant a given label is in the pattern. A score of 0.0 means that a label is not represented in a pattern. A single score with a value of 1.0 means that, for the given mode, the label with score 1.0 is the only label represented by the pattern. The most common case is when there are a number of scores between 0.0 and 1.0 where the magnitude of the score indicates the relative importance of the score in the pattern. The scores in each mode are normalized such that their sum is equal to 1.0. The highest scoring labels in each mode are printed to the right of the plots.

Intuitively, any tuple of indices formed by choosing one non-zero score from each mode should corresopnd to one or more entries in the original CSV data that are involved in this pattern. In practice, the decomposition represents the best fit of the data to the model, so compression may occur resulting in some tuples not being represented in the original data. This is normal and, in fact, often desirable in condensing the data into coherent patterns.

Components are sorted by weight. We plot the zeroeth and highest-weighted component below. Its weight is approximately 1976, which is a rough estimate of the sum of tensor entry values associated with this pattern. It captures baseline DNS requests from computers on the network. We determine this by analyzing the modes. The non-zero scores in the source IP mode are machines on the network and the only non-zero score in the destination IP mode is the network's primary DNS server (recognized by its IP address). The single non-zero scoring index in the destination port is not surprisingly for port 53. Finally, we conclude this component is capturing baseline requests due to the consistent scores across the time mode. It is very typical to capture such expected behavior in the highest weight component.

In [15]:

```
comp_id = 0
viz.plot_component(decomposition, comp_id)
```

Out[15]:

In order to determine the significance of a component, it is useful to examine the top scoring labels in each mode. ENSIGN provides a utility function, `get_top_k()`

, to retrieve the top scoring labels in one or more components. Below, we see an example invocation of this function that gets the top 10 highest scoring labels for all modes of all components of a decomposition. The arguments are described below.

`decomposition.factors`

: The factor matrices of a decomposition to examine`decomposition.labels`

: The labels corresponding to the above factor matrices`list(range(rank))`

: A list of integers from 0 to the rank of the decomposition minus 1. This list tells`get_top_k()`

what components to examine.`k=10`

: The maximum number of labels to return for each mode of each component

Here we use `get_top_k()`

to examine the source IP addresses involved in Component 0.

In [16]:

```
top_k = get_top_k(decomposition.factors, decomposition.labels, list(range(rank)), k=10)
# Print results for mode 1 of Component 0
comp_id = 0
mode_id = 1
print('LABEL\t\t\t|INDEX\t\t|SCORE')
for tup in top_k[comp_id][mode_id]:
print('{:<23}\t|{:<14}\t|{}'.format(*tup))
```

If a pattern contains a label of interest, it may be important to know which other patterns include the label. ENSIGN provides this functionality with the `query_decomp()`

function. Below we see an example invocation of this function that searches the decomposition for components that contain the label "x.x.x.21" in mode 2. The arguments are described below.

`decomposition.factors`

: The factor matrices of a decomposition to examine`decomposition.labels`

: The labels corresponding to the above factor matrices`[mode_id]`

: A list of the mode_ids to search. In this case we are only searching in mode 1.`x.x.x.21`

: The label we are searching for

Not surprisingly, the tool will indicate that "x.x.x.21" appears in mode 2 in component 0, but it also indicates which other components involve that particular destination IP address.

In [17]:

```
mode_id = 2
query_decomp(decomposition.factors, decomposition.labels, [mode_id], 'x.x.x.21')
```

Out[17]:

By providing the `gen_backtrack=True`

argument to both `csv2tensor()`

and `cp_apr()`

, we obtain mappings from tensor entries to original lines and from decomposition components to tensor entries, respectively. We can now compose these maps in order to see the original log entries corresponding to a component. This allows an analyst to examine all other fields not used in tensor construction when studying a pattern. We show how to recover the original log entries associated with component 0. As expected, many of the queries are for websites, which include Google, Gmail, and GitHub.

In [18]:

```
comp_id = 0
idxs = []
for entry in decomposition.cpd_backtrack[comp_id]:
idxs += [line for [log, line] in tensor.spt_backtrack[entry]]
data.loc[idxs].reset_index(drop=True).head(10)
```

Out[18]:

In this notebook, we introduced the ENSIGN Python API for tensor construction and decomposition. We explained the basic principles of tensor decomposition and alluded to its utility in extracting patterns of interest in a variety of data. This notebook provides the requisite knowledge of ENSIGN and tensor decomposition to explore our other use case notebooks, which demonstrate how ENSIGN can be applied to practical problems in geospatial analysis, cyber security, fake news detection, COVID-19 tracking, and bioinformatics.