rust-ml / linfa

A Rust machine learning framework.
Apache License 2.0
3.76k stars 246 forks source link

linfa-logistic can't fit/predict very simple example #252

Closed jakkos-net closed 1 year ago

jakkos-net commented 2 years ago

I originally posted this on the rust-ml zulip here and another user suggested I open an issue on this repo as it may be a bug.


I'm trying out linfa-logistic and wanted to get a simple case running, however the output confuses me and I'm not sure if I'm doing something wrong. I was attempting to have the model just map [1.0] to 1 and [0.0] to 0, but it seems to predict [1,1,1,1]? Any help would be appreciated :)

use linfa::{traits::{Fit, Predict}, DatasetBase};
use linfa_logistic::{LogisticRegression};
use ndarray::array;

fn main() {
    let x = array![[1.0], [0.0], [1.0], [0.0]];
    let y = array![1,0,1,0];
    let dataset = DatasetBase::new(x,y);
    let model = LogisticRegression::default().fit(&dataset).unwrap();

    println!("model: {:?}", model);
    println!();
    let pred = model.predict(&dataset.records);
    println!("pred: {:?}", pred);
}
model: FittedLogisticRegression { threshold: 0.5, intercept: 0.8989829201964533, params: [0.4214817977184791], shape=[1], strides=[1], layout=CFcf (0xf), const ndim=1, labels: [ClassLabel { class: 1, label: 1.0 }, ClassLabel { class: 0, label: -1.0 }] }

pred: [1, 1, 1, 1], shape=[4], strides=[1], layout=CFcf (0xf), const ndim=1
bernardo-sb commented 2 years ago

Interesting enough changing the order of x and y yields the correct result:

let x = array![[1.0], [1.0], [0.0],  [0.0]];
let y = array![1, 1, 0, 0];
YuhanLiin commented 2 years ago

@bytesnake Are you familiar with linfa-logistic? I'm honestly not sure why it's not working.