# Extract features

Students will need to implement their own `extract()` method. This method receives the `protocol, src, sport, dst, dport` values giving information for the entire flow. Additionally, the method receives `timestamps` and `sizes`, two `np.array` objects giving the numerical value of the timestamps and packet sizes, respectively. In this assignment, the students should extract features from these values and output them as an `array-like` object, e.g., `list()`, `np.array()`, `tuple`.

`Assignment.``extract`(protocol, src, sport, dst, dport, timestamps, sizes)

  Extract features from each flow. Must be implemented by the students.

  Parameters

    - protocol (string) – Either ‘TCP’ or ‘UDP’.

    - src (string) – Source IP address as string.

    - sport (int) – Source port.

    - dst (string) – Destination IP address as string.

    - dport (int) – Destination port.

    - timestamps (np.array of shape=(n_packets,)) – Timestamp for each packet in the flow.

    - sizes (np.array of shape=(n_packets,)) – Size of each packet in the flow.

  Returns

    features – Extracted features, determined by the student.

  Return type

    array-like of shape=(n_features,)

We create a feature matrix using the student implementation of `Assignment.extract()` with the `feature_matrix()` method. This method calls the `extract()` method for each flow in the dataset and transforms the output to a numpy array (matrix) of shape=``(n_flows, n_features)``. Because of the transformation to a numpy array, the output of `extract()` should be an `array-like` object.

`Assignment.``feature_matrix`(data)

  Computes the feature matrix for the implemented ‘extract’ method.

  Parameters

    data (iterator) – Iterator over flow-tuple, flow-dataframe.

  Returns

    feature_matrix – Feature matrix for given data.

  Return type

    np.array of shape=(n_flows, n_features)

We show the feature matrix by transforming the output of `Assignment.feature_matrix()` to a pandas DataFrame, which is automatically displayed nicely in Jupyter Notebook.

`Assignment.``show_matrix`()

  Shows the feature matrix for benign flows.

  1. Extracts the feature matrix

  2. Turns it into a pd.DataFrame.

The `test_extract()` method loops over a given number of flows, calls the `extract()` method for each flow, and shows the output. This method is available for students to test their implementation of `extract()`.

`Assignment.``test_extract`(maximum=inf)

  Tests the extract function implemented by students.

  Parameters

    maximum (float, default='inf') – Maximum number of flows to test.