1<!DOCTYPE html> 2<html lang="en-US"> 3 <head> 4 5 <!-- Global site tag (gtag.js) - Google Analytics -->
vendor: 64 bytes, lines 5-6
5 6<script async src="https://www.googletagmanager.com/gtag/js?id=
6G-D1L0JVQB04
vendor: 12 bytes, line 6
6"></script>
7<script> 8
vendor: 134 bytes, lines 8-12
8window.dataLayer = window.dataLayer || []; 9 function gtag(){dataLayer.push(arguments);} 10 gtag('js', new Date()); 11 12 gtag('config', '
12G-D1L0JVQB04
vendor: 4 bytes, line 12
12');
13</script>
13 14 15 16 <meta charset="utf-8"> 17 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 18 <title>Getting Started - SmartCore</title> 19 20 <link rel="apple-touch-icon-precomposed" sizes="57x57" href="/assets/favicon/apple-touch-icon-57x57.png" /> 21<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/assets/favicon/apple-touch-icon-114x114.png" /> 22<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/assets/favicon/apple-touch-icon-72x72.png" /> 23<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/assets/favicon/apple-touch-icon-144x144.png" /> 24<link rel="apple-touch-icon-precomposed" sizes="60x60" href="/assets/favicon/apple-touch-icon-60x60.png" /> 25<link rel="apple-touch-icon-precomposed" sizes="120x120" href="/assets/favicon/apple-touch-icon-120x120.png" /> 26<link rel="apple-touch-icon-precomposed" sizes="76x76" href="/assets/favicon/apple-touch-icon-76x76.png" /> 27<link rel="apple-touch-icon-precomposed" sizes="152x152" href="/assets/favicon/apple-touch-icon-152x152.png" /> 28<link rel="icon" type="image/png" href="/assets/favicon/favicon-196x196.png" sizes="196x196" /> 29<link rel="icon" type="image/png" href="/assets/favicon/favicon-96x96.png" sizes="96x96" /> 30<link rel="icon" type="image/png" href="/assets/favicon/favicon-32x32.png" sizes="32x32" /> 31<link rel="icon" type="image/png" href="/assets/favicon/favicon-16x16.png" sizes="16x16" /> 32<link rel="icon" type="image/png" href="/assets/favicon/favicon-128.png" sizes="128x128" /> 33<meta name="application-name" content=" "/> 34<meta name="msapplication-TileColor" content="#FFFFFF" /> 35<meta name="msapplication-TileImage" content="/assets/favicon/mstile-144x144.png" /> 36<meta name="msapplication-square70x70logo" content="/assets/favicon/mstile-70x70.png" /> 37<meta name="msapplication-square150x150logo" content="/assets/favicon/mstile-150x150.png" /> 38<meta name="msapplication-wide310x150logo" content="/assets/favicon/mstile-310x150.png" /> 39<meta name="msapplication-square310x310logo" content="/assets/favicon/mstile-310x310.png" /> 40 41 42 <link rel="stylesheet" href="/assets/css/main.css"> 43 <link rel="stylesheet" href="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.css"/> 44 <link rel="shortcut icon" type="image/png" href="/favicon.png"> 45 <!-- Begin Jekyll SEO tag v2.8.0 --> 46<title>Getting Started | SmartCore</title> 47<meta name="generator" content="Jekyll v3.10.0" /> 48<meta property="og:title" content="Getting Started" /> 49<meta name="author" content="SmartCore Developers" /> 50<meta property="og:locale" content="en_US" /> 51<meta name="description" content="Quick introduction to Smartcore capabilities. Fit your first machine learning algorithm in Rust, learn Smartcore API and core concepts." /> 52<meta property="og:description" content="Quick introduction to Smartcore capabilities. Fit your first machine learning algorithm in Rust, learn Smartcore API and core concepts." /> 53<link rel="canonical" href="https://smartcorelib.github.io/user_guide/quick_start.html" /> 54<meta property="og:url" content="https://smartcorelib.github.io/user_guide/quick_start.html" /> 55<meta property="og:site_name" content="SmartCore" /> 56<meta property="og:type" content="website" /> 57<meta name="twitter:card" content="summary" /> 58<meta property="twitter:title" content="Getting Started" />
59<script type="application/ld+json"> 60{"@context":"https://schema.org","@type":"WebPage","author":{"@type":"Person","name":"SmartCore Developers"},"description":"Quick introduction to Smartcore capabilities. Fit your first machine learning algorithm in Rust, learn Smartcore API and core concepts.","headline":"Getting Started","url":"https://smartcorelib.github.io/user_guide/quick_start.html"}</script>
60 61<!-- End Jekyll SEO tag --> 62 63 </head> 64 <body> 65 <nav class="navbar navbar-expand-md navbar-dark bg-dark"> 66 <div class="container"> 67 <a class="navbar-brand text-primary font-weight-bold mt-1" href="/"> 68 <img src="/assets/logo/smartcore.png" width="50" height="50" class="d-inline-block align-middle" alt=""> 69 SmartCore 70 </a> 71 <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"> 72 <span class="navbar-toggler-icon"></span> 73 </button> 74 <div class="collapse navbar-collapse" id="navbarNav"> 75 <ul class="navbar-nav mr-auto"> 76 77 <li class="nav-item active"> 78 79 <a class="nav-link font-weight-bold align-middle" href="/user_guide/quick_start.html">Quick Start</a> 80 81 </li> 82 83 <li class="nav-item "> 84 85 <a class="nav-link font-weight-bold align-middle" href="https://docs.rs/smartcore/0.6/smartcore/">API</a> 86 87 </li> 88 89 <li class="nav-item "> 90 91 <a class="nav-link font-weight-bold align-middle" href="https://github.com/smartcorelib/smartcore-jupyter">Examples</a> 92 93 </li> 94 95 <li class="nav-item "> 96 97 <a class="nav-link font-weight-bold align-middle" href="/user_guide/benchmarks.html">Benchmarks</a> 98 99 </li> 100 101 <li class="nav-item dropdown"> 102 <a class="nav-link dropdown-toggle font-weight-bold align-middle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 103 User Guide 104 </a> 105 <div class="dropdown-menu" aria-labelledby="navbarDropdown"> 106 107 108 <a class="dropdown-item" href="/user_guide/quick_start.html">Getting Started</a> 109 110 111 112 <a class="dropdown-item" href="/user_guide/supervised.html">Supervised Learning</a> 113 114 115 116 <a class="dropdown-item" href="/user_guide/unsupervised.html">Unsupervised Learning</a> 117 118 119 120 <a class="dropdown-item" href="/user_guide/preprocessing.html">Preprocessing</a> 121 122 123 124 <a class="dropdown-item" href="/user_guide/model_selection.html">Model Selection</a> 125 126 127 128 <a class="dropdown-item" href="/user_guide/benchmarks.html">Benchmarks</a> 129 130 131 132 <a class="dropdown-item" href="/user_guide/developer.html">Developer's Guide</a> 133 134 135 </div> 136 </li> 137 </ul> 138 <ul class="navbar-nav ml-auto"> 139 140 <li class="nav-item "> 141 142 <a class="nav-link font-weight-bold align-middle" href="/about.html">About Us</a> 143 144 </li> 145 146 <li class="nav-item"> 147 <a class="nav-link" href="https://github.com/smartcorelab/smartcore"> 148 <span><i class="fab fa-github" aria-hidden="true"></i></span> 149 </a> 150 </li> 151 </ul> 152 </div> 153 </div> 154</nav> 155 156 <div class="container"> 157 158 <div class="row flex-xl-nowrap"> 159 160 <div class="col-md-3"> 161 <div class=" bg-light rounded mt-3 p-3 text-center font-weight-bold text-muted"> 162 SmartCore 0.6 163 <br/> 164 <a class="align-bottom font-weight-light" href="/user_guide/versions.html">other version</a> 165 </div> 166 <nav id="toc" class="sticky-top bg-light rounded mt-3 p-3"></nav> 167 </div> 168 169 <div manual="true" class="col-md-9 mt-3 text-justify" markdown="1"> 170 171 <h1 id="getting-started">Getting Started</h1> 172 173<p>Welcome to <em>SmartCore</em>, the most advanced machine learning library in Rust! In <em>SmartCore</em> you will find cutting-edge implementations of the most core machine learning (ML) algorithms and tools, like:</p> 174 175<ul> 176 <li><strong>Regression</strong>: Linear Regression (OLS), LASSO, Ridge Regression and Elastic Net, Decision Tree, Random Forest, Extra Trees, K Nearest Neighbors, Support Vector Regressor</li> 177 <li><strong>Classification</strong>: Logistic Regression, Decision Tree, Random Forest, Supervised Nearest Neighbors (KNN), Support Vector Classifier, Naive Bayes</li> 178 <li><strong>Clustering</strong>: K-Means, DBSCAN, Agglomerative Clustering</li> 179 <li><strong>Matrix Decomposition</strong>: PCA, LU, QR, SVD, EVD</li> 180 <li><strong>Distance Metrics</strong>: Euclidian, Minkowski, Manhattan, Hamming, Mahalanobis, Cosine, Jaccard</li> 181 <li><strong>Evaluation Metrics</strong>: Accuracy, AUC, Recall, Precision, F1, Mean Absolute Error, Mean Squared Error, R2</li> 182</ul> 183 184<p>All of these algorithms are implemented in Rust (edition 2024, MSRV 1.85).</p> 185 186<p>
186Why another machine learning library for Rust, you might ask? While there are at least three <a href="http://www.arewelearningyet.com/">general-purpose ML libraries</a> for Rust, 187most of these libraries either do not support all of the algorithms that are implemented in <em>SmartCore</em> or arenât integrated with <a href="https://github.com/rust-ndarray/ndarray">ndarray</a>. 188All algorithms in <em>SmartCore</em> works well with <a href="https://docs.rs/ndarray">ndarray</a>. You can also use standard Rust vectors with all of the algorithms implemented here if you prefer to have minimum number of dependencies in your code.</p> 189 190<h2 id="quick-start">Quick start</h2> 191 192<p>To start using <em>SmartCore</em> simply add the following line to your <code class="language-plaintext highlighter-rouge">Cargo.toml</code> file:</p> 193 194<div class="language-toml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">[dependencies]</span> 195<span class="py">smartcore</span> <span class="p">=</span> <span class="s">"0.6"</span> 196</code></pre></div></div> 197 198<p>By default no features are enabled. To use the built-in toy datasets (and the examples on this site), enable the <code class="language-plaintext highlighter-rouge">datasets</code> feature:</p> 199 200<div class="language-toml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">[dependencies]</span> 201<span class="nn">smartcore</span> <span class="o">=</span> <span class="p">{</span> <span class="py">version</span> <span class="p">=</span> <span class="s">"0.6"</span><span class="p">,</span> <span class="py">features</span> <span class="p">=</span> <span class="nn">["datasets"]</span> <span class="p">}</span> 202</code></pre></div></div> 203 204<p>You will also have to decide which linear algebra library to use with <em>SmartCore</em>. We support:</p> 205<ul> 206 <li><a href="https://docs.rs/ndarray">ndarray</a> - provides an n-dimensional container for general elements and for numerics.</li> 207</ul> 208 209<p>If you prefer not to depend on any external library other than <em>SmartCore</em> you can use any algorithms implemented here with Rust <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html">Vec</a> and <a href="https://doc.rust-lang.org/std/primitive.array.html">array</a>.</p> 210 211<h3 id="iris-flower-classification">Iris Flower Classification</h3> 212 213<p>Letâs train our first machine learning algorithm using <a href="https://archive.ics.uci.edu/ml/datasets/iris">Iris dataset</a>. The Iris Dataset was introduced by the Ronald Fisher and is by far the best known database to be found in the pattern recognition literature. It consists of 50 samples from each of three species of Iris (Iris setosa, Iris virginica and Iris versicolor). Four features were measured from each sample: the length and the width of the sepals and petals, in centimeters. This dataset is often used in classification and clustering examples. First letâs fit <a href="https://docs.rs/smartcore/0.6/smartcore/neighbors/index.html">K-Nearest Neighbors</a> algorithm to this dataset:</p> 214 215<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">dataset</span><span class="p">::</span><span class="nn">iris</span><span class="p">::</span><span class="n">load_dataset</span><span class="p">;</span> 216<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">linalg</span><span class="p">::</span><span class="nn">basic</span><span class="p">::</span><span class="nn">matrix</span><span class="p">::</span><span class="n">DenseMatrix</span><span class="p">;</span> 217<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">neighbors</span><span class="p">::</span><span class="nn">knn_classifier</span><span class="p">::</span><span class="n">KNNClassifier</span><span class="p">;</span> 218<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">metrics</span><span class="p">::</span><span class="n">accuracy</span><span class="p">;</span> 219 220<span class="c1">// Load Iris dataset</span> 221<span class="k">let</span> <span class="n">iris_data</span> <span class="o">=</span> <span class="nf">load_dataset</span><span class="p">();</span> 222<span class="c1">// Turn Iris dataset into NxM matrix</span> 223<span class="k">let</span> <span class="n">rows</span><span class="p">:</span> <span class="nb">Vec</span><span class="o"><</span><span class="nb">Vec</span><span class="o"><</span><span class="nb">f32</span><span class="o">>></span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="o">..</span><span class="n">iris_data</span><span class="py">.num_samples</span><span class="p">)</span> 224 <span class="nf">.map</span><span class="p">(|</span><span class="n">r</span><span class="p">|</span> <span class="n">iris_data</span><span class="py">.data</span><span class="p">[</span><span class="n">r</span> <span class="o">*</span> <span class="n">iris_data</span><span class="py">.num_features</span><span class="o">..</span><span class="p">(</span><span class="n">r</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">iris_data</span><span class="py">.num_features</span><span class="p">]</span><span class="nf">.to_vec</span><span class="p">())</span> 225 <span class="nf">.collect</span><span class="p">();</span> 226<span class="k">let</span> <span class="n">x</span> <span class="o">=</span> <span class="nn">DenseMatrix</span><span class="p">::</span><span class="nf">from_2d_vec</span><span class="p">(</span><span class="o">&</span><span class="n">rows</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> 227<span class="c1">// These are our target class labels</span> 228<span class="k">let</span> <span class="n">y</span> <span class="o">=</span> <span class="n">iris_data</span><span class="py">.target</span><span class="p">;</span> 229<span class="c1">// Fit KNN classifier to Iris dataset</span> 230<span class="k">let</span> <span class="n">knn</span> <span class="o">=</span> <span class="nn">KNNClassifier</span><span class="p">::</span><span class="nf">fit</span><span class="p">(</span><span class="o">&</span><span class="n">x</span><span class="p">,</span> <span class="o">&</span><span class="n">y</span><span class="p">,</span> <span class="nn">Default</span><span class="p">::</span><span class="nf">default</span><span class="p">())</span><span class="nf">.unwrap</span><span class="p">();</span> 231<span class="k">let</span> <span class="n">y_hat</span> <span class="o">=</span> <span class="n">knn</span><span class="nf">.predict</span><span class="p">(</span><span class="o">&</span><span class="n">x</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> <span class="c1">// Predict class labels</span> 232<span class="c1">// Calculate training error</span> 233<span class="nd">println!</span><span class="p">(</span><span class="s">"accuracy: {}"</span><span class="p">,</span> <span class="nf">accuracy</span><span class="p">(</span><span class="o">&</span><span class="n">y</span><span class="p">,</span> <span class="o">&</span><span class="n">y_hat</span><span class="p">));</span> <span class="c1">// Prints 0.96</span> 234</code></pre></div></div> 235 236<p>Now, if you want to fit <a href="https://docs.rs/smartcore/0.6/smartcore/linear/index.html">Logistic Regression</a>
236 to your data you donât have to change your code a lot:</p> 237 238<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">dataset</span><span class="p">::</span><span class="nn">iris</span><span class="p">::</span><span class="n">load_dataset</span><span class="p">;</span> 239<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">linalg</span><span class="p">::</span><span class="nn">basic</span><span class="p">::</span><span class="nn">matrix</span><span class="p">::</span><span class="n">DenseMatrix</span><span class="p">;</span> 240<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">linear</span><span class="p">::</span><span class="nn">logistic_regression</span><span class="p">::</span><span class="n">LogisticRegression</span><span class="p">;</span> 241<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">metrics</span><span class="p">::</span><span class="n">accuracy</span><span class="p">;</span> 242 243<span class="c1">// Load Iris dataset</span> 244<span class="k">let</span> <span class="n">iris_data</span> <span class="o">=</span> <span class="nf">load_dataset</span><span class="p">();</span> 245<span class="c1">// Turn Iris dataset into NxM matrix</span> 246<span class="k">let</span> <span class="n">rows</span><span class="p">:</span> <span class="nb">Vec</span><span class="o"><</span><span class="nb">Vec</span><span class="o"><</span><span class="nb">f32</span><span class="o">>></span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="o">..</span><span class="n">iris_data</span><span class="py">.num_samples</span><span class="p">)</span> 247 <span class="nf">.map</span><span class="p">(|</span><span class="n">r</span><span class="p">|</span> <span class="n">iris_data</span><span class="py">.data</span><span class="p">[</span><span class="n">r</span> <span class="o">*</span> <span class="n">iris_data</span><span class="py">.num_features</span><span class="o">..</span><span class="p">(</span><span class="n">r</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">iris_data</span><span class="py">.num_features</span><span class="p">]</span><span class="nf">.to_vec</span><span class="p">())</span> 248 <span class="nf">.collect</span><span class="p">();</span> 249<span class="k">let</span> <span class="n">x</span> <span class="o">=</span> <span class="nn">DenseMatrix</span><span class="p">::</span><span class="nf">from_2d_vec</span><span class="p">(</span><span class="o">&</span><span class="n">rows</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> 250<span class="c1">// These are our target class labels</span> 251<span class="k">let</span> <span class="n">y</span> <span class="o">=</span> <span class="n">iris_data</span><span class="py">.target</span><span class="p">;</span> 252<span class="c1">// Fit Logistic Regression to Iris dataset</span> 253<span class="k">let</span> <span class="n">lr</span> <span class="o">=</span> <span class="nn">LogisticRegression</span><span class="p">::</span><span class="nf">fit</span><span class="p">(</span><span class="o">&</span><span class="n">x</span><span class="p">,</span> <span class="o">&</span><span class="n">y</span><span class="p">,</span> <span class="nn">Default</span><span class="p">::</span><span class="nf">default</span><span class="p">())</span><span class="nf">.unwrap</span><span class="p">();</span> 254<span class="k">let</span> <span class="n">y_hat</span> <span class="o">=</span> <span class="n">lr</span><span class="nf">.predict</span><span class="p">(</span><span class="o">&</span><span class="n">x</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> <span class="c1">// Predict class labels</span> 255<span class="c1">// Calculate training error</span> 256<span class="nd">println!</span><span class="p">(</span><span class="s">"accuracy: {}"</span><span class="p">,</span> <span class="nf">accuracy</span><span class="p">(</span><span class="o">&</span><span class="n">y</span><span class="p">,</span> <span class="o">&</span><span class="n">y_hat</span><span class="p">));</span> <span class="c1">// Prints 0.98</span> 257</code></pre></div></div> 258 259<p>Our performance metric (accuracy) went up two percentage points! Nice work!</p> 260 261<h2 id="high-level-overview">High-level overview</h2> 262 263<p>Majority of machine learning algorithms rely on linear algebra routines and optimization methods to fit a model to a dataset or to make a prediction from new data. There are many crates for linear algebra and optimization in Rust but SmartCore does not has a hard dependency on any of these crates. Instead, machine learning algorithms in <em>SmartCore</em> use an abstraction layer where operations on multidimensional arrays and maximization/minimization routines are defined. This approach allows us to quickly integrate with any new type of matrix or vector as long as it implements all abstract methods from this layer.</p> 264 265<p>Functions from optimization module are not available directly but we plan to make optimization library public once it is mature enough.</p> 266 267<p>While functions from <a href="https://docs.rs/smartcore/0.6/smartcore/linalg/index.html">linear algebra module</a> are public you should not use them directly because this module is still unstable. We keep this interface open to let anyone add implementations of other types of matrices that are currently not supported by <em>SmartCore</em>. Please see <a href="/user_guide/developer.html">
267Developerâs Guide</a> if you want to add your favourite matrix type to <em>SmartCore</em>.</p> 268 269<p>The library is structured in three layers:</p> 270 271<ol> 272 <li><strong>Abstract linear algebra</strong> (<code class="language-plaintext highlighter-rouge">linalg::basic::arrays</code>, <code class="language-plaintext highlighter-rouge">linalg::traits</code>): the <code class="language-plaintext highlighter-rouge">Array</code>, <code class="language-plaintext highlighter-rouge">Array1</code>, <code class="language-plaintext highlighter-rouge">Array2</code>, <code class="language-plaintext highlighter-rouge">ArrayView1</code>, <code class="language-plaintext highlighter-rouge">ArrayView2</code> traits define all matrix and vector operations used by <em>SmartCore</em>. The <code class="language-plaintext highlighter-rouge">linalg::traits</code> module hosts decomposition traits (<code class="language-plaintext highlighter-rouge">SVDDecomposable</code>, <code class="language-plaintext highlighter-rouge">QRDecomposable</code>, <code class="language-plaintext highlighter-rouge">LUDecomposable</code>, <code class="language-plaintext highlighter-rouge">EVDDecomposable</code>, <code class="language-plaintext highlighter-rouge">CholeskyDecomposable</code>) and statistical/pre-processing helpers.</li> 273 <li><strong>Machine learning algorithms</strong> (<code class="language-plaintext highlighter-rouge">linear</code>, <code class="language-plaintext highlighter-rouge">tree</code>, <code class="language-plaintext highlighter-rouge">ensemble</code>, <code class="language-plaintext highlighter-rouge">svm</code>, <code class="language-plaintext highlighter-rouge">neighbors</code>, <code class="language-plaintext highlighter-rouge">naive_bayes</code>, <code class="language-plaintext highlighter-rouge">cluster</code>, <code class="language-plaintext highlighter-rouge">decomposition</code>, <code class="language-plaintext highlighter-rouge">preprocessing</code>): defined on top of the abstract layer.</li> 274 <li><strong>Model evaluation and selection</strong> (<code class="language-plaintext highlighter-rouge">metrics</code>, <code class="language-plaintext highlighter-rouge">model_selection</code>): used to optimize and measure performance of statistical models.</li> 275</ol> 276 277<h3 id="api">API</h3> 278 279<p>All algorithms in <em>SmartCore</em> implement the same interface when it comes to fitting an algorithm to your dataset or making a prediction from new data. All core interfaces are defined in the <a href="https://docs.rs/smartcore/0.6/smartcore/api/index.html">api module</a>.</p> 280 281<p>There is a static function <code class="language-plaintext highlighter-rouge">fit</code> that fits an algorithm to your data. This function is defined in two places, <a href="https://docs.rs/smartcore/0.6/smartcore/api/trait.SupervisedEstimator.html"><code class="language-plaintext highlighter-rouge">SupervisedEstimator</code></a> and <a href="https://docs.rs/smartcore/0.6/smartcore/api/trait.UnsupervisedEstimator.html"><code class="language-plaintext highlighter-rouge">UnsupervisedEstimator</code></a>, one is used for supervised learning and another for unsupervised learning. Both estimators take your training data and hyperparameters for the algorithm and produce a fully trained instance of the estimator. The only difference between these two traits is that <code class="language-plaintext highlighter-rouge">SupervisedEstimator</code> requires training target values in addition to training predictors to fit an algorithm to your data.</p> 282 283<p>A function <code class="language-plaintext highlighter-rouge">predict</code> is defined in the <a href="https://docs.rs/smartcore/0.6/smartcore/api/trait.Predictor.html"><code class="language-plaintext highlighter-rouge">Predictor</code></a> trait and is used to predict labels or target values from new data. All mandatory parameters of the model are declared as parameters of function <code class="language-plaintext highlighter-rouge">fit</code>. All optional parameters are hidden behind <code class="language-plaintext highlighter-rouge">Default::default()</code>.</p> 284 285<p>Some estimators (notably <code class="language-plaintext highlighter-rouge">SVC</code> and <code class="language-plaintext highlighter-rouge">SVR</code>) borrow their parameters instead of owning them â they implement <a href="https://docs.rs/smartcore/0.6/smartcore/api/trait.SupervisedEstimatorBorrow.html"><code class="language-plaintext highlighter-rouge">SupervisedEstimatorBorrow</code></a> and take <code class="language-plaintext highlighter-rouge">&parameters</code> in <code class="language-plaintext highlighter-rouge">fit</code>. See the <a href="/user_guide/supervised.html">supervised learning guide</a> for concrete examples.</p> 286 287<p>The <a href="https://docs.rs/smartcore/0.6/smartcore/api/trait.Transformer.html"><code class="language-plaintext highlighter-rouge">Transformer</code></a> trait is the unsupervised analogue for algorithms that transform (rather than predict from) input data, used by <code class="language-plaintext highlighter-rouge">PCA</code>, <code class="language-plaintext highlighter-rouge">SVD</code>, and the <code class="language-plaintext highlighter-rouge">preprocessing</code> module.</p> 288 289<h3 id="input-and-output">Input and Output</h3> 290 291<p>Algorithms in <em>SmartCore</em> take two dimensional arrays (matrices) and vectors as input and produce matrices and vectors on output, as demonstrated in Figure 2.</p> 292 293<figure class="image" align="center"> 294 <img src="/assets/imgs/io_format.svg" alt="Input and output format." class="img-fluid" /> 295 <figcaption>Figure 1. Input and output as matrices and vectors where <em>N</em> is number of samples and <em>M</em> is number of features in each sample.</figcaption> 296</figure> 297 298<p><strong>X</strong> is a <em>NxM</em> matrix with your training data. Training data comes as a set of samples of length <em>N</em>. Each sample has <em>M</em> features. For supervised learning, you also provide a vector of training labels, <strong>y</strong> that should have length <em>N</em>. Traits <a href="https://docs.rs/smartcore/0.6/smartcore/linalg/basic/arrays/trait.Array2.html"><code class="language-plaintext highlighter-rouge">Array2</code></a> and <a href="https://docs.rs/smartcore/0.6/smartcore/linalg/basic/arrays/trait.Array1.html"><code class="language-plaintext highlighter-rouge">Array1</code></a> are where all matrix and vector operations used by <em>SmartCore</em> are defined.</p> 299 300<h2 id="linear-algebra-libraries">Linear algebra libraries</h2> 301 302<p>All functions in <em>SmartCore</em> work well with ordinary Rustâs vectors but we recommend using more advanced and faster crates for linear algebra, such as <a href="https://docs.rs/ndarray">ndarray</a>.</p> 303 304<h3 id="ndarray">ndarray</h3> 305 306<p>To enable <code class="language-plaintext highlighter-rouge">ndarray</code> add <code class="language-plaintext highlighter-rouge">ndarray-bindings</code> compilation feature to your <code class="language-plaintext highlighter-rouge">Cargo.toml</code> file:</p> 307 308<div class="language-toml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">[dependencies]</span> 309<span class="nn">smartcore</span> <span class="o">=</span> <span class="p">{</span> <span class="py">version</span> <span class="p">=</span> <span class="s">"0.6"</span><span class="p">,</span> <span class="py">features</span> <span class="p">=</span> <span class="p">[</span><span class="s">"datasets"</span><span class="p">,</span> <span class="s">"ndarray-bindings"</span><span class="p">]</span> <span class="p">}</span> 310</code></pre></div></div> 311 312<p>To fit Logistic Regression to Iris dataset using <code class="language-plaintext highlighter-rouge">ndarray</code>:</p> 313 314<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">
314use</span> <span class="nn">ndarray</span><span class="p">::</span><span class="n">Array</span><span class="p">;</span> 315<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">dataset</span><span class="p">::</span><span class="nn">iris</span><span class="p">::</span><span class="n">load_dataset</span><span class="p">;</span> 316<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">linear</span><span class="p">::</span><span class="nn">logistic_regression</span><span class="p">::</span><span class="n">LogisticRegression</span><span class="p">;</span> 317<span class="k">use</span> <span class="nn">smartcore</span><span class="p">::</span><span class="nn">metrics</span><span class="p">::</span><span class="n">accuracy</span><span class="p">;</span> 318 319<span class="c1">// Load Iris dataset</span> 320<span class="k">let</span> <span class="n">iris_data</span> <span class="o">=</span> <span class="nf">load_dataset</span><span class="p">();</span> 321<span class="c1">// Turn Iris dataset into NxM matrix</span> 322<span class="k">let</span> <span class="n">x</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">::</span><span class="nf">from_shape_vec</span><span class="p">(</span> 323 <span class="p">(</span><span class="n">iris_data</span><span class="py">.num_samples</span><span class="p">,</span> <span class="n">iris_data</span><span class="py">.num_features</span><span class="p">),</span> 324 <span class="n">iris_data</span><span class="py">.data</span><span class="p">,</span> 325<span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> 326<span class="c1">// These are our target class labels</span> 327<span class="k">let</span> <span class="n">y</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">::</span><span class="nf">from_shape_vec</span><span class="p">(</span><span class="n">iris_data</span><span class="py">.num_samples</span><span class="p">,</span> <span class="n">iris_data</span><span class="py">.target</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> 328<span class="c1">// Fit Logistic Regression to Iris dataset</span> 329<span class="k">let</span> <span class="n">lr</span> <span class="o">=</span> <span class="nn">LogisticRegression</span><span class="p">::</span><span class="nf">fit</span><span class="p">(</span><span class="o">&</span><span class="n">x</span><span class="p">,</span> <span class="o">&</span><span class="n">y</span><span class="p">,</span> <span class="nn">Default</span><span class="p">::</span><span class="nf">default</span><span class="p">())</span><span class="nf">.unwrap</span><span class="p">();</span> 330<span class="k">let</span> <span class="n">y_hat</span> <span class="o">=</span> <span class="n">lr</span><span class="nf">.predict</span><span class="p">(</span><span class="o">&</span><span class="n">x</span><span class="p">)</span><span class="nf">.unwrap</span><span class="p">();</span> <span class="c1">// Predict class labels</span> 331<span class="c1">// Calculate training error</span> 332<span class="nd">println!</span><span class="p">(</span><span class="s">"accuracy: {}"</span><span class="p">,</span> <span class="nf">accuracy</span><span class="p">(</span><span class="o">&</span><span class="n">y</span><span class="p">,</span> <span class="o">&</span><span class="n">y_hat</span><span class="p">));</span> <span class="c1">// Prints 0.98</span> 333</code></pre></div></div> 334 335<p>The only difference between this example and previous examples are lines where <code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">y</code> variables are defined.</p> 336 337<h2 id="what-can-i-do-next">What can I do next?</h2> 338 339<p>If you are done reading through this page we would recommend to go to a specific section that interests you most. Userâs manual is organized into these broad categories:</p> 340<ul> 341 <li><a href="/user_guide/supervised.html">Supervised Learning</a>, in this section you will find tree-based, linear, SVM and KNN models.</li> 342 <li><a href="/user_guide/unsupervised.html">Unsupervised Learning</a>, unsupervised methods like clustering and matrix decomposition methods.</li> 343 <li><a href="/user_guide/preprocessing.html">Preprocessing</a>, encoding categorical data and scaling numerical features.</li> 344 <li><a href="/user_guide/model_selection.html">Model Selection</a>, various metrics for model evaluation.</li> 345 <li><a href="/user_guide/benchmarks.html">Benchmarks</a>, live trend charts tracking wall-clock time and deterministic instruction-count regressions.</li> 346 <li><a href="/user_guide/developer.html">
346Developerâs Guide</a>, would you like to contribute? Here you will find useful guidelines and rubrics to consider.</li> 347</ul> 348 349 350 <footer class="my-md-5 pt-md-5 border-top"> 351 <div class="row justify-content-md-center"> 352 <div class="col-md-auto"> 353 <div class="row"> 354 <img class="mr-1" alt="License Apache 2.0" src="https://img.shields.io/github/license/smartcorelib/smartcore"> 355 <img class="mr-1" alt="Build status" src="https://img.shields.io/circleci/build/github/smartcorelib/smartcore"> 356 <img class="mr-1" alt="Code coverage" src="https://img.shields.io/codecov/c/github/smartcorelib/smartcore"> 357 <img class="mr-1" alt="Latest version" src="https://img.shields.io/crates/v/smartcore"> 358 </div> 359 </div> 360 <div class="col"> 361 <div class="d-block mb-3 text-muted text-right"> 362 MIT/APACHE-2.0 © 2020-2026 SmartCore developers 363 </div> 364 </div> 365 </div> 366</footer> 367 368 </div> 369 </div> 370 </div> 371
371<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
371 372
372<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
372 373
373<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
373 374
374<script src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script>
374 375
375<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
375 376
376<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
376 377
377<script src="/assets/js/main.js"></script>
377 378 </body> 379</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.