### Install NumPy and RustyNum Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/replacing-numpy-for-faster-analytics.md Install both NumPy and RustyNum using pip. This is a prerequisite for running the code examples. ```bash pip install numpy rustynum ``` -------------------------------- ### Install RustyNum using pip Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/installation.md Use this command to install RustyNum from PyPI. This is the recommended method for most users. ```bash pip install rustynum ``` -------------------------------- ### Install Python Bindings with Setup.py Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Install the Python bindings for RustyNum using the provided setup.py script. This method avoids using Maturin. ```python cd bindings/python/ && python setup.py install ``` -------------------------------- ### Install RustyNum and NumPy Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/benchmarks/rustynum-vs-numpy.md Installs the necessary packages for benchmarking RustyNum and NumPy. Ensure you are using a compatible Python version. ```bash python -V pip install -U rustynum numpy ``` -------------------------------- ### Install RustyNum and Dependencies Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md Install RustyNum along with NumPy and scikit-learn for data manipulation and model integration. ```bash pip install rustynum pip install numpy scikit-learn ``` -------------------------------- ### NumPy Matrix-Vector Multiplication Setup Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Initializes a NumPy matrix and vector for performance comparison. This setup is used to measure NumPy's execution time. ```python import numpy as np import rustynum as rnp import time # NumPy multiplication matrix_np = np.arange(16, dtype=np.float32).reshape((4, 4)) vector_np = np.array([1, 2, 3, 4], dtype=np.float32) start_np = time.time() result_np = np.dot(matrix_np, vector_np) end_np = time.time() numpy_time = end_np - start_np ``` -------------------------------- ### Install RustyNum using Poetry Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/installation.md Add RustyNum to your project if you are using Poetry as your package manager. ```bash poetry add rustynum ``` -------------------------------- ### Verify RustyNum Installation Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/installation.md Run this Python code to confirm that RustyNum has been installed correctly. It creates a 2x3 matrix of zeros. ```python import rustynum as rnp a = rnp.zeros([2, 3]) print(a) ``` -------------------------------- ### Install RustyNum using Conda and pip Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/installation.md Install RustyNum within a conda environment. First, activate your environment, then use pip for installation. ```bash conda activate your-environment pip install rustynum ``` -------------------------------- ### Install RustyNum using Rye Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/installation.md Add RustyNum to your project if you are using Rye as your package manager. ```bash rye add rustynum ``` -------------------------------- ### Initialize and Print a RustyNum Array Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md This snippet shows how to import RustyNum and create a basic NumArray. Ensure RustyNum is installed before running. ```python import rustynum as rnp sample = rnp.NumArray([1.0, 2.0, 3.0], dtype="float32") print("Sample array:", sample) ``` -------------------------------- ### Run Python Benchmarks Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Execute the benchmark suite for the Python bindings. This command assumes pytest is installed. ```python pytest benchmarks ``` -------------------------------- ### Run Python Tests Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Execute the test suite for the Python bindings. This command assumes pytest is installed. ```python pytest tests ``` -------------------------------- ### Create Array with Linspace Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a NumArray with a specified number of evenly spaced values between a start and stop point, similar to NumPy's np.linspace. ```Python rnp.linspace(start, stop, num) ``` -------------------------------- ### rustynum.linspace Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Creates an array with a specified number of evenly spaced values between a start and end point. ```APIDOC ## rustynum.linspace ### Description Creates an array with a specified number of evenly spaced values between a start and end point. ### Function rustynum.linspace ``` -------------------------------- ### Compare NumPy and RustyNum Mean Calculation Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/replacing-numpy-for-faster-analytics.md Compares the performance of calculating the mean of a large array using NumPy versus RustyNum. Demonstrates setup with random data and prints timing results and speedup. ```python import numpy as np import rustynum as rnp import time # Create test data data_np = np.random.rand(1_000_000).astype(np.float32) data_rn = rnp.NumArray(data_np.tolist(), dtype="float32") # NumPy timing start_np = time.time() mean_np = np.mean(data_np) end_np = time.time() numpy_duration = end_np - start_np # RustyNum timing start_rn = time.time() mean_rn = data_rn.mean().item() end_rn = time.time() rustynum_duration = end_rn - start_rn # Print results print("Results Comparison:") print("-" * 40) print(f"NumPy mean: {mean_np:.8f}") print(f"RustyNum mean: {mean_rn:.8f}") print("\nPerformance:") print("-" * 40) print(f"NumPy time: {numpy_duration:.6f} seconds") print(f"RustyNum time: {rustynum_duration:.6f} seconds") print(f"Speedup: {numpy_duration/rustynum_duration:.2f}x") ``` -------------------------------- ### Find Minimum Value in NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Get the minimum value from a NumArray, optionally along a specified axis. ```Python minimum = a.min() ``` -------------------------------- ### Find Maximum Value in NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Get the maximum value from a NumArray, optionally along a specified axis. ```Python maximum = a.max() ``` -------------------------------- ### Build Python Wheel with Setup.py Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a Python wheel distribution for the RustyNum bindings using setup.py. This is useful for packaging and distribution. ```python cd bindings/python/ && python setup.py bdist_wheel ``` -------------------------------- ### NumArray Initialization Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Demonstrates how to initialize a NumArray object from various sources, including Python lists and other NumArray instances. ```APIDOC ## NumArray Initialization ### Description Initializes a `NumArray` object. ### Usage ```python from rustynum import NumArray # From a list a = NumArray([1.0, 2.0, 3.0], dtype="float32") # From another NumArray b = NumArray(a) # From nested lists (2D array) c = NumArray([[1.0, 2.0], [3.0, 4.0]], dtype="float64") ``` ``` -------------------------------- ### Execute Benchmark Script Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/benchmarks/rustynum-vs-numpy.md Runs the benchmark script to collect performance data. Ensure the script is saved as 'benchmarks.py' or adjust the command accordingly. ```bash python benchmarks.py ``` -------------------------------- ### Create Matrices for Matrix-Matrix Multiplication Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Initializes two 2x2 matrices using RustyNum for demonstrating matrix-matrix multiplication. ```python import rustynum as rnp # Create two 2D NumArrays dataA = [1.0, 2.0, 3.0, 4.0] dataB = [5.0, 6.0, 7.0, 8.0] A = rnp.NumArray(dataA, dtype="float32").reshape([2, 2]) B = rnp.NumArray(dataB, dtype="float32").reshape([2, 2]) print("Matrix A:\n", A) print("Matrix B:\n", B) ``` -------------------------------- ### Record Environment Details Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/benchmarks/rustynum-vs-numpy.md Prints the versions of Python, NumPy, RustyNum, and system information like OS and CPU. This is useful for ensuring reproducible benchmark results. ```python import numpy as np, platform, sys, rustynum as rnp print("Python", sys.version.split()[0]) print("NumPy", np.__version__) print("OS", platform.platform()) print("CPU", platform.processor()) print("RustyNum", rnp.__version__) ``` -------------------------------- ### Create Matrix and Vector for Multiplication Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Demonstrates creating a 4x4 matrix and a 4-element vector using RustyNum. The reshape method is used to form the matrix. ```python import rustynum as rnp # Create a 4x4 matrix matrix_data = [i for i in range(16)] # 0 to 15 matrix = rnp.NumArray(matrix_data, dtype="float32").reshape([4, 4]) # Create a 4-element vector vector_data = [1, 2, 3, 4] vector = rnp.NumArray(vector_data, dtype="float32") print("Matrix:\n", matrix) print("Vector:\n", vector) ``` -------------------------------- ### Basic Array Operations in RustyNum vs Numpy Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Demonstrates basic array creation and element-wise addition, comparing RustyNum with NumPy. RustyNum provides a similar interface to NumPy. ```Python import numpy as np import rustynum as rnp # Using Numpy a = np.array([1.0, 2.0, 3.0, 4.0], dtype="float32") a = a + 2 print(a.mean()) # 4.5 # Using RustyNum b = rnp.NumArray([1.0, 2.0, 3.0, 4.0], dtype="float32") b = b + 2 print(b.mean().item()) # 4.5 ``` -------------------------------- ### Generate Rust Documentation Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Build and open the documentation generated from Rust source code comments. This requires the 'cargo doc' command. ```rust cargo doc --open ``` -------------------------------- ### Run Cargo Tests Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Execute all tests for the Rust core library. Ensure your project is set up with Cargo. ```rust cargo test ``` -------------------------------- ### Initialize NumArray from another NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a new NumArray by copying an existing one. ```Python from rustynum import NumArray # From another NumArray b = NumArray(a) ``` -------------------------------- ### Create Various RustyNum Arrays Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Demonstrates creating arrays of zeros, arrays with evenly spaced values, and arrays with values within a specified interval using RustyNum. ```python import rustynum as rnp # Create an array of zeros zeros_array = rnp.zeros([3, 3]) # Create an array with evenly spaced values arange_array = rnp.arange(0, 10, 2) # Create an array with evenly spaced values over a specified interval linspace_array = rnp.linspace(0, 1, 5) print(zeros_array) print(arange_array) print(linspace_array) ``` -------------------------------- ### Benchmark Runner Script Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/benchmarks/rustynum-vs-numpy.md A Python script to benchmark RustyNum and NumPy for mean, min, matrix-vector dot product, and matrix multiplication. It includes a timing utility and helper functions for converting NumPy arrays to RustyNum arrays. ```python import time, math, numpy as np import rustynum as rnp from statistics import median def bench(fn, repeats=7, warmup=2): for _ in range(warmup): fn() times = [] for _ in range(repeats): t0 = time.perf_counter() fn() times.append(time.perf_counter() - t0) return median(times) def as_rn(x): if x.dtype == np.float32: return rnp.NumArray(x.flatten().tolist(), dtype="float32").reshape(list(x.shape)) elif x.dtype == np.float64: return rnp.NumArray(x.flatten().tolist(), dtype="float64").reshape(list(x.shape)) else: raise ValueError("Use float32 or float64") def run_suite(n=1_000_000, m=1000): results = [] # 1) mean over vector vec32 = np.random.rand(n).astype(np.float32) vec32_rn = as_rn(vec32) t_np = bench(lambda: float(np.mean(vec32))) t_rn = bench(lambda: float(vec32_rn.mean().item())) results.append(("mean", f"{n}", t_rn, t_np, t_np / t_rn if t_rn > 0 else math.nan)) # 2) minimum over vector t_np = bench(lambda: float(np.min(vec32))) t_rn = bench(lambda: float(vec32_rn.min())) results.append(("min", f"{n}", t_rn, t_np, t_np / t_rn if t_rn > 0 else math.nan)) # 3) matrix vector dot mat = np.random.rand(m, m).astype(np.float32) vec = np.random.rand(m).astype(np.float32) mat_rn = as_rn(mat) vec_rn = as_rn(vec) t_np = bench(lambda: np.dot(mat, vec)) t_rn = bench(lambda: mat_rn.dot(vec_rn)) results.append(("matrix@vector", f"{m}x{m} ยท {m}", t_rn, t_np, t_np / t_rn if t_rn > 0 else math.nan)) # 4) matrix matrix a = np.random.rand(m, m).astype(np.float32) b = np.random.rand(m, m).astype(np.float32) a_rn = as_rn(a) b_rn = as_rn(b) t_np = bench(lambda: a @ b) t_rn = bench(lambda: a_rn @ b_rn) results.append(("matrix@matrix", f"{m}x{m}", t_rn, t_np, t_np / t_rn if t_rn > 0 else math.nan)) print("\nOperation, Size, RustyNum s, NumPy s, Speedup NumPy/RustyNum") for op, size, trn, tnp, sp in results: print(f"{op}, {size}, {trn:.6f}, {tnp:.6f}, {sp:.2f}x") if __name__ == "__main__": run_suite(n=1_000_000, m=1000) ``` -------------------------------- ### Run Cargo Benchmarks Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Execute specific benchmarks within the Rust core library. Replace '' with the target benchmark. ```rust cargo bench -- ``` -------------------------------- ### Combine Feature Sets in RustyNum Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md Demonstrates creating and concatenating feature sets using RustyNum for ML preprocessing. Ensure feature sets have compatible dimensions for concatenation. ```python features1 = normalized_data_rn[:, :2] # First two columns (shape: [2, 2]) features2 = rnp.ones([2, 2]) # Match features1 shape: [2, 2] # Combine them combined = rnp.concatenate([features1, features2], axis=1) print("Combined feature shape:", combined.shape) # Expected: [2, 4] ``` -------------------------------- ### Matrix-Vector Dot Product with RustyNum and Numpy Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compares matrix-vector dot product calculations between RustyNum and NumPy. Both libraries are used to demonstrate equivalent operations. ```Python # Matrix-vector dot product using Numpy import numpy as np import rustynum as rnp a = np.random.rand(4 * 4).astype(np.float32) b = np.random.rand(4).astype(np.float32) result_numpy = np.dot(a.reshape((4, 4)), b) # Matrix-vector dot product using RustyNum a_rnp = rnp.NumArray(a.tolist()) b_rnp = rnp.NumArray(b.tolist()) result_rust = a_rnp.reshape([4, 4]).dot(b_rnp).tolist() print(result_numpy) # Example Output: [0.8383043, 1.678406, 1.4153088, 0.7959367] print(result_rust) # Example Output: [0.8383043, 1.678406, 1.4153088, 0.7959367] ``` -------------------------------- ### Compare NumPy and RustyNum Dot Product Calculation Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/replacing-numpy-for-faster-analytics.md Compares the performance of calculating the dot product between a matrix and a vector using NumPy versus RustyNum. Sets up large matrices and vectors for testing. ```python import numpy as np import rustynum as rnp import time # Create test data matrix_np = np.random.rand(1000, 1000).astype(np.float32) vector_np = np.random.rand(1000).astype(np.float32) matrix_rn = rnp.NumArray(matrix_np.flatten().tolist(), dtype="float32").reshape([1000, 1000]) vector_rn = rnp.NumArray(vector_np.tolist(), dtype="float32") # NumPy timing start_np = time.time() dot_np = np.dot(matrix_np, vector_np) end_np = time.time() numpy_duration = end_np - start_np # RustyNum timing start_rn = time.time() dot_rn = matrix_rn.dot(vector_rn) end_rn = time.time() rustynum_duration = end_rn - start_rn ``` -------------------------------- ### Create RustyNum Array Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Equivalent to NumPy's array creation but using RustyNum's NumArray. Use this for performance-critical numerical tasks where RustyNum's acceleration is beneficial. ```python import rustynum as rnp a = rnp.NumArray([1.0, 2.0, 3.0], dtype="float32") ``` -------------------------------- ### Load and Inspect Data with RustyNum Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md Generate random data using NumPy, convert it to RustyNum's NumArray, and verify its shape. This sets up the data for subsequent preprocessing steps. ```python import numpy as np import rustynum as rnp import math from sklearn.linear_model import LogisticRegression # Generate random data with shape (100, 3) data_np = np.random.rand(100, 3).astype(np.float32) # Convert to RustyNum's NumArray data_rn = rnp.NumArray(data_np.flatten().tolist(), dtype="float32").reshape([100, 3]) print("RustyNum data shape:", data_rn.shape) ``` -------------------------------- ### Matrix Multiplication with RustyNum and NumPy Comparison Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Compares the performance of matrix multiplication between RustyNum and NumPy. Ensure NumPy is imported as 'rnp' and the matrices/vectors are pre-defined. ```python matrix_rn = rnp.NumArray(matrix_np.flatten().tolist(), dtype="float32").reshape([4, 4]) vector_rn = rnp.NumArray(vector_np.tolist(), dtype="float32") start_rn = time.time() result_rn = matrix_rn.dot(vector_rn) end_rn = time.time() rustynum_time = end_rn - start_rn print("NumPy result:", result_np) print("RustyNum result:", result_rn) print(f"NumPy time: {numpy_time:.6f} seconds") print(f"RustyNum time: {rustynum_time:.6f} seconds") ``` -------------------------------- ### Print NumPy and RustyNum Results Comparison Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/replacing-numpy-for-faster-analytics.md This snippet prints a comparison of the computed dot product results between NumPy and RustyNum, along with their respective execution times and the calculated speedup. It's useful for verifying the accuracy and performance of RustyNum against NumPy. ```python print("Results Comparison:") print("-" * 40) print(f"NumPy dot[0]: {dot_np[0]:.8f}") print(f"RustyNum dot[0]: {dot_rn[0]:.8f}") print("\nPerformance:") print("-" * 40) print(f"NumPy time: {numpy_duration:.6f} seconds") print(f"RustyNum time: {rustynum_duration:.6f} seconds") print(f"Speedup: {numpy_duration/rustynum_duration:.2f}x") ``` -------------------------------- ### Compare NumPy and RustyNum Minimum Value Calculation Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/replacing-numpy-for-faster-analytics.md Compares the performance of finding the minimum value in a large array using NumPy versus RustyNum. Includes data generation, timing, and result output. ```python import numpy as np import rustynum as rnp import time # Create test data data_np = np.random.rand(100_000).astype(np.float32) data_rn = rnp.NumArray(data_np.tolist(), dtype="float32") # NumPy timing start_np = time.time() min_np = np.min(data_np) end_np = time.time() numpy_duration = end_np - start_np # RustyNum timing start_rn = time.time() min_rn = data_rn.min() end_rn = time.time() rustynum_duration = end_rn - start_rn # Print results print("Results Comparison:") print("-" * 40) print(f"NumPy min: {min_np:.8f}") print(f"RustyNum min: {min_rn:.8f}") print("\nPerformance:") print("-" * 40) print(f"NumPy time: {numpy_duration:.6f} seconds") print(f"RustyNum time: {rustynum_duration:.6f} seconds") print(f"Speedup: {numpy_duration/rustynum_duration:.2f}x") ``` -------------------------------- ### Initialize NumArray from a list Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a NumArray from a Python list, specifying the data type. ```Python from rustynum import NumArray # From a list a = NumArray([1.0, 2.0, 3.0], dtype="float32") ``` -------------------------------- ### Element-wise Operations with RustyNum Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md Illustrates applying element-wise arithmetic and math functions like sigmoid, exp, and log directly on RustyNum NumArray objects. Ensure data is in a compatible numeric format. ```python # Element-wise operations example data = rnp.NumArray([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype="float32") print("Original data:", data.tolist()) # Apply the sigmoid function sigmoid_data = data.sigmoid() print("After applying sigmoid:", sigmoid_data.tolist()) # Compute the exponential and logarithm of each element exp_data = data.exp() log_data = data.log() print("Exponential:", exp_data.tolist()) print("Logarithm:", log_data.tolist()) ``` -------------------------------- ### RustyNum Matrix Dot Product Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Demonstrates computing the dot product between a 2D NumArray (matrix) and a 1D NumArray (vector) using RustyNum. This is a fundamental linear algebra operation. ```python # Create a 2D NumArray matrix = rnp.NumArray([[1.0, 2.0], [3.0, 4.0]], dtype="float32") # Compute the dot product vector = rnp.NumArray([1.0, 2.0], dtype="float32") dot_product = matrix.dot(vector) print(dot_product) ``` -------------------------------- ### RustyNum Element-Wise Operations Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Shows how to perform element-wise addition and multiplication on RustyNum arrays. These operations are optimized for performance. ```python # Perform element-wise addition result = arange_array + 2 # Perform element-wise multiplication result = arange_array * 2 ``` -------------------------------- ### Perform Matrix-Matrix Multiplication with .dot() Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Computes the product of two matrices using RustyNum's .dot() method. Ensure inner dimensions match for valid multiplication. ```python result_matrix = A.dot(B) print("Matrix-Matrix Multiplication Result:\n", result_matrix) ``` -------------------------------- ### Compute Mean of a RustyNum Array Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Demonstrates creating a NumArray, adding a scalar, and computing its mean. Use this for basic array manipulation and statistical calculations. ```python import rustynum as rnp # Create a NumArray a = rnp.NumArray([1.0, 2.0, 3.0, 4.0], dtype="float32") # Add a scalar a = a + 2 # Compute the mean mean_value = a.mean().item() print(mean_value) # Output: 4.5 ``` -------------------------------- ### Perform Matrix-Vector Multiplication with .dot() Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Calculates the dot product between a matrix and a vector using RustyNum's .dot() method. This method adapts to the input shapes. ```python result_vec = matrix.dot(vector) print("Matrix-Vector Multiplication Result:\n", result_vec) ``` -------------------------------- ### Create Ones Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a NumArray filled with ones, similar to NumPy's np.ones. ```Python rnp.ones((2, 3)) ``` -------------------------------- ### Create Zeros Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a NumArray filled with zeros, similar to NumPy's np.zeros. ```Python rnp.zeros((2, 3)) ``` -------------------------------- ### rustynum.ones Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Creates a new array filled with ones. ```APIDOC ## rustynum.ones ### Description Creates a new array filled with ones. ### Function rustynum.ones ``` -------------------------------- ### Initialize NumArray from nested lists (2D) Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a 2D NumArray from nested Python lists, specifying the data type. ```Python from rustynum import NumArray # From nested lists (2D array) c = NumArray([[1.0, 2.0], [3.0, 4.0]], dtype="float64") ``` -------------------------------- ### Convert RustyNum to NumPy for Scikit-learn Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md Shows how to convert RustyNum arrays back to NumPy arrays for compatibility with libraries like scikit-learn. This is useful after data normalization or scaling. ```python # Convert our preprocessed data back to NumPy X_train = np.array(normalized_data_rn.tolist(), dtype=np.float32) # Create dummy labels y_train = np.random.randint(0, 2, size=(100,)) # Train a simple model model = LogisticRegression() model.fit(X_train, y_train) print("Model coefficients:", model.coef_) ``` -------------------------------- ### NumArray.tolist Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Converts the NumArray to a Python list. ```APIDOC ## NumArray.tolist ### Description Converts the `NumArray` to a Python list. ### Method Signature `tolist() -> Union[List[float], List[List[float]]]` ### Usage ```python # Assuming 'a' is a NumArray instance list_representation = a.tolist() ``` ``` -------------------------------- ### rustynum.zeros Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Creates a new array filled with zeros. ```APIDOC ## rustynum.zeros ### Description Creates a new array filled with zeros. ### Function rustynum.zeros ``` -------------------------------- ### Create NumPy Array Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Standard method for creating a NumPy array. Use this when working with existing NumPy code or libraries that require NumPy arrays. ```python import numpy as np a = np.array([1.0, 2.0, 3.0], dtype="float32") ``` -------------------------------- ### rustynum.min Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Finds the minimum value in an array. ```APIDOC ## rustynum.min ### Description Finds the minimum value in an array. ### Function rustynum.min ``` -------------------------------- ### Min-Max Scaling Function with RustyNum Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md A Python function to perform min-max scaling on input data using RustyNum. It computes column-wise min and max values and scales each feature to a [0, 1] range. ```python # Create a reusable scaling function def min_max_scale(array): # Step 1: Compute column-wise min and max col_mins = [] col_maxes = [] for col_idx in range(array.shape[1]): col_data = array[:, col_idx] col_mins.append(col_data.min()) col_maxes.append(col_data.max()) # Step 2 & 3: Scale each column scaled_data = [] for col_idx in range(array.shape[1]): col_data = array[:, col_idx] numerator = col_data - col_mins[col_idx] denominator = col_maxes[col_idx] - col_mins[col_idx] or 1.0 scaled_col = numerator / denominator scaled_data.append(scaled_col.tolist()) # Concatenate scaled columns return rnp.concatenate( [rnp.NumArray(col, dtype="float32").reshape([array.shape[0], 1]) for col in scaled_data], axis=1 ) # Scale our data scaled_data_rn = min_max_scale(data_rn) print("Scaled data shape:", scaled_data_rn.shape) print("First row after scaling:", scaled_data_rn[0, :].tolist()) ``` -------------------------------- ### Convert NumArray to Python List Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Convert a NumArray into a standard Python list representation. ```Python list_representation = a.tolist() ``` -------------------------------- ### Fancy Flipping Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Reverse the order of elements in a NumArray using slicing, similar to NumPy's [::-1]. ```Python rnp.array([1,2,3])[::-1] ``` -------------------------------- ### Concatenate Features with RustyNum Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md Demonstrates concatenating multiple feature sets into a single RustyNum array. This is useful when combining data from different sources, like images and text. ```python # Example usage for concatenation would go here, assuming two arrays `arr1` and `arr2` # For instance: # arr1 = rnp.NumArray([[1, 2], [3, 4]], dtype="float32") # arr2 = rnp.NumArray([[5, 6], [7, 8]], dtype="float32") # concatenated_array = rnp.concatenate([arr1, arr2], axis=1) # print(concatenated_array) ``` -------------------------------- ### Create Array with Arange Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Create a NumArray with evenly spaced values within a given interval, similar to NumPy's np.arange. ```Python rnp.arange(start, stop, step) ``` -------------------------------- ### rustynum.arange Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Creates an array with evenly spaced values within a given interval. ```APIDOC ## rustynum.arange ### Description Creates an array with evenly spaced values within a given interval. ### Function rustynum.arange ``` -------------------------------- ### NumArray.dot Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Computes the dot product with another NumArray. ```APIDOC ## NumArray.dot ### Description Computes the dot product with another `NumArray`. ### Method Signature `dot(other: NumArray) -> NumArray` ### Usage ```python # Assuming 'a' and 'b' are NumArray instances dot_product = a.dot(b) ``` ``` -------------------------------- ### Perform Matrix-Matrix Multiplication with @ Operator Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Utilizes Python's '@' operator for matrix-matrix multiplication with RustyNum, providing a concise syntax similar to NumPy. ```python result_matrix = A @ B print("Matrix-Matrix Multiplication Result:\n", result_matrix) ``` -------------------------------- ### Fancy Indexing Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Access elements of a NumArray using advanced indexing techniques, similar to NumPy. ```Python rnp.ones((2,3))[0, :] ``` -------------------------------- ### Perform Matrix-Vector Multiplication with @ Operator Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/better-matrix-operations.md Uses Python's matrix multiplication operator '@' for matrix-vector multiplication with RustyNum. This is the recommended approach. ```python result_vec = matrix @ vector print("Matrix-Vector Multiplication Result:\n", result_vec) ``` -------------------------------- ### Dot Product with NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compute the dot product between two NumArrays. ```Python dot_product = a.dot(b) ``` -------------------------------- ### rustynum.dot Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Computes the dot product of two arrays. ```APIDOC ## rustynum.dot ### Description Computes the dot product of two arrays. ### Function rustynum.dot ``` -------------------------------- ### rustynum.log Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Computes the element-wise natural logarithm of an array. ```APIDOC ## rustynum.log ### Description Computes the element-wise natural logarithm of an array. ### Function rustynum.log ``` -------------------------------- ### Find Minimum Value in Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Find the minimum value in a NumArray, similar to NumPy's np.min. ```Python rnp.min(a) ``` -------------------------------- ### rustynum.median Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Calculates the median of the elements in an array. ```APIDOC ## rustynum.median ### Description Calculates the median of the elements in an array. ### Function rustynum.median ``` -------------------------------- ### rustynum.exp Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Computes the element-wise exponential of an array. ```APIDOC ## rustynum.exp ### Description Computes the element-wise exponential of an array. ### Function rustynum.exp ``` -------------------------------- ### Element-wise Addition Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Perform element-wise addition between two NumArrays. ```Python a + b ``` -------------------------------- ### rustynum.max Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Finds the maximum value in an array. ```APIDOC ## rustynum.max ### Description Finds the maximum value in an array. ### Function rustynum.max ``` -------------------------------- ### Matrix Multiplication with NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Perform matrix multiplication between two NumArrays using the matmul method or the @ operator. ```Python result = a.matmul(b) # or result = a @ b ``` -------------------------------- ### Calculate Median of Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Calculate the median of a NumArray, similar to NumPy's np.median. ```Python rnp.median(a) ``` -------------------------------- ### Find Maximum Value in Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Find the maximum value in a NumArray, similar to NumPy's np.max. ```Python rnp.max(a) ``` -------------------------------- ### NumArray.matmul Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Performs matrix multiplication with another NumArray. ```APIDOC ## NumArray.matmul ### Description Performs matrix multiplication with another `NumArray`. ### Method Signature `matmul(other: NumArray) -> NumArray` ### Usage ```python # Assuming 'a' and 'b' are NumArray instances result = a.matmul(b) # Alternatively, using the @ operator: result = a @ b ``` ``` -------------------------------- ### rustynum.concatenate Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Joins a sequence of arrays along a specified axis. ```APIDOC ## rustynum.concatenate ### Description Joins a sequence of arrays along a specified axis. ### Function rustynum.concatenate ``` -------------------------------- ### rustynum.NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Represents a numerical array in RustyNum. This class provides methods for various numerical operations. ```APIDOC ## rustynum.NumArray ### Description Represents a numerical array in RustyNum. This class provides methods for various numerical operations. ### Class rustynum.NumArray ``` -------------------------------- ### rustynum.sigmoid Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Computes the element-wise sigmoid function of an array. ```APIDOC ## rustynum.sigmoid ### Description Computes the element-wise sigmoid function of an array. ### Function rustynum.sigmoid ``` -------------------------------- ### Element-wise Division Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Perform element-wise division between two NumArrays. ```Python a / b ``` -------------------------------- ### Calculate Dot Product of Arrays Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compute the dot product of two NumArrays, similar to NumPy's np.dot. ```Python rnp.dot(a, b) ``` -------------------------------- ### rustynum.mean Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Calculates the arithmetic mean of the elements in an array. ```APIDOC ## rustynum.mean ### Description Calculates the arithmetic mean of the elements in an array. ### Function rustynum.mean ``` -------------------------------- ### L2 Normalization Function with RustyNum Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/tutorials/streamlining-machine-learning-preprocessing.md A Python function to normalize rows of a RustyNum array to unit L2 norm. It calculates the L2 norm for each row and then divides the row by its norm. ```python def l2_normalize(array): """ Normalize array rows to unit L2 norm. """ # Compute L2 norm along axis 1 (for each row) norms = array.norm(p=2, axis=[1]) # Reshape norms to allow broadcasting (add dimension) norms = norms.reshape([norms.shape[0], 1]) # Divide each row by its norm (broadcasting will work) return array / norms # Create sample data data_rn = rnp.NumArray([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype="float32") # Normalize the data normalized_data_rn = l2_normalize(data_rn) print("Data after L2 normalization:", normalized_data_rn.tolist()) ``` -------------------------------- ### rustynum.norm Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/api/index.md Computes the norm of an array. ```APIDOC ## rustynum.norm ### Description Computes the norm of an array. ### Function rustynum.norm ``` -------------------------------- ### NumArray.min Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Returns the minimum value in the array, optionally along a specified axis. ```APIDOC ## NumArray.min ### Description Returns the minimum value in the array. ### Method Signature `min(axis: Union[None, int, Sequence[int]] = None) -> Union[NumArray, float]` ### Usage ```python # Assuming 'a' is a NumArray instance minimum = a.min() ``` ``` -------------------------------- ### Calculate Median of NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compute the median of a NumArray, optionally along a specified axis. ```Python median = a.median() median_axis0 = a.median(axis=0) ``` -------------------------------- ### Element-wise Multiplication Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Perform element-wise multiplication between two NumArrays. ```Python a * b ``` -------------------------------- ### Calculate Natural Logarithm of Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compute the element-wise natural logarithm of a NumArray, similar to NumPy's np.log. ```Python rnp.log(a) ``` -------------------------------- ### Calculate Exponential of Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compute the element-wise exponential of a NumArray, similar to NumPy's np.exp. ```Python rnp.exp(a) ``` -------------------------------- ### Calculate Mean of Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Calculate the mean of a NumArray, similar to NumPy's np.mean. ```Python rnp.mean(a) ``` -------------------------------- ### Reshape Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Reshape a NumArray to a new shape, similar to NumPy's reshape. ```Python a.reshape([2, 3]) ``` -------------------------------- ### NumArray.median Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Computes the median of the array elements, optionally along a specified axis. ```APIDOC ## NumArray.median ### Description Computes the median along specified axis. ### Method Signature `median(axis: Union[None, int, Sequence[int]] = None) -> Union[NumArray, float]` ### Usage ```python # Assuming 'a' is a NumArray instance median = a.median() median_axis0 = a.median(axis=0) ``` ``` -------------------------------- ### Calculate Sigmoid of Array Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compute the element-wise sigmoid of a NumArray. ```Python rnp.sigmoid(a) ``` -------------------------------- ### Compute Mean of RustyNum Array Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Calculates the arithmetic mean of the elements in a RustyNum NumArray. Use `.item()` to extract the scalar value. ```python mean_value = a.mean().item() ``` -------------------------------- ### Reshape NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Reshape an existing NumArray to a new shape specified by a list of integers. ```Python reshaped = a.reshape([3, 1]) ``` -------------------------------- ### Element-wise Subtraction Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Perform element-wise subtraction between two NumArrays. ```Python a - b ``` -------------------------------- ### NumArray.max Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Returns the maximum value in the array, optionally along a specified axis. ```APIDOC ## NumArray.max ### Description Returns the maximum value in the array. ### Method Signature `max(axis: Union[None, int, Sequence[int]] = None) -> Union[NumArray, float]` ### Usage ```python # Assuming 'a' is a NumArray instance maximum = a.max() ``` ``` -------------------------------- ### NumArray.reshape Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Reshapes the array to the specified dimensions. ```APIDOC ## NumArray.reshape ### Description Reshapes the array to the specified shape. ### Method Signature `reshape(shape: List[int]) -> NumArray` ### Usage ```python # Assuming 'a' is a NumArray instance reshaped = a.reshape([3, 1]) ``` ``` -------------------------------- ### Calculate Mean of NumArray Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Compute the mean of a NumArray, optionally along a specified axis. ```Python average = a.mean() average_axis0 = a.mean(axis=0) ``` -------------------------------- ### Concatenate Arrays Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Concatenate a list of NumArrays along a specified axis, similar to NumPy's np.concatenate. ```Python rnp.concatenate([a,b], axis=0) ``` -------------------------------- ### NumArray.mean Source: https://github.com/igorsusmelj/rustynum/blob/main/README.md Computes the mean of the array elements, optionally along a specified axis. ```APIDOC ## NumArray.mean ### Description Computes the mean along specified axis. ### Method Signature `mean(axis: Union[None, int, Sequence[int]] = None) -> Union[NumArray, float]` ### Usage ```python # Assuming 'a' is a NumArray instance average = a.mean() average_axis0 = a.mean(axis=0) ``` ``` -------------------------------- ### Compute Mean of NumPy Array Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Calculates the arithmetic mean of the elements in a NumPy array. Returns a NumPy scalar. ```python mean_value = a.mean() ``` -------------------------------- ### Add Scalar to NumPy Array Source: https://github.com/igorsusmelj/rustynum/blob/main/docs/quick-start.md Adds a scalar value to each element of a NumPy array. This operation is common for scaling or shifting array data. ```python a = a + 2 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.