Skip to main content

BanknoteAuthentication

Struct BanknoteAuthentication 

Source
pub struct BanknoteAuthentication { /* private fields */ }
Expand description

A struct that represents the Banknote Authentication dataset with lazy loading.

The dataset loads only when you call a data accessor method. After the first load, the dataset caches the data for later accesses.

§About Dataset

Researchers extracted the data from images of genuine and forged banknote-like specimens. They digitized the images with an industrial camera normally used for print inspection. This camera produced 400×400 pixel grayscale images at a resolution of about 660 dpi. Researchers then used a Wavelet Transform tool to extract four continuous statistics from each image. These statistics are the variance, skewness, curtosis, and entropy of the transformed image. They cover 1372 specimens.

§Columns

NameTypeDescription
varianceNumericvariance of the Wavelet-Transformed image
skewnessNumericskewness of the Wavelet-Transformed image
curtosisNumericcurtosis of the Wavelet-Transformed image
entropyNumericentropy of the image
classIntegerraw class code, 0 or 1

curtosis keeps the source’s spelling. UCI names the attribute that way.

The source designates the four statistics as the inputs (BanknoteAuthentication::FEATURE_NAMES) and class as the label (BanknoteAuthentication::TARGET).

UCI does not document which class code marks a genuine note and which one marks a forged note. The loader keeps the code verbatim.

Missing values: none.

See more information at https://archive.ics.uci.edu/dataset/267/banknote+authentication.

§Citation

V. Lohweg. “Banknote Authentication,” UCI Machine Learning Repository, [Online]. Available: https://doi.org/10.24432/C55P57

§Thread Safety

This struct implements Send and Sync automatically, because all fields implement them. This makes the struct safe to share across threads. The internal Dataset makes lazy initialization thread-safe.

§Example

use dataset_ml::BanknoteAuthentication;

// the loader creates the directory if it does not exist
let download_dir = "./banknote_authentication";

let mut dataset = BanknoteAuthentication::new(download_dir);
let table = dataset.data().unwrap();

assert_eq!(table.n_samples(), 1372);
assert_eq!(table.n_columns(), 5);

// Ask for the feature matrix when you want it.
let features = table.numeric_matrix(&BanknoteAuthentication::FEATURE_NAMES).unwrap();
assert_eq!(features.shape(), &[1372, 4]);

// Reach one column by name.
let class = table.column(BanknoteAuthentication::TARGET).unwrap().as_integer().unwrap();
assert_eq!(class.len(), 1372);

// `get_data_mut()` edits the table in place. This needs no clone and no
// reload. The change stays cached.
if let Some(table) = dataset.get_data_mut() {
    if let Some(column) = table.column_mut("variance") {
        if let dataset_ml::ColumnData::Numeric(values) = column.data_mut() {
            values[0] = 0.5;
        }
    }
}
assert!(dataset.get_data().is_some());

// `take_data()` moves the owned table out with no clone. This leaves the
// instance reusable.
let owned = dataset.take_data().unwrap();
assert_eq!(owned.n_samples(), 1372);

// `into_data()` also returns the owned table with no clone, but it consumes
// the instance.
let owned = dataset.into_data().unwrap();
assert_eq!(owned.n_samples(), 1372);

Implementations§

Source§

impl BanknoteAuthentication

Source

pub const FEATURE_NAMES: [&'static str; 4]

The columns the source designates as the model inputs, in source order.

Source

pub const TARGET: &'static str = "class"

The column the source designates as the label.

Source

pub fn new(storage_dir: &str) -> Self

Create a new BanknoteAuthentication instance without loading data.

The dataset loads lazily, on your first call to a data accessor method. This is a lightweight operation that only stores the storage directory.

§Parameters
  • storage_dir - The directory that stores the dataset.
§Returns
  • Self - a BanknoteAuthentication instance ready for lazy loading.
Source

pub fn data(&self) -> Result<&Table, DatasetError>

Get a reference to the parsed table.

This method triggers lazy loading on the first call. Later calls return the cached data.

§Returns
  • &Table - reference to the cached table of 1372 samples and 5 columns.
§Errors

Returns DatasetError if:

  • Download fails due to network issues
  • File extraction or I/O operations fail
  • Data format is invalid (wrong number of columns, unparseable values, or invalid labels)
Source

pub fn get_data(&self) -> Option<&Table>

Get a reference to the parsed table without triggering loading.

Unlike BanknoteAuthentication::data, this method never runs the loader. If the data has not loaded yet, it returns None instead of downloading and parsing it.

§Returns
  • Some(&Table) - reference to the cached table, if loaded.
  • None - if the dataset has not loaded yet.
Source

pub fn get_data_mut(&mut self) -> Option<&mut Table>

Get a mutable reference to the parsed table for in-place editing.

This needs no clone, and it does not remove the data from the cache. The changes stay in the cache. Later calls to BanknoteAuthentication::data or BanknoteAuthentication::get_data see them.

Like BanknoteAuthentication::get_data, this does not trigger loading.

§Returns
  • Some(&mut Table) - mutable reference to the cached table, if loaded.
  • None - if the dataset has not loaded yet.
Source

pub fn into_data(self) -> Result<Table, DatasetError>

Consume the dataset and return the owned table.

This consumes self. If you want owned data but need to keep using the instance, use BanknoteAuthentication::take_data instead.

§Returns
  • Table - the owned table of 1372 samples and 5 columns.
§Errors

Returns DatasetError if loading fails (network, file I/O, or parsing).

Source

pub fn take_data(&mut self) -> Result<Table, DatasetError>

Take the owned table out of the dataset. This leaves the instance reusable.

This resets the instance to its unloaded state. The next accessor call loads the dataset again.

§Returns
  • Table - the owned table of 1372 samples and 5 columns.
§Errors

Returns DatasetError if loading fails (network, file I/O, or parsing).

Trait Implementations§

Source§

impl Debug for BanknoteAuthentication

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl MlDataset for BanknoteAuthentication

Source§

const NAME: &'static str = "banknote_authentication"

The dataset’s identifier, matching the one used in its error messages (for example, "iris", "sms_spam").
Source§

fn dataset(&self) -> &Dataset<Table, DatasetError>

Borrow the underlying container.
Source§

fn dataset_mut(&mut self) -> &mut Dataset<Table, DatasetError>

Borrow the underlying container mutably.
Source§

fn into_dataset(self) -> Dataset<Table, DatasetError>

Consume the loader and return the underlying container.
Source§

fn load(&self) -> Result<&Table, DatasetError>

Load the dataset if needed and borrow the table. Read more
Source§

fn load_mut(&mut self) -> Result<&mut Table, DatasetError>

Load the dataset if needed and borrow the table mutably. Read more
Source§

fn peek(&self) -> Option<&Table>

Borrow the table without triggering loading. Read more
Source§

fn unload(&mut self) -> Option<Table>

Move the table out, leaving the loader reusable and unloaded. Read more
Source§

fn is_loaded(&self) -> bool

Whether the cache currently holds the table. Read more
Source§

fn storage_dir(&self) -> &str

The directory this loader stores its files in.
Source§

fn invalidate(&mut self)

Drop the cached table, keeping the loader usable. Read more
Source§

fn n_samples(&self) -> Result<usize, DatasetError>

The number of samples in the dataset, loading it if needed. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.