mllite / ml2cpp

Machine Learning Models Deployment using C++ Code Generation
BSD 3-Clause "New" or "Revised" License
6 stars 1 forks source link

ml2cpp step 22 : Outlier Detection #33

Closed antoinecarme closed 3 years ago

antoinecarme commented 3 years ago

sklearn.svm.OneClassSVM sklearn.covariance.EllipticEnvelope

Follow the six steps described in #1

antoinecarme commented 3 years ago

Added a OneClassSVM outlier detection model.

https://github.com/antoinecarme/ml2cpp/blob/master/doc/OutlierDetection/ml2cpp_onelcass_svm_outlier_detection_iris.ipynb

antoinecarme commented 3 years ago

OneClassSVM C++ sample code:

namespace  {

    std::vector<std::any> lProblem_data_dual = { 
     1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 0.9345504106360158 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 0.06544958936398419 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 ,};

    std::vector<std::vector<std::any> > lProblem_data_sv = { 
     { 5.1, 3.5, 1.4, 0.2 },
     { 4.9, 3.0, 1.4, 0.2 },
     { 4.7, 3.2, 1.3, 0.2 },
     { 4.6, 3.1, 1.5, 0.2 },
     { 5.0, 3.6, 1.4, 0.2 },
     { 4.6, 3.4, 1.4, 0.3 },
     { 4.4, 2.9, 1.4, 0.2 },
     { 4.9, 3.1, 1.5, 0.1 },
     { 5.4, 3.7, 1.5, 0.2 },
     { 4.8, 3.4, 1.6, 0.2 },
     { 4.8, 3.0, 1.4, 0.1 },
     { 4.3, 3.0, 1.1, 0.1 },
     { 5.8, 4.0, 1.2, 0.2 },
     { 5.7, 4.4, 1.5, 0.4 },
     { 5.4, 3.9, 1.3, 0.4 },
     { 5.1, 3.5, 1.4, 0.3 },
     { 5.7, 3.8, 1.7, 0.3 },
     { 5.1, 3.8, 1.5, 0.3 },
     { 4.6, 3.6, 1.0, 0.2 },
     { 5.2, 3.4, 1.4, 0.2 },
     { 4.7, 3.2, 1.6, 0.2 },
     { 4.8, 3.1, 1.6, 0.2 },
     { 5.2, 4.1, 1.5, 0.1 },
     { 5.5, 4.2, 1.4, 0.2 },
     { 4.9, 3.1, 1.5, 0.2 },
     { 5.0, 3.2, 1.2, 0.2 },
     { 5.5, 3.5, 1.3, 0.2 },
     { 4.9, 3.6, 1.4, 0.1 },
     { 4.4, 3.0, 1.3, 0.2 },
     { 5.0, 3.5, 1.3, 0.3 },
     { 4.5, 2.3, 1.3, 0.3 },
     { 4.4, 3.2, 1.3, 0.2 },
     { 4.8, 3.0, 1.4, 0.3 },
     { 5.1, 3.8, 1.6, 0.2 },
     { 4.6, 3.2, 1.4, 0.2 },
     { 5.3, 3.7, 1.5, 0.2 },
     { 5.0, 3.3, 1.4, 0.2 },
     { 5.0, 2.0, 3.5, 1.0 },
     { 6.3, 3.3, 6.0, 2.5 },
     { 7.1, 3.0, 5.9, 2.1 },
     { 6.3, 2.9, 5.6, 1.8 },
     { 6.5, 3.0, 5.8, 2.2 },
     { 7.6, 3.0, 6.6, 2.1 },
     { 4.9, 2.5, 4.5, 1.7 },
     { 7.3, 2.9, 6.3, 1.8 },
     { 6.7, 2.5, 5.8, 1.8 },
     { 7.2, 3.6, 6.1, 2.5 },
     { 6.4, 2.7, 5.3, 1.9 },
     { 6.8, 3.0, 5.5, 2.1 },
     { 5.7, 2.5, 5.0, 2.0 },
     { 5.8, 2.8, 5.1, 2.4 },
     { 6.4, 3.2, 5.3, 2.3 },
     { 6.5, 3.0, 5.5, 1.8 },
     { 7.7, 3.8, 6.7, 2.2 },
     { 7.7, 2.6, 6.9, 2.3 },
     { 6.0, 2.2, 5.0, 1.5 },
     { 6.9, 3.2, 5.7, 2.3 },
     { 7.7, 2.8, 6.7, 2.0 },
     { 6.7, 3.3, 5.7, 2.1 },
     { 7.2, 3.2, 6.0, 1.8 },
     { 6.4, 2.8, 5.6, 2.1 },
     { 7.2, 3.0, 5.8, 1.6 },
     { 7.4, 2.8, 6.1, 1.9 },
     { 7.9, 3.8, 6.4, 2.0 },
     { 6.4, 2.8, 5.6, 2.2 },
     { 6.1, 2.6, 5.6, 1.4 },
     { 7.7, 3.0, 6.1, 2.3 },
     { 6.3, 3.4, 5.6, 2.4 },
     { 6.4, 3.1, 5.5, 1.8 },
     { 6.9, 3.1, 5.4, 2.1 },
     { 6.7, 3.1, 5.6, 2.4 },
     { 6.9, 3.1, 5.1, 2.3 },
     { 6.8, 3.2, 5.9, 2.3 },
     { 6.7, 3.3, 5.7, 2.5 },
     { 6.7, 3.0, 5.2, 2.3 },
     { 6.2, 3.4, 5.4, 2.3 },
    };

    std::vector<std::string> get_input_names(){
        std::vector<std::string> lFeatures = { "Feature_0", "Feature_1", "Feature_2", "Feature_3" };

        return lFeatures;
    }

    std::vector<std::string> get_output_names(){
        std::vector<std::string> lOutputs = { 
            "AnomalyScore","OutlierIndicator" };

        return lOutputs;
    }

    tTable compute_outlier_scores(std::any Feature_0, std::any Feature_1, std::any Feature_2, std::any Feature_3) {
        std::vector<std::any> inputs = { Feature_0, Feature_1, Feature_2, Feature_3 };

        std::any lDotProduct = rbf_kernel( lProblem_data_dual, lProblem_data_sv, inputs, 0.06416744863614975 ) + -44.517533261328936;

        tTable lTable;

        lTable["AnomalyScore"] = { lDotProduct } ;
        lTable["OutlierIndicator"] = { ( lDotProduct >= 0.0) ? 1 : -1 } ;

        return lTable;
    }

    tTable compute_model_outputs_from_table( tTable const & iTable) {
        tTable lTable = compute_outlier_scores(iTable.at("Feature_0")[0], iTable.at("Feature_1")[0], iTable.at("Feature_2")[0], iTable.at("Feature_3")[0]);

        return lTable;
    }

} // eof namespace 
antoinecarme commented 3 years ago

Added sklearn.covariance.EllipticEnvelope models

https://github.com/antoinecarme/ml2cpp/blob/master/doc/OutlierDetection/ml2cpp_elliptic_envelope_outlier_detection_iris.ipynb

antoinecarme commented 3 years ago

EllipticEnvelope model C++ sample code:

namespace  {

    std::vector<std::string> get_input_names(){
        std::vector<std::string> lFeatures = { "Feature_0", "Feature_1", "Feature_2", "Feature_3" };

        return lFeatures;
    }

    std::vector<std::string> get_output_names(){
        std::vector<std::string> lOutputs = { 
            "AnomalyScore","OutlierIndicator" };

        return lOutputs;
    }

    tTable compute_outlier_scores(std::any Feature_0, std::any Feature_1, std::any Feature_2, std::any Feature_3) {
        std::any Feature_0_c = Feature_0 - 5.754411764705883;

        std::any Feature_1_c = Feature_1 - 3.054411764705883;

        std::any Feature_2_c = Feature_2 - 3.6080882352941193;

        std::any Feature_3_c = Feature_3 - 1.1500000000000004;

        std::any lMahalanobis = 10.874533961614407 * Feature_0_c * Feature_0_c + -7.523594252028072 * Feature_0_c * Feature_1_c + -8.670095822562605 * Feature_0_c * Feature_2_c + 8.302222841507668 * Feature_0_c * Feature_3_c + -7.523594252028072 * Feature_1_c * Feature_0_c + 13.824125187366795 * Feature_1_c * Feature_1_c + 10.307810357345911 * Feature_1_c * Feature_2_c + -13.196091595841748 * Feature_1_c * Feature_3_c + -8.670095822562605 * Feature_2_c * Feature_0_c + 10.307810357345911 * Feature_2_c * Feature_1_c + 15.996574228771326 * Feature_2_c * Feature_2_c + -25.652281932055583 * Feature_2_c * Feature_3_c + 8.302222841507668 * Feature_3_c * Feature_0_c + -13.196091595841748 * Feature_3_c * Feature_1_c + -25.652281932055583 * Feature_3_c * Feature_2_c + 48.37648645607273 * Feature_3_c * Feature_3_c;

        std::any lScore = -lMahalanobis -(-10.226154869292477);

        tTable lTable;

        lTable["AnomalyScore"] = { lScore } ;
        lTable["OutlierIndicator"] = { ( lScore >= 0.0 ) ? 1 : -1 } ;

        return lTable;
    }

    tTable compute_model_outputs_from_table( tTable const & iTable) {
        tTable lTable = compute_outlier_scores(iTable.at("Feature_0")[0], iTable.at("Feature_1")[0], iTable.at("Feature_2")[0], iTable.at("Feature_3")[0]);

        return lTable;
    }

} // eof namespace
antoinecarme commented 3 years ago

Done.