pub struct NDArray {
pub data: Vec<f64>,
pub shape: Vec<usize>,
}
Fields§
§data: Vec<f64>
§shape: Vec<usize>
Implementations§
Source§impl NDArray
impl NDArray
Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Normalizes the array values to range [0, 1] using min-max normalization
Sourcepub fn normalize_with_range(&mut self, min_val: f64, max_val: f64)
pub fn normalize_with_range(&mut self, min_val: f64, max_val: f64)
Normalizes the array values using specified min and max values
pub fn data_mut(&mut self) -> &mut Vec<f64>
Sourcepub fn load_and_split_dataset(
path: &str,
train_percent: f64,
) -> Result<((NDArray, NDArray), (NDArray, NDArray))>
pub fn load_and_split_dataset( path: &str, train_percent: f64, ) -> Result<((NDArray, NDArray), (NDArray, NDArray))>
Loads a dataset from .nab files and splits it into training and testing sets
§Arguments
path
- Base path for the .nab files (e.g., “mnist”)train_percent
- Percentage of data to use for training (e.g., 80 for 80%)
§Returns
A tuple containing ((train_images, train_labels), (test_images, test_labels))
Sourcepub fn csv_to_nab(
csv_path: &str,
output_path: &str,
shape: Vec<usize>,
skip_first_column: bool,
) -> Result<()>
pub fn csv_to_nab( csv_path: &str, output_path: &str, shape: Vec<usize>, skip_first_column: bool, ) -> Result<()>
Converts CSV data to .nab format
§Arguments
csv_path
- Path to the CSV fileoutput_path
- Path where to save the .nab fileshape
- Shape of the resulting array (e.g., [60000, 28, 28] for MNIST images)
Source§impl NDArray
impl NDArray
pub fn new(data: Vec<f64>, shape: Vec<usize>) -> Self
pub fn from_vec(data: Vec<f64>) -> Self
pub fn from_matrix(data: Vec<Vec<f64>>) -> Self
pub fn shape(&self) -> &[usize]
pub fn ndim(&self) -> usize
Sourcepub fn extract_sample(&self, sample_index: usize) -> Self
pub fn extract_sample(&self, sample_index: usize) -> Self
Sourcepub fn pretty_print(&self, precision: usize)
pub fn pretty_print(&self, precision: usize)
Pretty prints an N-dimensional array
§Arguments
precision
- The number of decimal places to round each value to.
Sourcepub fn linspace(start: f64, end: f64, num: usize, precision: usize) -> Self
pub fn linspace(start: f64, end: f64, num: usize, precision: usize) -> Self
Creates a 1D array with evenly spaced numbers over a specified interval
§Arguments
start
- The starting value of the interval.end
- The ending value of the interval.num
- The number of evenly spaced samples to generate.precision
- The number of decimal places to round each value to.
§Returns
A 1D NDArray containing the evenly spaced numbers.
Sourcepub fn sub_matrix(
&self,
row_start: usize,
row_end: usize,
col_start: usize,
col_end: usize,
) -> Self
pub fn sub_matrix( &self, row_start: usize, row_end: usize, col_start: usize, col_end: usize, ) -> Self
Returns a sub-matrix from a 2D array
§Arguments
row_start
- The starting row index of the sub-matrix.row_end
- The ending row index of the sub-matrix (exclusive).col_start
- The starting column index of the sub-matrix.col_end
- The ending column index of the sub-matrix (exclusive).
§Returns
A new NDArray representing the specified sub-matrix.
Sourcepub fn set(&mut self, index: usize, value: f64)
pub fn set(&mut self, index: usize, value: f64)
Sets a specific element in the array
§Arguments
index
- The index of the element to set.value
- The value to set the element to.
Sourcepub fn set_range(&mut self, start: usize, end: usize, value: f64)
pub fn set_range(&mut self, start: usize, end: usize, value: f64)
Sets a range of elements in the array to a specific value
§Arguments
start
- The starting index of the range.end
- The ending index of the range (exclusive).value
- The value to set the elements to.
Sourcepub fn set_2d(&mut self, row: usize, col: usize, value: f64)
pub fn set_2d(&mut self, row: usize, col: usize, value: f64)
Sets a specific element in a 2D array
§Arguments
row
- The row index of the element.col
- The column index of the element.value
- The value to set the element to.
Sourcepub fn expand_dims(&self, axis: usize) -> Self
pub fn expand_dims(&self, axis: usize) -> Self
Sourcepub fn greater_than(&self, threshold: f64) -> Vec<bool>
pub fn greater_than(&self, threshold: f64) -> Vec<bool>
Sourcepub fn dtype(&self) -> &'static str
pub fn dtype(&self) -> &'static str
Returns the data type of the elements in the array
§Returns
A string representing the data type of the elements.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the total number of elements in the array
§Returns
The total number of elements in the array.
Sourcepub fn one_hot_encode(labels: &NDArray) -> Self
pub fn one_hot_encode(labels: &NDArray) -> Self
Source§impl NDArray
impl NDArray
Source§impl NDArray
impl NDArray
Sourcepub fn tanh(&self) -> Self
pub fn tanh(&self) -> Self
Calculates the hyperbolic tangent of each element in the array
§Returns
A new NDArray with the hyperbolic tangent of each element.
Sourcepub fn relu(&self) -> Self
pub fn relu(&self) -> Self
Applies the ReLU function to each element in the array
§Returns
A new NDArray with the ReLU function applied to each element.
Sourcepub fn leaky_relu(&self, alpha: f64) -> Self
pub fn leaky_relu(&self, alpha: f64) -> Self
Source§impl NDArray
impl NDArray
Sourcepub fn sqrt(&self) -> Self
pub fn sqrt(&self) -> Self
Calculates the square root of each element in the array
§Returns
A new NDArray with the square root of each element.
Sourcepub fn exp(&self) -> Self
pub fn exp(&self) -> Self
Calculates the exponential (e^x) of each element in the array
§Returns
A new NDArray with the exponential of each element.
Sourcepub fn sin(&self) -> Self
pub fn sin(&self) -> Self
Calculates the sine of each element in the array
§Returns
A new NDArray with the sine of each element.
Source§impl NDArray
impl NDArray
Sourcepub fn linear_regression(
X: &NDArray,
y: &NDArray,
alpha: f64,
epochs: usize,
) -> (Vec<f64>, Vec<f64>)
pub fn linear_regression( X: &NDArray, y: &NDArray, alpha: f64, epochs: usize, ) -> (Vec<f64>, Vec<f64>)
Performs linear regression using gradient descent with multiple features
§Arguments
X
- The input feature matrix as an NDArray.y
- The output target as an NDArray.alpha
- The learning rate.epochs
- The number of iterations for gradient descent.
§Returns
A tuple containing the optimized parameters and the history of MSE for each epoch.
Source§impl NDArray
Represents a dataset split into training and testing sets
impl NDArray
Represents a dataset split into training and testing sets
Sourcepub fn mnist_csv_to_nab(
csv_path: &str,
images_path: &str,
labels_path: &str,
image_shape: Vec<usize>,
) -> Result<()>
pub fn mnist_csv_to_nab( csv_path: &str, images_path: &str, labels_path: &str, image_shape: Vec<usize>, ) -> Result<()>
Converts MNIST CSV data to image and label .nab files
§Arguments
csv_path
- Path to the CSV fileimages_path
- Path where to save the images .nab filelabels_path
- Path where to save the labels .nab fileimage_shape
- Shape of a single image (e.g., [28, 28])