Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Ridge regression 1D

Here, we implement one-dimensional Ridge Regression in several styles, using increasing levels of abstraction. It's designed as a learning path for beginners, and focuses on writing idiomatic, clear, and type-safe Rust code. We focus on minimizing this loss function:

where: is an input covariate, is the associated output, is the Ridge coefficient, is the regularization strength.

How this chapter is organized

This chapter introduces several useful concepts for Rust beginners. It is divided into four sections, each solving the same problem (1D Ridge regression) using different tools and with slightly increasing complexity.

  • The first section shows how to use basic functions and the Rust standard library to build a simple library. In particular, it shows how to manipulate vectors (Vec<f64>) and slices (&[f64]).

  • The next section explains how to solve the same problem using structs and traits to make the code more modular and extensible.

  • The third section introduces generics, allowing the code to work with different floating-point types (f32 and f64).

  • Finally, the last section goes further by using ndarray for linear algebra and incorporating additional Rust features such as optional values, pattern matching, and error handling.

If you want to implement and run this example while you read but are not familiar with Cargo yet, have a look at Cargo 101 for how to set up your project.