pub struct Dataset {
pub data: DataMatrix,
pub labels: Vec<u8>,
pub features: Vec<Bytes>,
pub ftype: FileType,
pub stride: u16,
}Expand description
A dataset contains data for training or inference, training requires labels
Fields§
§data: DataMatrixData used for training a model or calculating predictions
labels: Vec<u8>Data labels, can be empty if only used for inference
features: Vec<Bytes>N-gram byte features
ftype: FileTypeThe type of file represented
stride: u16Stride used to find the n-grams
Implementations§
Source§impl Dataset
impl Dataset
Sourcepub fn load<P: AsRef<Path>>(path: P) -> Result<Dataset>
pub fn load<P: AsRef<Path>>(path: P) -> Result<Dataset>
Load a file
§Errors
An error results if the file type can’t be determined, is incorrectly determined, or if the file isn’t a supported format.
Sourcepub fn probe<P: AsRef<Path>>(path: P) -> Result<DatasetProbe>
pub fn probe<P: AsRef<Path>>(path: P) -> Result<DatasetProbe>
Work out what loading this file will cost, without loading it.
Reads the header plus at most PROBE_SAMPLE_ROWS data rows — a few megabytes of a
file of any size — and projects the total from the average row length.
§Errors
Returns an error if the file can’t be read, its format can’t be determined from the extension, or its header doesn’t declare any features.
Sourcepub fn from_csv_file<P: AsRef<Path>>(
path: P,
data_length: usize,
) -> Result<Self>
pub fn from_csv_file<P: AsRef<Path>>( path: P, data_length: usize, ) -> Result<Self>
Create a dataset struct from a CSV file
§Errors
Returns an error if:
- The file can’t be read
- The data contained isn’t numeric
- Feature data is missing
- The expected amount of data isn’t encountered
Sourcepub fn from_csv_file_assume_data_length<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn from_csv_file_assume_data_length<P: AsRef<Path>>(path: P) -> Result<Self>
Create a dataset struct from a CSV file
§Errors
Returns an error if:
- The file can’t be read
- The data contained isn’t a float
- Feature data is missing
- The amount of columns can’t be determined
Sourcepub fn from_csv_string(contents: &str, data_length: usize) -> Result<Self>
pub fn from_csv_string(contents: &str, data_length: usize) -> Result<Self>
Create a dataset struct from a CSV string
§Errors
Returns an error if:
- The data contained isn’t numeric
- Feature data is missing
- The expected amount of data isn’t encountered
Sourcepub fn from_arff_file<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn from_arff_file<P: AsRef<Path>>(path: P) -> Result<Self>
Create a dataset struct from an ARFF string
§Errors
Returns an error if:
- The file can’t be read
- The data contained isn’t numeric
- Feature data is missing
- The expected amount of data isn’t encountered
Sourcepub fn from_arff_string(contents: &str) -> Result<Self>
pub fn from_arff_string(contents: &str) -> Result<Self>
Create a dataset struct from an ARFF string
§Errors
Returns an error if:
- The data contained isn’t numeric
- Feature data is missing
- The expected amount of data isn’t encountered
Sourcepub fn from_libsvm_file<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn from_libsvm_file<P: AsRef<Path>>(path: P) -> Result<Self>
Create a dataset struct from a libsvm file
§Errors
Returns an error if:
- The file can’t be read
- Feature data is missing
- The data isn’t in the expected format
- The expected amount of data isn’t encountered
Sourcepub fn from_libsvm_string(contents: &str) -> Result<Self>
pub fn from_libsvm_string(contents: &str) -> Result<Self>
Create a dataset from a libsvm string
§Errors
Returns an error if the file doesn’t contain the expected format or is missing features
Sourcepub fn create_save_from_benign_malicious_files_and_ngrams<P: AsRef<Path>>(
malicious_dir: P,
benign_dir: P,
ngrams_file: P,
output_file: P,
verbose: bool,
) -> Result<()>
pub fn create_save_from_benign_malicious_files_and_ngrams<P: AsRef<Path>>( malicious_dir: P, benign_dir: P, ngrams_file: P, output_file: P, verbose: bool, ) -> Result<()>
Given paths to malicious files, benign files, and n-grams (features), get a Dataset object.
§Errors
This will fail if:
- The directories for benign or malicious files don’t exist or are empty.
- The n-gram feature file doesn’t exist, is empty, or doesn’t have hexidecimal-encoded features
Sourcepub fn save_libsvm<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn save_libsvm<P: AsRef<Path>>(&self, path: P) -> Result<()>
Save a dataset as a libsvm file
§Errors
An error will result if the file can’t be opened for writing
Sourcepub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()>
Save the dataset using the file extension to determine data format
§Errors
There’s an error if the file can’t be written or if the format can’t be determined
Sourcepub fn validate(&self) -> bool
pub fn validate(&self) -> bool
Ensure the dataset is valid
- Same size data columns
- If present, the amount of data rows equals the amount of labels
Sourcepub fn shuffle(&mut self)
pub fn shuffle(&mut self)
Shuffle the data, ensuring the labels move with their rows.
This is a full Fisher-Yates pass, so every row is displaced. The previous
10 * log10(size) heuristic gave 50 random swaps for a 307,000-row dataset, which
left over 99.9% of it in its original order — and since training reshuffles every
epoch, the epochs all saw effectively the same ordering.
Rows are moved by swapping handles, not by copying feature data, so a full pass is cheap regardless of how wide the rows are.
Sourcepub fn split(&mut self, ratio: f32) -> Self
pub fn split(&mut self, ratio: f32) -> Self
Split the dataset, ideally into train/test datasets. The ratio indicates how much data is kept, the remaining size is shed and returned.
Sourcepub fn split_balanced(&mut self, ratio: f32) -> Result<Self>
pub fn split_balanced(&mut self, ratio: f32) -> Result<Self>
Shuffle the dataset and split it into a label-balanced training set and a test set.
ratio is the fraction of rows to keep, split evenly between benign (label 0) and
malicious (any non-zero label, matching the model’s convention). Every other row is
returned as the new dataset. For example, 1,000 rows with a ratio of 0.8 keeps 400
benign and 400 malicious rows and returns the other 200.
If either class has fewer rows than its half of the requested size, both classes are capped at the size of the smaller one so the kept set stays balanced; the shortfall goes to the returned dataset.
§Errors
Returns an error if the dataset has no labels, the labels don’t match the rows, or the
ratio isn’t between 0.0 and 1.0.
Sourcepub fn same_ngrams_ftype(&self, other: &Self) -> bool
pub fn same_ngrams_ftype(&self, other: &Self) -> bool
Ensure that two datasets have the same file type and same n-grams. The idea here is to ensure that we’re evaluating a model on test data with a dataset created from the same n-gram features.
Sourcepub fn reduce(&mut self, model: &LogisticRegression) -> Result<Vec<usize>>
pub fn reduce(&mut self, model: &LogisticRegression) -> Result<Vec<usize>>
The model training allows for the algorithm to not only train a model but determine the features most useful for determining benign vs. malicious. This action removes the features deemed unneeded.
§Errors
If the model would remove all features, an error is returned as an empty dataset isn’t useful, and it’s instead likely the modal and dataset weren’t for the same data collection.
Sourcepub fn column_sums(&self) -> Vec<f32>
pub fn column_sums(&self) -> Vec<f32>
Sum each feature column across every row.
A single pass over the rows, touching only their non-zero features. Summing via
Dataset::column_iter instead means one strided pass per column, which for a wide
dataset is both quadratic in work and hostile to the cache.
Sourcepub fn column_iter(&self, index: usize) -> Option<ColumnIterator<'_>>
pub fn column_iter(&self, index: usize) -> Option<ColumnIterator<'_>>
Returns an iterator over a column
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Dataset
impl<'de> Deserialize<'de> for Dataset
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for Dataset
Equality ignores row order, so a shuffled dataset still compares equal to the original.
impl PartialEq for Dataset
Equality ignores row order, so a shuffled dataset still compares equal to the original.
That makes it quadratic in the number of rows, which is fine for the small datasets in the tests but should not be used on a real one.
Source§impl TrainingData for Dataset
impl TrainingData for Dataset
Source§fn has_labels(&self) -> bool
fn has_labels(&self) -> bool
Source§fn labelled_rows(&self) -> impl Iterator<Item = (RowSlice<'_>, u8)> + '_
fn labelled_rows(&self) -> impl Iterator<Item = (RowSlice<'_>, u8)> + '_
Source§fn column_sums(&self) -> Vec<f32>
fn column_sums(&self) -> Vec<f32>
Source§fn validate(&self) -> bool
fn validate(&self) -> bool
Dataset::validate.Auto Trait Implementations§
impl Freeze for Dataset
impl RefUnwindSafe for Dataset
impl Send for Dataset
impl Sync for Dataset
impl Unpin for Dataset
impl UnsafeUnpin for Dataset
impl UnwindSafe for Dataset
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more