### 6 releases

0.3.2 | May 1, 2023 |
---|---|

0.3.1 | Mar 20, 2023 |

0.3.0 | Nov 9, 2022 |

0.2.1 | May 9, 2022 |

0.1.0 | Sep 26, 2020 |

#**22** in Machine learning

**3,040** downloads per month

Used in **3** crates

**Apache-2.0**

1MB

**23K**
SLoC

**
User guide | API | Notebooks
**

**Machine Learning in Rust**

To start getting familiar with the new smartcore v0.3 API, there is now available a **Jupyter Notebook environment repository**. Please see instructions there, contributions welcome see CONTRIBUTING.

###
`lib.rs`

:

# smartcore

Welcome to

, machine learning in Rust!`smartcore`

features various classification, regression and clustering algorithms including support vector machines, random forests, k-means and DBSCAN,
as well as tools for model selection and model evaluation.`smartcore`

provides its own traits system that extends Rust standard library, to deal with linear algebra and common
computational models. Its API is designed using well recognizable patterns. Extra features (like support for ndarray
structures) is available via optional features.`smartcore`

## Getting Started

To start using

latest stable version simply add the following to your `smartcore`

file:`Cargo .toml`

`[dependencies]
smartcore = "*"
`

To start using smartcore development version with latest unstable additions:

`[dependencies]
smartcore = { git = "https://github.com/smartcorelib/smartcore", branch = "development" }
`

There are different features that can be added to the base library, for example to add sample datasets:

`[dependencies]
smartcore = { git = "https://github.com/smartcorelib/smartcore", features = ["datasets"] }
`

Check

's `smartcore`

for available features.`Cargo .toml`

## Using Jupyter

For quick introduction, Jupyter Notebooks are available here. You can set up a local environment to run Rust notebooks using EVCXR following these instructions.

## First Example

For example, you can use this code to fit a K Nearest Neighbors classifier to a dataset that is defined as standard Rust vector:

`//` DenseMatrix definition
`use` `smartcore``::``linalg``::``basic``::``matrix``::`DenseMatrix`;`
`//` KNNClassifier
`use` `smartcore``::``neighbors``::``knn_classifier``::``*``;`
`//` Various distance metrics
`use` `smartcore``::``metrics``::``distance``::``*``;`
`//` Turn Rust vector-slices with samples into a matrix
`let` x `=` `DenseMatrix``::`from_2d_array`(``&``[`
`&``[``1.``,` `2.``]``,`
`&``[``3.``,` `4.``]``,`
`&``[``5.``,` `6.``]``,`
`&``[``7.``,` `8.``]``,`
`&``[``9.``,` `10.``]``]``)``;`
`//` Our classes are defined as a vector
`let` y `=` `vec!``[``2``,` `2``,` `2``,` `3``,` `3``]``;`
`//` Train classifier
`let` knn `=` `KNNClassifier``::`fit`(``&`x`,` `&`y`,` `Default``::`default`(``)``)``.``unwrap``(``)``;`
`//` Predict classes
`let` y_hat `=` knn`.``predict``(``&`x`)``.``unwrap``(``)``;`

## Overview

### Supported algorithms

All machine learning algorithms are grouped into these broad categories:

- Clustering, unsupervised clustering of unlabeled data.
- Matrix Decomposition, various methods for matrix decomposition.
- Linear Models, regression and classification methods where output is assumed to have linear relation to explanatory variables
- Ensemble Models, variety of regression and classification ensemble models
- Tree-based Models, classification and regression trees
- Nearest Neighbors, K Nearest Neighbors for classification and regression
- Naive Bayes, statistical classification technique based on Bayes Theorem
- SVM, support vector machines

### Linear Algebra traits system

For an introduction to

's traits system see this notebook`smartcore`

#### Dependencies

~0.7–1.6MB

~31K SLoC