smartcorelib / smartcore

A comprehensive library for machine learning and numerical computing. The library provides a set of tools for linear algebra, numerical computing, optimization, and enables a generic, powerful yet still efficient approach to machine learning.
https://smartcorelib.org/
Apache License 2.0
698 stars 75 forks source link

Make SVC multiclass #224

Open Mec-iS opened 1 year ago

Mec-iS commented 1 year ago

I'm submitting a

Current Behaviour:

smartcore::svm::svc only works with 2 classes [-1, 1]

Expected Behaviour:

we should support n number of classes. Use Iris dataset as example. See this intro blogpost

        use crate::dataset::iris::load_dataset as iris_load;

        // Load Iris dataset
        let iris_dataset = iris_load();

        // Turn Iris dataset into NxM matrix
        // Input data
        let x: DenseMatrix<f32> = DenseMatrix::new(
            iris_dataset.num_samples,      // num rows
            iris_dataset.num_features,     // num columns
            iris_dataset.data,             // data as Vec
            false,                         // column_major
        );
        // These are our target class labels
        let y: Vec<u32> = iris_dataset.target;

        let y_hat = SVC::fit(
            &x,
            &y,
            &SVCParameters::default()
                .with_c(1.0)
                .with_kernel(&Kernels::rbf().with_gamma(0.7)),
        )
        .and_then(|lr| lr.predict(&x))
        .unwrap();

        println!("{:?}", &y_hat);