### Add SmartCore Dependency Source: https://smartcorelib.org/user_guide/quick_start This snippet shows how to add the SmartCore library to your Rust project's Cargo.toml file to start using its machine learning functionalities. ```toml [dependencies] smartcore = "0.2.0" ``` -------------------------------- ### Fit Logistic Regression with nalgebra Source: https://smartcorelib.org/user_guide/quick_start Illustrates fitting a Logistic Regression model with SmartCore using the `nalgebra` crate for linear algebra operations. The example covers loading the Iris dataset, converting it into `nalgebra` matrices, training the model, generating predictions, and evaluating accuracy. ```rust use nalgebra::{DMatrix, RowDVector}; use smartcore::dataset::iris::load_dataset; use smartcore::linear::logistic_regression::LogisticRegression; use smartcore::metrics::accuracy; // Load Iris dataset let iris_data = load_dataset(); // Turn Iris dataset into NxM matrix using nalgebra let x = DMatrix::from_row_slice( iris_data.num_samples, iris_data.num_features, &iris_data.data, ); // These are our target class labels let y = RowDVector::from_iterator(iris_data.num_samples, iris_data.target.into_iter()); // Fit Logistic Regression to Iris dataset let lr = LogisticRegression::fit(&x, &y, Default::default()).unwrap(); // Predict class labels let y_hat = lr.predict(&x).unwrap(); // Calculate training error println!("accuracy: {}", accuracy(&y, &y_hat)); // Prints 0.98 ``` -------------------------------- ### Train Logistic Regression on Iris Dataset Source: https://smartcorelib.org/user_guide/quick_start This example shows how to train a Logistic Regression model using the SmartCore library on the Iris dataset. It follows similar steps to the KNN example: loading data, matrix conversion, fitting the model, making predictions, and evaluating accuracy. ```rust use smartcore::dataset::iris::load_dataset; use smartcore::linalg::naive::dense_matrix::DenseMatrix; use smartcore::linear::logistic_regression::LogisticRegression; use smartcore::metrics::accuracy; // Load Iris dataset let iris_data = load_dataset(); // Turn Iris dataset into NxM matrix let x = DenseMatrix::from_array( iris_data.num_samples, iris_data.num_features, &iris_data.data, ); // These are our target class labels let y = iris_data.target; // Fit Logistic Regression to Iris dataset let lr = LogisticRegression::fit(&x, &y, Default::default()).unwrap(); let y_hat = lr.predict(&x).unwrap(); // Predict class labels // Calculate training error println!("accuracy: {}", accuracy(&y, &y_hat)); // Prints 0.98 ``` -------------------------------- ### Rust Development Commands Source: https://smartcorelib.org/user_guide/developer Essential commands for testing, formatting, and linting Rust code within the SmartCore project to ensure code quality and adherence to style guidelines. ```bash cargo test --features "ndarray-bindings, nalgebra-bindings" ``` ```bash cargo fmt ``` ```bash cargo clippy --all-features -- -Drust-2018-idioms -Dwarnings ``` -------------------------------- ### SmartCore API Interfaces and Data Handling Source: https://smartcorelib.org/user_guide/quick_start Details the core interfaces for machine learning algorithms in SmartCore, including `SupervisedEstimator`, `UnsupervisedEstimator`, and `Predictor` traits for fitting models and making predictions. It also outlines the expected input and output data formats, typically matrices and vectors, and references base traits for linear algebra operations. ```APIDOC SmartCore Core API: Core interfaces for algorithms are defined in the `smartcore::api` module. fit function: - Defined in `SupervisedEstimator` (for supervised learning) and `UnsupervisedEstimator` (for unsupervised learning). - Takes training data (predictors and optional targets) and hyperparameters. - Produces a fully trained estimator instance. - `SupervisedEstimator` requires target values in addition to predictors. predict function: - Defined in the `Predictor` trait. - Used to predict labels or target values from new data. - Mandatory model parameters are function parameters; optional parameters use `Default::default()`. Input and Output: - Algorithms accept two-dimensional arrays (matrices) and vectors as input and produce matrices and vectors on output. - **X**: NxM matrix (N samples, M features per sample). - **y**: Vector of training labels (length N for supervised learning). - Traits `BaseMatrix` and `BaseVector` define matrix and vector operations used by SmartCore. ``` -------------------------------- ### Fit Logistic Regression with ndarray Source: https://smartcorelib.org/user_guide/quick_start Demonstrates how to fit a Logistic Regression model using the SmartCore library with the `ndarray` crate for data manipulation. It includes loading the Iris dataset, preparing data into `ndarray` matrices, fitting the model, making predictions, and calculating accuracy. ```rust use ndarray::Array; use smartcore::dataset::iris::load_dataset; use smartcore::linear::logistic_regression::LogisticRegression; use smartcore::metrics::accuracy; // Load Iris dataset let iris_data = load_dataset(); // Turn Iris dataset into NxM matrix using ndarray let x = Array::from_shape_vec( (iris_data.num_samples, iris_data.num_features), iris_data.data, ).unwrap(); // These are our target class labels let y = Array::from_shape_vec(iris_data.num_samples, iris_data.target).unwrap(); // Fit Logistic Regression to Iris dataset let lr = LogisticRegression::fit(&x, &y, Default::default()).unwrap(); // Predict class labels let y_hat = lr.predict(&x).unwrap(); // Calculate training error println!("accuracy: {}", accuracy(&y, &y_hat)); // Prints 0.98 ``` -------------------------------- ### Train KNN Classifier on Iris Dataset Source: https://smartcorelib.org/user_guide/quick_start This snippet demonstrates how to load the Iris dataset, convert it into a DenseMatrix, and train a K-Nearest Neighbors classifier. It then predicts class labels and calculates the accuracy of the model on the training data. ```rust use smartcore::dataset::iris::load_dataset; use smartcore::linalg::naive::dense_matrix::DenseMatrix; use smartcore::neighbors::knn_classifier::KNNClassifier; use smartcore::metrics::accuracy; // Load Iris dataset let iris_data = load_dataset(); // Turn Iris dataset into NxM matrix let x = DenseMatrix::from_array( iris_data.num_samples, iris_data.num_features, &iris_data.data, ); // These are our target class labels let y = iris_data.target; // Fit KNN classifier to Iris dataset let knn = KNNClassifier::fit( &x, &y, Default::default(), ).unwrap(); let y_hat = knn.predict(&x).unwrap(); // Predict class labels // Calculate training error println!("accuracy: {}", accuracy(&y, &y_hat)); // Prints 0.96 ``` -------------------------------- ### Rust SVD Decomposition of Digits Dataset Source: https://smartcorelib.org/user_guide/unsupervised Demonstrates Singular Value Decomposition (SVD) using the SmartCore Rust library. The example loads the Digits dataset, decomposes it into U, Sigma, and V matrices, and verifies the reconstruction of the original matrix. ```Rust use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // SVD use smartcore::linalg::BaseMatrix; use smartcore::linalg::svd::SVDDecomposableMatrix; // Load dataset let digits_data = digits::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( digits_data.num_samples, digits_data.num_features, &digits_data.data, ); // Decompose matrix into U . Sigma . V^T let svd = x.svd().unwrap(); let u: &DenseMatrix = &svd.U; //U let v: &DenseMatrix = &svd.V; // V let s: &DenseMatrix = &svd.S(); // Sigma // Print dimensions of components println!("U is {}x{}", u.shape().0, u.shape().1); println!("V is {}x{}", v.shape().0, v.shape().1); println!("sigma is {}x{}", s.shape().0, s.shape().1); // Restore original matrix let x_hat = u.matmul(s).matmul(&v.transpose()); for (x_i, x_hat_i) in x.iter().zip(x_hat.iter()){ assert!((x_i - x_hat_i).abs() < 1e-3) } ``` -------------------------------- ### Fit DBSCAN Clustering in Rust Source: https://smartcorelib.org/user_guide/unsupervised Demonstrates fitting a DBSCAN clustering model to data using the SmartCore library. It generates a synthetic dataset (circles), transforms it into a dense matrix, fits the DBSCAN model with specified epsilon (eps=0.2) and minimum samples (min_samples=5), predicts labels, and evaluates performance. It also includes an example of plotting the results. ```Rust // Load datasets API use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // DBSCAN use smartcore::cluster::dbscan::{DBSCANParameters, DBSCAN}; // Performance metrics use smartcore::metrics::{completeness_score, homogeneity_score, v_measure_score}; // Load dataset let circles = generator::make_circles(1000, 0.5, 0.05); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array(circles.num_samples, circles.num_features, &circles.data); // These are our target class labels let true_labels = circles.target; // Fit & predict let labels = DBSCAN::fit( &x, DBSCANParameters::default() .with_eps(0.2) .with_min_samples(5), ) .and_then(|c| c.predict(&x)) .unwrap(); // Measure performance println!("Homogeneity: {}", homogeneity_score(&true_labels, &labels)); println!( "Completeness: {}", completeness_score(&true_labels, &labels) ); println!("V Measure: {}", v_measure_score(&true_labels, &labels)); utils::scatterplot( &x, Some(&labels.into_iter().map(|f| f as usize).collect()), "test", ) .unwrap(); ``` -------------------------------- ### Fit Support Vector Regressor in Rust Source: https://smartcorelib.org/user_guide/supervised This example demonstrates fitting a Support Vector Regressor (SVR) using the smartcore library. It loads the diabetes dataset, prepares it as a dense matrix, splits the data, and trains the SVR model with a Radial Basis Function (RBF) kernel and specified C and epsilon parameters. The performance is then evaluated using the Mean Squared Error (MSE). ```Rust use smartcore::dataset::*; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // SVM use smartcore::svm::svr::{SVRParameters, SVR}; use smartcore::svm::Kernels; // Model performance use smartcore::model_selection::train_test_split; use smartcore::metrics::mean_squared_error; // Load dataset let diabetes_data = diabetes::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( diabetes_data.num_samples, diabetes_data.num_features, &diabetes_data.data, ); // These are our target values let y = diabetes_data.target; // Split dataset into training/test (80%/20%) let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // SVM let y_hat_svm = SVR::fit(&x_train, &y_train, SVRParameters::default().with_kernel(Kernels::rbf(0.5)). with_c(2000.0).with_eps(10.0)) .and_then(|svm| svm.predict(&x_test)) .unwrap(); // Calculate test error println!( "MSE: {}", mean_squared_error(&y_test, &y_hat_svm) ); ``` -------------------------------- ### Logistic Regression with SmartCore in Rust Source: https://smartcorelib.org/user_guide/supervised Illustrates Logistic Regression using the SmartCore library. This example loads the breast cancer dataset, prepares it for classification, splits the data, trains a Logistic Regression model, and predicts class labels. Performance is measured using the Area Under the ROC Curve (AUC). ```rust use smartcore::dataset::*; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Logistic Regression use smartcore::linear::logistic_regression::LogisticRegression; // Model performance use smartcore::metrics::roc_auc_score; use smartcore::model_selection::train_test_split; // Load dataset let cancer_data = breast_cancer::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( cancer_data.num_samples, cancer_data.num_features, &cancer_data.data, ); // These are our target class labels let y = cancer_data.target; // Split dataset into training/test (80%/20%) let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // Logistic Regression let y_hat_lr = LogisticRegression::fit(&x_train, &y_train, Default::default()) .and_then(|lr| lr.predict(&x_test)).unwrap(); // Calculate test error println!("AUC: {}", roc_auc_score(&y_test, &y_hat_lr)); ``` -------------------------------- ### Evaluate Model with K-fold CV using cross_validate in Rust Source: https://smartcorelib.org/user_guide/model_selection Illustrates model evaluation using k-fold cross-validation with the `cross_validate` function. This example applies Logistic Regression to the Breast Cancer dataset, performing a 3-fold cross-validation. It calculates and prints the mean test and training scores, providing a more robust performance estimate than a simple train/test split. Dependencies include `smartcore::dataset::breast_cancer`, `smartcore::linalg::naive::dense_matrix::DenseMatrix`, `smartcore::linear::logistic_regression::LogisticRegression`, `smartcore::metrics::accuracy`, and `smartcore::model_selection::{cross_validate, KFold}`. ```rust use smartcore::dataset::breast_cancer; use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Logistic regression use smartcore::linear::logistic_regression::LogisticRegression; // Model performance use smartcore::metrics::accuracy; // K-fold CV use smartcore::model_selection::{cross_validate, KFold}; // Load dataset let breast_cancer_data = breast_cancer::load_dataset(); let x = DenseMatrix::from_array( breast_cancer_data.num_samples, breast_cancer_data.num_features, &breast_cancer_data.data, ); // These are our target values let y = breast_cancer_data.target; // cross-validated estimator let results = cross_validate( LogisticRegression::fit, &x, &y, Default::default(), KFold::default().with_n_splits(3), accuracy, ) .unwrap(); println!( "Test score: {}, training score: " results.mean_test_score(), results.mean_train_score() ); ``` -------------------------------- ### PCA Dimensionality Reduction in SmartCore Source: https://smartcorelib.org/user_guide/unsupervised Demonstrates Principal Component Analysis (PCA) for dimensionality reduction using the SmartCore library. PCA projects data onto a lower-dimensional subspace while retaining maximum variance. This example shows fitting PCA to the Digits dataset and transforming it to two principal components. ```rust use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // PCA use smartcore::decomposition::pca::{PCA, PCAParameters}; // Load dataset let digits_data = digits::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( digits_data.num_samples, digits_data.num_features, &digits_data.data, ); // These are our target class labels let labels = digits_data.target; // Fit PCA to digits dataset let pca = PCA::fit(&x, PCAParameters::default().with_n_components(2)).unwrap(); // Reduce dimensionality of X to 2 principal components let x_transformed = pca.transform(&x).unwrap(); ``` -------------------------------- ### SVD Dimensionality Reduction in SmartCore Source: https://smartcorelib.org/user_guide/unsupervised Illustrates Singular Value Decomposition (SVD) for dimensionality reduction with the SmartCore library. SVD is another powerful technique for reducing the number of features in a dataset. This example loads the Digits dataset and applies SVD to reduce its dimensions. ```rust // Load datasets API use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // SVD use smartcore::decomposition::svd::{SVDParameters, SVD}; // Load dataset let digits_data = digits::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( digits_data.num_samples, digits_data.num_features, &digits_data.data, ); // These are our target class labels let labels = digits_data.target; // Fit SVD to digits dataset let svd = SVD::fit(&x, SVDParameters::default().with_n_components(2)).unwrap(); // Reduce dimensionality of X let x_transformed = svd.transform(&x).unwrap(); ``` -------------------------------- ### Fit Random Forest Regressor to Boston Housing Dataset Source: https://smartcorelib.org/user_guide/supervised Demonstrates fitting a `RandomForestRegressor` from the `smartcore` library to the Boston Housing dataset. It covers data loading, preprocessing into a `DenseMatrix`, splitting into training and testing sets, fitting the model, making predictions, and evaluating performance using Mean Squared Error. Dependencies include `smartcore`'s dataset, linalg, ensemble, metrics, and model_selection modules. ```rust use smartcore::dataset::*; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Random Forest use smartcore::ensemble::random_forest_regressor::RandomForestRegressor; // Model performance use smartcore::metrics::mean_squared_error; use smartcore::model_selection::train_test_split; // Load dataset let cancer_data = boston::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( cancer_data.num_samples, cancer_data.num_features, &cancer_data.data, ); // These are our target class labels let y = cancer_data.target; // Split dataset into training/test (80%/20%) let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // Random Forest let y_hat_rf = RandomForestRegressor::fit(&x_train, &y_train, Default::default()) .and_then(|rf| rf.predict(&x_test)).unwrap(); // Calculate test error println!("MSE: {}", mean_squared_error(&y_test, &y_hat_rf)); ``` -------------------------------- ### Fit and Predict with Gaussian Naive Bayes in Rust Source: https://smartcorelib.org/user_guide/supervised Demonstrates how to load the Iris dataset, prepare it into a DenseMatrix, fit a Gaussian Naive Bayes model, and predict class labels. It also shows how to calculate the accuracy of the model on the training data. ```rust use smartcore::dataset::iris::load_dataset; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Imports Gaussian Naive Bayes classifier use smartcore::naive_bayes::gaussian::GaussianNB; // Model performance use smartcore::metrics::accuracy; // Load Iris dataset let iris_data = load_dataset(); // Turn Iris dataset into NxM matrix let x = DenseMatrix::from_array( iris_data.num_samples, iris_data.num_features, &iris_data.data, ); // These are our target class labels let y = iris_data.target; // Fit Logistic Regression to Iris dataset let gnb = GaussianNB::fit(&x, &y, Default::default()).unwrap(); let y_hat = gnb.predict(&x).unwrap(); // Predict class labels // Calculate training error println!("accuracy: {}", accuracy(&y, &y_hat)); // Prints 0.96 ``` -------------------------------- ### Linear Regression with SmartCore Source: https://smartcorelib.org/user_guide/supervised Demonstrates fitting a Linear Regression model using Ordinary Least Squares with the smartcore Rust library, including data loading, splitting, prediction, and error calculation. It highlights the use of DenseMatrix for data representation and various metrics for evaluation. ```Rust use smartcore::dataset::*; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Linear Regression use smartcore::linear::linear_regression::LinearRegression; // Model performance use smartcore::metrics::mean_squared_error; use smartcore::model_selection::train_test_split; // Load dataset let cancer_data = boston::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( cancer_data.num_samples, cancer_data.num_features, &cancer_data.data, ); // These are our target class labels let y = cancer_data.target; // Split dataset into training/test (80%/20%) let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // Linear Regression let y_hat_lr = LinearRegression::fit(&x_train, &y_train, Default::default()) .and_then(|lr| lr.predict(&x_test)).unwrap(); // Calculate test error println!("MSE: {}", mean_squared_error(&y_test, &y_hat_lr)); ``` -------------------------------- ### Fit KNN Regressor with smartcorelib Rust Source: https://smartcorelib.org/user_guide/supervised Shows how to fit a K-Nearest Neighbors Regressor to the Boston Housing dataset using smartcorelib. It includes data loading, splitting, fitting the model with custom parameters (Euclidean distance), prediction, and calculating Mean Squared Error. ```rust use smartcore::dataset::*; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // KNN use smartcore::math::distance::Distances; use smartcore::neighbors::knn_regressor::{KNNRegressor, KNNRegressorParameters}; // Model performance use smartcore::metrics::mean_squared_error; use smartcore::model_selection::train_test_split; // Load dataset let cancer_data = boston::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( cancer_data.num_samples, cancer_data.num_features, &cancer_data.data, ); // These are our target class labels let y = cancer_data.target; // Split dataset into training/test (80%/20%) let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // KNN regressor let y_hat_knn = KNNRegressor::fit( &x_train, &y_train, KNNRegressorParameters::default().with_distance(Distances::euclidian()), ).and_then(|knn| knn.predict(&x_test)).unwrap(); // Calculate test error println!("MSE: {}", mean_squared_error(&y_test, &y_hat_knn)); ``` -------------------------------- ### SmartCore 0.2.0 Release Features Source: https://smartcorelib.org/about This entry summarizes the new algorithms and features introduced in SmartCore version 0.2.0. It includes supervised learning models like DBSCAN, Epsilon-SVR, SVC, Ridge, Lasso, ElasticNet, and various Naive Bayes implementations. Unsupervised learning enhancements include K-fold Cross Validation, Singular value decomposition, and Cholesky decomposition. API changes and dependency upgrades are also noted. ```APIDOC SmartCore 0.2.0 Release Highlights: Algorithms Added/Improved: - DBSCAN - Epsilon-SVR, SVC - Ridge, Lasso, ElasticNet - Bernoulli Naive Bayes - Gaussian Naive Bayes - Categorical Naive Bayes - Multinomial Naive Bayes - K-fold Cross Validation - Singular value decomposition (SVD) - Cholesky decomposition API and Module Changes: - New 'api' module introduced. - Integration with Clippy. - smartcore::error:FailedError is now non-exhaustive. - ndarray upgraded to 0.14. - API changes in: K-Means, PCA, Random Forest, Linear and Logistic Regression, KNN, Decision Tree. Related Functionality: - Supervised Learning - Unsupervised Learning - Model Selection - Linear Algebra Decompositions ``` -------------------------------- ### Split Data with train_test_split in Rust Source: https://smartcorelib.org/user_guide/model_selection Demonstrates splitting a dataset into training and testing sets using the `train_test_split` function from the `smartcore::model_selection` module. It shows how to load the Boston Housing dataset, transform it into a `DenseMatrix`, and split it into 80% training and 20% testing data. This is useful for large datasets but can be sensitive to the split. ```rust use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Model performance use smartcore::model_selection::train_test_split; // Load dataset let boston_data = boston::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( boston_data.num_samples, boston_data.num_features, &boston_data.data, ); // These are our target class labels let y = boston_data.target; // Split data 80/20 let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); ``` -------------------------------- ### Fit K-means Clustering in Rust Source: https://smartcorelib.org/user_guide/unsupervised Demonstrates fitting a K-means clustering model to data using the SmartCore library. It loads a dataset, transforms it into a dense matrix, fits the KMeans model with a specified number of clusters (k=10), predicts labels, and then calculates homogeneity, completeness, and v-measure scores to evaluate performance against true labels. ```Rust // Load datasets API use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // K-Means use smartcore::cluster::kmeans::{KMeans, KMeansParameters}; // Performance metrics use smartcore::metrics::{homogeneity_score, completeness_score, v_measure_score}; // Load dataset let digits_data = digits::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( digits_data.num_samples, digits_data.num_features, &digits_data.data, ); // These are our target class labels let true_labels = digits_data.target; // Fit & predict let labels = KMeans::fit(&x, KMeansParameters::default().with_k(10)) .and_then(|kmeans| kmeans.predict(&x)) .unwrap(); // Measure performance println!("Homogeneity: {}", homogeneity_score(&true_labels, &labels)); println!("Completeness: {}", completeness_score(&true_labels, &labels)); println!("V Measure: {}", v_measure_score(&true_labels, &labels)); ``` -------------------------------- ### SmartCore Dependencies Source: https://smartcorelib.org/user_guide/model_selection Specifies the SmartCore library dependency in Cargo.toml. The `default-features = false` setting allows selective enabling of features like 'datasets'. ```toml [dependencies] smartcore = { version = "0.1.0", default-features = false} ``` -------------------------------- ### SmartCore Decision Tree and Data Handling APIs Source: https://smartcorelib.org/user_guide/supervised API documentation for key components in the `smartcore` library related to decision trees, data manipulation, and model evaluation. This includes classifiers, regressors, matrix structures, dataset loading, splitting, and performance metrics. ```APIDOC smartcore::tree::decision_tree_classifier::DecisionTreeClassifier - Fits a decision tree for classification tasks. - Parameters: - `x`: Training data features (e.g., `DenseMatrix`). - `y`: Training data target labels. - `params`: Configuration parameters for the classifier (e.g., `Default::default()` for default settings). - Returns: A trained `DecisionTreeClassifier` model. - Methods: - `predict(x: &DenseMatrix) -> Result, Error>`: Predicts class labels for new data. smartcore::tree::decision_tree_regressor::DecisionTreeRegressor - Fits a decision tree for regression tasks. - Parameters: - `x`: Training data features. - `y`: Training data target values. - `params`: Configuration parameters for the regressor. - Returns: A trained `DecisionTreeRegressor` model. smartcore::linalg::naive::dense_matrix::DenseMatrix - A dense matrix implementation for storing numerical data. - Used for representing datasets and feature matrices. - Methods: - `from_array(rows: usize, cols: usize, data: &[T]) -> DenseMatrix`: Creates a `DenseMatrix` from a slice. smartcore::metrics::roc_auc_score - Calculates the Area Under the Receiver Operating Characteristic Curve (AUC). - Parameters: - `y_true`: True target labels. - `y_pred`: Predicted labels or probabilities. - Returns: The AUC score. smartcore::model_selection::train_test_split - Splits datasets into random train and test subsets. - Parameters: - `x`: Features dataset. - `y`: Target dataset. - `test_size`: The proportion of the dataset to include in the test split. - `shuffle`: Whether to shuffle the data before splitting. - Returns: A tuple containing `(x_train, x_test, y_train, y_test)`. smartcore::dataset::breast_cancer::load_dataset - Loads the Breast Cancer dataset. - Returns: A struct containing dataset features (`data`) and target labels (`target`). ``` -------------------------------- ### Fit KNN Classifier with smartcorelib Rust Source: https://smartcorelib.org/user_guide/supervised Demonstrates fitting a K-Nearest Neighbors Classifier to the Breast Cancer dataset using the smartcorelib Rust library. It covers data loading, splitting, fitting the model with default parameters, prediction, and calculating AUC score. ```rust use smartcore::dataset::*; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Imports for KNN classifier use smartcore::neighbors::knn_classifier::KNNClassifier; // Model performance use smartcore::metrics::roc_auc_score; use smartcore::model_selection::train_test_split; // Load dataset let cancer_data = breast_cancer::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( cancer_data.num_samples, cancer_data.num_features, &cancer_data.data, ); // These are our target class labels let y = cancer_data.target; // Split dataset into training/test (80%/20%) let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // KNN classifier let y_hat_knn = KNNClassifier::fit( &x_train, &y_train, Default::default(), ).and_then(|knn| knn.predict(&x_test)).unwrap(); // Calculate test error println!("AUC: {}", roc_auc_score(&y_test, &y_hat_knn)); ``` -------------------------------- ### Linear Regression API and Parameters Source: https://smartcorelib.org/user_guide/supervised Details the API for Linear Regression in smartcore, including the `fit` and `predict` methods. It also describes the `LinearRegressionParameters` struct, which allows selection of decomposition methods like SVD (default) or QR, along with their respective runtime complexities. ```APIDOC LinearRegression: fit(x: &DenseMatrix, y: &Array, parameters: LinearRegressionParameters) -> Result - Fits the linear regression model using Ordinary Least Squares. - Parameters: - x: Training data features (NxM matrix). - y: Training data target values (N-dimensional array). - parameters: Configuration for the regression, including solver. - Returns: A trained LinearRegression model or an error. predict(x: &DenseMatrix) -> Result, Error> - Predicts target values for new data using the trained model. - Parameters: - x: New data features (PxM matrix). - Returns: Predicted target values (P-dimensional array) or an error. LinearRegressionParameters: solver: SolverType - Specifies the decomposition method for solving the normal equations. - Options: - SVD: Singular Value Decomposition (default). Runtime: O(mn^2 + n^3). - QR: QR Decomposition. Runtime: O(mn^2 + n^3/3). - T: Type of the data (e.g., f32, f64). Related Methods: mean_squared_error(y_true: &Array, y_pred: &Array) -> T - Calculates the Mean Squared Error between true and predicted values. train_test_split(x: &DenseMatrix, y: &Array, test_size: f64, shuffle: bool) -> (DenseMatrix, DenseMatrix, Array, Array) - Splits dataset into training and testing sets. DenseMatrix::from_array(rows: usize, cols: usize, data: &[T]) -> DenseMatrix - Creates a DenseMatrix from a slice of data. ``` -------------------------------- ### Save and Load KNN Model with Bincode Source: https://smartcorelib.org/user_guide/model_selection Demonstrates how to serialize (save) and deserialize (load) a KNN classifier model using the bincode format in Rust. It includes loading the Iris dataset, training a KNN model, saving it to a file, and then loading it back to make predictions. ```rust use std::fs::File; use std::io::prelude::*; use smartcore::dataset::iris::load_dataset; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Imports for KNN classifier use smartcore::math::distance::*; use smartcore::neighbors::knn_classifier::*; // Model performance use smartcore::metrics::accuracy; // Load Iris dataset let iris_data = load_dataset(); // Turn Iris dataset into NxM matrix let x = DenseMatrix::from_array( iris_data.num_samples, iris_data.num_features, &iris_data.data, ); // These are our target class labels let y = iris_data.target; // Fit KNN classifier to Iris dataset let knn = KNNClassifier::fit( &x, &y, Default::default(), ).unwrap(); // File name for the model let file_name = "iris_knn.model"; // Save the model { let knn_bytes = bincode::serialize(&knn).expect("Can not serialize the model"); File::create(file_name) .and_then(|mut f| f.write_all(&knn_bytes)) .expect("Can not persist model"); } // Load the model let knn: KNNClassifier = { let mut buf: Vec = Vec::new(); File::open(&file_name) .and_then(|mut f| f.read_to_end(&mut buf)) .expect("Can not load model"); bincode::deserialize(&buf).expect("Can not deserialize the model") }; //Predict class labels let y_hat = knn.predict(&x).unwrap(); // Predict class labels // Calculate training error println!("accuracy: {}", accuracy(&y, &y_hat)); // Prints 0.96 ``` -------------------------------- ### SmartCore 0.1.0 Release Features Source: https://smartcorelib.org/about This entry details the initial release (0.1.0) of the SmartCore library. It highlights the inclusion of various distance metrics for KNN, regression and classification models like Linear Regression (OLS), Logistic Regression, Random Forest, and Decision Trees. It also covers dimensionality reduction techniques (PCA), clustering (K-Means), and linear algebra methods (LU, QR, SVD, EVD). Integration with ndarray, nalgebra, and serde is also noted. ```APIDOC SmartCore 0.1.0 Initial Release Features: Core Algorithms: - KNN (with Euclidean, Minkowski, Manhattan, Hamming, Mahalanobis distances) - Linear Regression (OLS) - Logistic Regression - Random Forest Classifier - Decision Tree Classifier - PCA (Principal Component Analysis) - K-Means Clustering - RandomForest Regressor - Decision Tree Regressor Linear Algebra: - Abstract linear algebra methods - LU decomposition - QR decomposition - SVD (Singular Value Decomposition) - EVD (Eigenvalue Decomposition) Integrations: - Integrated with ndarray - Integrated with nalgebra - Serde integration Evaluation: - Evaluation Metrics Related Functionality: - Distance Metrics - Dimensionality Reduction - Clustering - Regression - Classification ``` -------------------------------- ### Fit Ridge Regression using Smartcore in Rust Source: https://smartcorelib.org/user_guide/supervised Demonstrates fitting a Ridge Regression model using the smartcore library. It loads a dataset, splits it into training and testing sets, fits the Ridge Regression model with L2 regularization, and predicts target values. The mean squared error is then calculated to evaluate the model's performance. ```Rust use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; use smartcore::linear::ridge_regression::{RidgeRegression, RidgeRegressionParameters}; // Model performance use smartcore::metrics::mean_squared_error; use smartcore::model_selection::train_test_split; // Load dataset let boston_data = boston::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( boston_data.num_samples, boston_data.num_features, &boston_data.data, ); // These are our target values let y = boston_data.target; let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // Ridge Regression let y_hat_rr = RidgeRegression::fit( &x_train, &y_train, RidgeRegressionParameters::default().with_alpha(0.5), ) .and_then(|rr| rr.predict(&x_test)) .unwrap(); // Calculate test error println!( "MSE Ridge Regression: {}", mean_squared_error(&y_test, &y_hat_rr) ); ``` -------------------------------- ### Fit LASSO Regression using Smartcore in Rust Source: https://smartcorelib.org/user_guide/supervised Demonstrates fitting a LASSO (Least Absolute Shrinkage and Selection Operator) model using the smartcore library. Similar to Ridge, it loads data, splits it, fits the LASSO model with L1 regularization, and predicts values. The mean squared error is computed for performance evaluation, highlighting LASSO's variable selection capability. ```Rust use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; use smartcore::linear::lasso::{Lasso, LassoParameters}; // Model performance use smartcore::metrics::mean_squared_error; use smartcore::model_selection::train_test_split; // Load dataset let boston_data = boston::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( boston_data.num_samples, boston_data.num_features, &boston_data.data, ); // These are our target values let y = boston_data.target; let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // LASSO let y_hat_lasso = Lasso::fit( &x_train, &y_train, LassoParameters::default().with_alpha(0.5), ) .and_then(|lr| lr.predict(&x_test)) .unwrap(); // Calculate test error println!("MSE LASSO: {}", mean_squared_error(&y_test, &y_hat_lasso)); ``` -------------------------------- ### Elastic Net Regression with SmartCore in Rust Source: https://smartcorelib.org/user_guide/supervised Demonstrates Elastic Net linear regression using the SmartCore library. It loads the Boston dataset, splits it into training and testing sets, trains an Elastic Net model with specified alpha and l1_ratio, and evaluates its performance using Mean Squared Error (MSE). ```rust use smartcore::dataset::*; // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; use smartcore::linear::elastic_net::{ElasticNet, ElasticNetParameters}; // Model performance use smartcore::metrics::mean_squared_error; use smartcore::model_selection::train_test_split; // Load dataset let boston_data = boston::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( boston_data.num_samples, boston_data.num_features, &boston_data.data, ); // These are our target values let y = boston_data.target; let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // Elastic Net let y_hat_en = ElasticNet::fit( &x_train, &y_train, ElasticNetParameters::default() .with_alpha(0.5) .with_l1_ratio(0.5), ) .and_then(|lr| lr.predict(&x_test)) .unwrap(); // Calculate test error println!( "MSE Elastic Net: {}", mean_squared_error(&y_test, &y_hat_en) ); ``` -------------------------------- ### Fit Decision Tree Classifier (Rust) Source: https://smartcorelib.org/user_guide/supervised Demonstrates fitting a `DecisionTreeClassifier` from the `smartcore` library to the Breast Cancer dataset. Includes data loading, preprocessing into `DenseMatrix`, splitting into training/test sets, fitting the model, making predictions, and calculating the AUC score. ```rust use smartcore::dataset::* // DenseMatrix wrapper around Vec use smartcore::linalg::naive::dense_matrix::DenseMatrix; // Tree use smartcore::tree::decision_tree_classifier::DecisionTreeClassifier; // Model performance use smartcore::metrics::roc_auc_score; use smartcore::model_selection::train_test_split; // Load dataset let cancer_data = breast_cancer::load_dataset(); // Transform dataset into a NxM matrix let x = DenseMatrix::from_array( cancer_data.num_samples, cancer_data.num_features, &cancer_data.data, ); // These are our target class labels let y = cancer_data.target; // Split dataset into training/test (80%/20%) let (x_train, x_test, y_train, y_test) = train_test_split(&x, &y, 0.2, true); // Decision Tree let y_hat_tree = DecisionTreeClassifier::fit(&x_train, &y_train, Default::default()) .and_then(|tree| tree.predict(&x_test)).unwrap(); // Calculate test error println!("AUC: {}", roc_auc_score(&y_test, &y_hat_tree)); ``` -------------------------------- ### Supervised Learning Interface Source: https://smartcorelib.org/user_guide/supervised SmartCore implements supervised learning algorithms that support both classification (qualitative output) and regression (quantitative output). All algorithms follow a consistent naming convention for their methods. ```APIDOC SupervisedLearningModel: fit(x: Predictors, y: TargetValues) - Fits the model to the provided data. - Parameters: - x: Predictors (input features). - y: Target values (output variable). - Optional parameters are accessible via `Default::default()`. predict(x: Predictors) -> Predictions - Makes predictions on new observations. - Parameters: - x: New observations (input features). - Returns: Predicted class labels or target values. ```