pub struct NDArray {
pub data: Vec<f64>,
pub shape: Vec<usize>,
}
Fields§
§data: Vec<f64>
§shape: Vec<usize>
Implementations§
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 argmax(&self, axis: Option<usize>) -> Vec<usize>
pub fn argmax(&self, axis: Option<usize>) -> Vec<usize>
Returns the index of the maximum value in the array
§Returns
The index of the maximum value. Returns the indices of maximum values For 1D arrays: returns a single index For 2D arrays: returns indices along the specified axis
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
Sourcepub fn scalar_sub(&self, scalar: f64) -> Self
pub fn scalar_sub(&self, scalar: f64) -> Self
Sourcepub fn multiply_scalar(&self, scalar: f64) -> Self
pub fn multiply_scalar(&self, scalar: f64) -> Self
Sourcepub fn divide_scalar(&self, scalar: f64) -> Self
pub fn divide_scalar(&self, scalar: f64) -> Self
Sourcepub fn add_scalar(&self, scalar: f64) -> Self
pub fn add_scalar(&self, scalar: f64) -> Self
Sourcepub fn log(&self) -> Self
pub fn log(&self) -> Self
Calculates the natural logarithm of each element in the array
§Returns
A new NDArray with the natural logarithm of each element.
pub fn pad_to_size(&self, target_size: usize) -> Self
Sourcepub fn layer_normalize(&self) -> Self
pub fn layer_normalize(&self) -> Self
Add layer normalization
Sourcepub fn batch_normalize(&self) -> Self
pub fn batch_normalize(&self) -> Self
Add batch normalization
pub fn add(self, other: &NDArray) -> Self
Sourcepub fn min_axis(&self, axis: usize) -> Result<Self, &'static str>
pub fn min_axis(&self, axis: usize) -> Result<Self, &'static str>
Returns the minimum value along the specified axis
Sourcepub fn concatenate(
&self,
other: &Self,
axis: usize,
) -> Result<Self, &'static str>
pub fn concatenate( &self, other: &Self, axis: usize, ) -> Result<Self, &'static str>
Concatenates two arrays along the specified axis
pub fn map<F>(&self, f: F) -> Self
Sourcepub fn unique(&self) -> Self
pub fn unique(&self) -> Self
Returns unique elements of the array
§Returns
A new NDArray containing unique elements in sorted order
Sourcepub fn where_cond<F>(&self, condition: F, x: f64, y: f64) -> Self
pub fn where_cond<F>(&self, condition: F, x: f64, y: f64) -> Self
Sourcepub fn rand_uniform(shape: &[usize]) -> Self
pub fn rand_uniform(shape: &[usize]) -> Self
Sourcepub fn mean_axis(&self, axis: usize) -> Self
pub fn mean_axis(&self, axis: usize) -> Self
Calculates the mean along the specified axis
§Arguments
axis
- Axis along which to calculate mean (0 for columns, 1 for rows)
Sourcepub fn var_axis(&self, axis: usize) -> Self
pub fn var_axis(&self, axis: usize) -> Self
Calculates the variance along the specified axis
§Arguments
axis
- Axis along which to calculate variance (0 for columns, 1 for rows)
Sourcepub fn to_categorical(&self, num_classes: Option<usize>) -> Self
pub fn to_categorical(&self, num_classes: Option<usize>) -> Self
Converts a class vector (integers) to binary class matrix (one-hot encoding)
§Arguments
num_classes
- Optional number of classes. If None, it will be inferred from the data
§Returns
A 2D NDArray where each row is a one-hot encoded vector
§Example
use nabla_ml::nab_array::NDArray;
let labels = NDArray::from_vec(vec![0.0, 1.0, 2.0]);
let categorical = labels.to_categorical(None);
assert_eq!(categorical.shape(), &[3, 3]);
assert_eq!(categorical.data(), &[1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0]);
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.
Trait Implementations§
Source§impl<'a, 'b> Add<&'b NDArray> for &'a NDArray
Implements element-wise addition between two NDArray references
impl<'a, 'b> Add<&'b NDArray> for &'a NDArray
Implements element-wise addition between two NDArray references
Source§impl<'a, 'b> Div<&'b NDArray> for &'a NDArray
Implements element-wise division between two NDArray references
impl<'a, 'b> Div<&'b NDArray> for &'a NDArray
Implements element-wise division between two NDArray references
Source§impl<'a, 'b> Mul<&'b NDArray> for &'a NDArray
Implements element-wise multiplication between two NDArray references
impl<'a, 'b> Mul<&'b NDArray> for &'a NDArray
Implements element-wise multiplication between two NDArray references
Source§impl<'a, 'b> Sub<&'b NDArray> for &'a NDArray
Implements element-wise subtraction between two NDArray references
impl<'a, 'b> Sub<&'b NDArray> for &'a NDArray
Implements element-wise subtraction between two NDArray references