How to do it...

In the following steps, we will demonstrate a complete workflow in which we begin with raw samples, featurize them, vectorize their results, put them together, and finally train and test a classifier:

  1. Begin by enumerating our samples and assigning their labels:
import osfrom os import listdirdirectories_with_labels = [("Benign PE Samples", 0), ("Malicious PE Samples", 1)]list_of_samples = []labels = []for dataset_path, label in directories_with_labels:    samples = [f for f in listdir(dataset_path)]    for sample in samples:        file_path = os.path.join(dataset_path, sample)        list_of_samples.append(file_path)        labels.append(label)
  1. We perform a stratified train-test split:
from sklearn.model_selection import train_test_splitsamples_train, ...

Get Machine Learning for Cybersecurity Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.