Added more metrics

This commit is contained in:
Andrew Kane
2021-07-26 14:06:36 -07:00
parent a095755bb9
commit cc0024a9f9
4 changed files with 76 additions and 2 deletions

View File

@@ -1,3 +1,7 @@
## 0.1.1 (unreleased)
- Added more metrics
## 0.1.0 (2021-07-26)
- First release

View File

@@ -77,18 +77,48 @@ model.cv(&data, 5);
## Metrics
Calculate RMSE
Calculate RMSE (for real-valued MF)
```rust
model.rmse(&data);
```
Calculate MAE
Calculate MAE (for real-valued MF)
```rust
model.mae(&data);
```
Calculate generalized KL-divergence (for non-negative real-valued MF)
```rust
model.gkl(&data);
```
Calculate logarithmic loss (for binary MF)
```rust
model.logloss(&data);
```
Calculate accuracy (for binary MF)
```rust
model.accuracy(&data);
```
Calculate MPR (for one-class MF)
```rust
model.mpr(&data, transpose);
```
Calculate AUC (for one-class MF)
```rust
model.auc(&data, transpose);
```
## Parameters
Set parameters - default values below

View File

@@ -60,4 +60,9 @@ extern "C" {
pub fn mf_predict(model: *const MfModel, u: c_int, v: c_int) -> c_float;
pub fn calc_rmse(prob: *const MfProblem, model: *const MfModel) -> c_double;
pub fn calc_mae(prob: *const MfProblem, model: *const MfModel) -> c_double;
pub fn calc_gkl(prob: *const MfProblem, model: *const MfModel) -> c_double;
pub fn calc_logloss(prob: *const MfProblem, model: *const MfModel) -> c_double;
pub fn calc_accuracy(prob: *const MfProblem, model: *const MfModel) -> c_double;
pub fn calc_mpr(prob: *const MfProblem, model: *const MfModel, tranpose: bool) -> c_double;
pub fn calc_auc(prob: *const MfProblem, model: *const MfModel, tranpose: bool) -> c_double;
}

View File

@@ -117,6 +117,36 @@ impl Model {
unsafe { calc_mae(&prob, self.model) }
}
pub fn gkl(&self, data: &Matrix) -> f64 {
assert!(self.is_fit());
let prob = data.to_problem();
unsafe { calc_gkl(&prob, self.model) }
}
pub fn logloss(&self, data: &Matrix) -> f64 {
assert!(self.is_fit());
let prob = data.to_problem();
unsafe { calc_logloss(&prob, self.model) }
}
pub fn accuracy(&self, data: &Matrix) -> f64 {
assert!(self.is_fit());
let prob = data.to_problem();
unsafe { calc_accuracy(&prob, self.model) }
}
pub fn mpr(&self, data: &Matrix, tranpose: bool) -> f64 {
assert!(self.is_fit());
let prob = data.to_problem();
unsafe { calc_mpr(&prob, self.model, tranpose) }
}
pub fn auc(&self, data: &Matrix, tranpose: bool) -> f64 {
assert!(self.is_fit());
let prob = data.to_problem();
unsafe { calc_auc(&prob, self.model, tranpose) }
}
fn with_model(model: *const MfModel) -> Self {
let param = unsafe { mf_get_default_param() };
Self {
@@ -227,6 +257,11 @@ mod tests {
assert!(model.rmse(&data) < 0.15);
assert!(model.mae(&data) < 0.15);
assert!(model.gkl(&data) < 0.01);
assert!(model.logloss(&data) < 0.3);
assert_eq!(1.0, model.accuracy(&data));
assert_eq!(0.0, model.mpr(&data, false));
assert_eq!(1.0, model.auc(&data, false));
}
#[test]