|
1 | 1 | # Naive Bayes
|
2 | 2 |
|
3 |
| -`linfa-bayes` aims to provide pure Rust implementations of Naive Bayes algorithms. |
| 3 | +`linfa-bayes` provides pure Rust implementations of Naive Bayes algorithms for the Linfa toolkit. |
4 | 4 |
|
5 | 5 | ## The Big Picture
|
6 | 6 |
|
|
14 | 14 |
|
15 | 15 | ## Examples
|
16 | 16 |
|
17 |
| -There is an usage example in the `examples/` directory. To run, use: |
| 17 | +You can find an example in the `examples/` directory. To run, use: |
18 | 18 |
|
19 | 19 | ```bash
|
20 |
| -$ cargo run --example winequality |
| 20 | +$ cargo run --example winequality --release |
21 | 21 | ```
|
22 | 22 |
|
23 |
| -## License |
24 |
| -Dual-licensed to be compatible with the Rust project. |
25 |
| - |
26 |
| -Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or distributed except according to those terms. |
27 |
| - |
| 23 | +<details> |
| 24 | +<summary style="cursor: pointer; display:list-item;"> |
| 25 | +Show source code |
| 26 | +</summary> |
| 27 | + |
| 28 | +```rust, no_run |
| 29 | +use linfa::metrics::ToConfusionMatrix; |
| 30 | +use linfa::traits::{Fit, Predict}; |
| 31 | +use linfa_bayes::{GaussianNb, Result}; |
| 32 | +
|
| 33 | +// Read in the dataset and convert targets to binary data |
| 34 | +let (train, valid) = linfa_datasets::winequality() |
| 35 | + .map_targets(|x| if *x > 6 { "good" } else { "bad" }) |
| 36 | + .split_with_ratio(0.9); |
| 37 | +
|
| 38 | +// Train the model |
| 39 | +let model = GaussianNb::params().fit(&train)?; |
| 40 | +
|
| 41 | +// Predict the validation dataset |
| 42 | +let pred = model.predict(&valid); |
| 43 | +
|
| 44 | +// Construct confusion matrix |
| 45 | +let cm = pred.confusion_matrix(&valid)?; |
| 46 | +
|
| 47 | +// classes | bad | good |
| 48 | +// bad | 130 | 12 |
| 49 | +// good | 7 | 10 |
| 50 | +// |
| 51 | +// accuracy 0.8805031, MCC 0.45080978 |
| 52 | +println!("{:?}", cm); |
| 53 | +println!("accuracy {}, MCC {}", cm.accuracy(), cm.mcc()); |
| 54 | +# Result::Ok(()) |
| 55 | +``` |
| 56 | +</details> |
0 commit comments