Nabla-ML
Nabla-ML is a Rust library inspired by NumPy, providing a multi-dimensional array implementation with various mathematical and array manipulation functionalities.
Features
- Array Creation: Create 1D and 2D arrays using vectors and matrices.
- Random Arrays: Generate arrays with random numbers, including uniform and normal distributions.
- Arithmetic Operations: Perform element-wise addition, subtraction, multiplication, and division.
- Mathematical Functions: Apply functions like square root, exponential, sine, cosine, logarithm, hyperbolic tangent, ReLU, Leaky ReLU, and Sigmoid to arrays.
- Array Reshaping: Change the shape of arrays while maintaining data integrity.
- Indexing and Slicing: Access and modify elements using indices and slices.
- Conditional Selection: Filter arrays based on conditions.
- Array Attributes: Retrieve attributes like number of dimensions, shape, size, and data type.
- Axis Manipulation: Add new axes to arrays to increase dimensionality.
- File I/O: Save and load arrays using a custom
.nabfile format with compression. - Loss Functions: Calculate Mean Squared Error (MSE) and Cross-Entropy Loss.
- Linear Regression: Perform linear regression using gradient descent with Mean Squared Error as the loss function.
Usage
Creating Arrays
use NDArray;
// Create a 1D array
let arr = from_vec;
// Create a 2D array
let mat = from_matrix;
Random Arrays
// Create a 1D array of random numbers between 0 and 1
let rand_arr = rand;
// Create a 2D array of random integers between 1 and 10
let rand_int_mat = randint_2d;
Arithmetic Operations
let arr1 = from_vec;
let arr2 = from_vec;
// Element-wise addition
let sum = arr1.clone + arr2;
// Scalar multiplication
let scaled = arr1.clone * 2.0;
Mathematical Functions
let arr = from_vec;
// Calculate square roots
let sqrt_arr = arr.sqrt;
// Calculate exponentials
let exp_arr = arr.exp;
// Calculate hyperbolic tangent
let tanh_arr = arr.tanh;
// Apply ReLU
let relu_arr = arr.relu;
// Apply Leaky ReLU with alpha = 0.01
let leaky_relu_arr = arr.leaky_relu;
// Apply Sigmoid
let sigmoid_arr = arr.sigmoid;
Reshaping Arrays
let arr = from_vec;
let reshaped = arr.reshape;
Indexing and Slicing
let arr = from_vec;
// Access an element
let first = arr.get;
// Slice the array
let slice = arr.slice;
Conditional Selection
let arr = from_vec;
// Filter elements greater than 0.7
let filtered = arr.filter;
Array Attributes
let arr = from_matrix;
// Get number of dimensions
let ndim = arr.ndim;
// Get shape
let shape = arr.shape;
// Get size
let size = arr.size;
// Get data type
let dtype = arr.dtype;
Axis Manipulation
let arr = from_vec;
// Add a new axis
let row_vector = arr.new_axis;
let col_vector = arr.new_axis;
File I/O with .nab Format
Save and Load a Single Array
use ;
// Create an NDArray
let array = from_vec;
// Save the array to a .nab file
save_nab.expect;
// Load the array from the .nab file
let loaded_array = load_nab.expect;
// Verify the loaded data
assert_eq!;
assert_eq!;
Save Multiple Arrays
use ;
Load Multiple Arrays
use ;
use HashMap;
Loss Functions
Mean Squared Error (MSE)
The MSE measures the average of the squares of the differences between predicted and actual values.
use NDArray;
let y_true = from_vec;
let y_pred = from_vec;
let mse = mean_squared_error;
println!;
Cross-Entropy Loss
Cross-Entropy Loss is commonly used for classification problems, especially with softmax outputs.
use NDArray;
let y_true = from_matrix;
let y_pred = from_matrix;
let cross_entropy = cross_entropy_loss;
println!;
Linear Regression
Perform linear regression using gradient descent with Mean Squared Error as the loss function.
use NDArray;
// Generate a simple dataset
let X = from_vec;
let y = from_vec;
// Apply linear regression
let = linear_regression;
println!;
println!;
// Predict on new data
let X_new = from_vec;
let y_pred: = X_new.data.iter.map.collect;
println!;
println!;

License
This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.