Skip to content

Commit

Permalink
Add doc example to lib.rs
Browse files Browse the repository at this point in the history
Doc fixes
  • Loading branch information
shadeMe committed Apr 2, 2023
1 parent dd7c761 commit 22302fd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/liblinear/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! FFI bindings for liblinear.
//! FFI bindings for LIBLINEAR.

use std::os::raw::c_char;

Expand Down
42 changes: 40 additions & 2 deletions crates/liblinear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@
//!
//! `liblinear` is a Rust wrapper for the [LIBLINEAR](https://www.csie.ntu.edu.tw/~cjlin/liblinear/)
//! C/C++ machine learning library.
//!
//! ## Usage
//! ```
//! use liblinear::{
//! Model,
//! Parameters,
//! TrainingInput,
//! PredictionInput,
//! solver::L2R_LR,
//! model::traits::*,
//! parameter::traits::*,
//! solver::traits::*,
//! };
//!
//! let x: Vec<Vec<(u32, f64)>> = vec![
//! vec![(1, 0.1), (3, 0.2)],
//! vec![(3, 9.9)],
//! vec![(1, 0.2), (2, 3.2)],
//! ];
//! let y = vec![0.0, 1.0, 0.0];
//!
//! let mut params = Parameters::<L2R_LR>::default();
//! params
//! .bias(0f64)
//! .stopping_tolerance(0.1f64)
//! .constraints_violation_cost(0.1f64);
//!
//! let model = Model::train(&TrainingInput::from_sparse_features(y, x).unwrap(), &params).unwrap();
//!
//! let predicted_class = model
//! .predict(&PredictionInput::from_sparse_features(vec![(3u32, 9.9f64)]).unwrap())
//! .unwrap();
//! println!("{}",predicted_class);
//! ```
#[macro_use]
extern crate num_derive;

Expand All @@ -12,12 +46,16 @@ pub mod parameter;
pub mod solver;
pub mod util;

/// The version of the bundled liblinear C-library.
pub use model::Model;
pub use parameter::Parameters;
pub use util::{PredictionInput, TrainingInput, TrainingInstance};

/// The version of the bundled LIBLINEAR C/C++ library.
pub fn liblinear_version() -> i32 {
unsafe { ffi::liblinear_version }
}

/// Toggles the log output liblinear prints to the program's `stdout`.
/// Toggles the log output LIBLINEAR prints to the program's `stdout`.
pub fn toggle_liblinear_stdout_output(state: bool) {
unsafe {
match state {
Expand Down
3 changes: 1 addition & 2 deletions crates/liblinear/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Types and traits that wrap models.
//! Types and traits that wrap LIBLINEAR models.

use std::{ffi::CStr, marker::PhantomData};

Expand Down Expand Up @@ -130,7 +130,6 @@ pub mod traits {
pub trait NonSingleClassModel {
/// Returns the bias term corresponding to the class with the given index.
///
/// For classification models, if label index is not in a valid range, an !!!ERROR value will be returned.
/// For regression models, the label index is ignored.
fn label_bias(&self, label_index: u32) -> Result<f64, ModelError>;
}
Expand Down
6 changes: 4 additions & 2 deletions crates/liblinear/src/parameter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Types and traits that wrap hyper parameters.
//! Types and traits that wrap LIBLINEAR hyper parameters.

use std::marker::PhantomData;

Expand All @@ -23,6 +23,8 @@ pub mod traits {
/// [`SupportsInitialSolutions`](crate::solver::traits::SupportsInitialSolutions) trait.
pub trait SetInitialSolutions {
/// Set the initial solution specification.
///
/// Default: `None`
fn initial_solutions(&mut self, init_solutions: Vec<f64>) -> &mut Self;
}

Expand Down Expand Up @@ -51,7 +53,7 @@ pub mod traits {
pub trait SetRegressionLossSensitivity {
/// Set the tolerance margin/loss sensitivity of support vector regression (parameter `p`).
///
/// Default: `0.1
/// Default: `0.1`
fn regression_loss_sensitivity(&mut self, p: f64) -> &mut Self;
}
}
Expand Down

0 comments on commit 22302fd

Please sign in to comment.