Package: clara-transpiler
ClaraCLARA is a tool designed to help Machine Learning developers to build their models using High-Level languages (Python), but easily implement them in C. The goal is not to convert the code, but to convert the trained model itself (the object). Therefore, this is not a code converter, but acode transpiler.The following algorithms are availableClassificationMLPDecision TreeSupport-Vector Machines (SVC & Nu)LinearSVMGaussian Naive BayesComplement Naive BayesMultinomial Naive BayesCategorical Naive BayesBernoulli Naive BayesRegressionMLPSupport-Vector MachinesDecompositionPCAPreprocessingStandardScalerKernelCentererMaxAbsScalerMinMaxScalerRobustScalerTranspiling ToolsPython ClassClara ClassDecompositionsklearn.decomposition.PCAclara.transpiler.pca.PCATranspilerNeural Networkssklearn.neural_network.MLPClassifierclara.transpiler.mlp.MLPCTranspilersklearn.neural_network.MLPRegressorclara.transpiler.mlp.MLPRTranspilerDecision Treesklearn.tree.DecisionTreeClassifierclara.transpiler.tree.DecisionTreeClassifierTranspilerSupport-Vector Machinessklearn.svm.SVCclara.transpiler.svm.SVCTranspilersklearn.svm.NuSVCclara.transpiler.svm.SVCTranspilersklearn.svm.LinearSVMclara.transpiler.svm.LinearSVMTranspilersklearn.svm.SVRclara.transpiler.svm.SVRTranspilerNaive Bayessklearn.naive_bayes.GaussianNBclara.transpiler.naive_bayes.GaussianNBTranspilersklearn.naive_bayes.ComplementNBclara.transpiler.naive_bayes.ComplementNBTranspilersklearn.naive_bayes.MultinomialNBclara.transpiler.naive_bayes.MultinomialNBTranspilersklearn.naive_bayes.CategoricalNBclara.transpiler.naive_bayes.CategoricalNBTranspilersklearn.naive_bayes.BernoulliNBclara.transpiler.naive_bayes.BernoulliNBTranspilerPreprocessingsklearn.preprocessing.StandardScalerclara.transpiler.preprocessing.StandardScalerTranspilerSyntaxBesides the multiple available algorithms, the syntax to use in any of them is the same and shown in the snippet bellow.#The ML Algorithm you want to usemodel=ScikitLearnClass()# TRAIN YOUR MODEL FIRST!!!model.fit()# The correspondent CLARA TRanspiler Classtranspiler=ClaraClassTranspiler(model)#The correspondent Clara Class# The C code to be exported to a .c file and compiled# The code is of the model trained, therefore no retraining is needed.c_code=transpiler.generate_code()PCA TranspilerPython Exportingfromsklearn.decompositionimportPCAfromsklearn.preprocessingimportStandardScalerfromsklearn.datasetsimportload_winedata=load_wine()dataset=np.column_stack((data.data,data.target))scale=StandardScaler()pca=PCA(n_components=0.8)X=scale.fit_transform(dataset[::,:-1])pca.fit(X)fromclara.transpiler.pcaimportPCATranspilertranspiler=PCATranspiler(pca)code=transpiler.generate_code()withopen("pca.c","w+")asfp:fp.write(code)Test code in CThe results may vary, but if they should be the same!!intmain(intargc,constchar*argv[]){// insert code here...doublesample[N_FEATURES]={1.51861254,-0.5622498,0.23205254,-1.16959318,1.91390522,0.80899739,1.03481896,-0.65956311,1.22488398,0.25171685,0.36217728,1.84791957,1.01300893};doublescores[N_COMPONENTS]={0};doubleinverse_sample[N_FEATURES]={0};calculate_scores(sample,scores);printf("\nScores\n");for(inti=0;i<N_COMPONENTS;i++){printf("%f\t",scores[i]);}printf("\n\nInverse Transform\n");inverse(scores,inverse_sample);for(inti=0;i<N_FEATURES;i++){printf("%f\t",inverse_sample[i]);}printf("\n\n");pca_dimensions_tval;val=calculate_dimensions(sample);printf("T2 = %f, Q-Residuals: %f\n\n",val.hoteling2,val.q_residuals);}MLP TranspilerMulti-Layer Perceptron are the basis of Neural Networks and Deep Learning. Our tools provides a way to transpile MLPs for regression and classification problems.Note:At the current time, binary classifications are not working... Sorry*MLPClassifierfromsklearn.neural_networkimportMLPClassifierfromsklearn.datasetsimportload_wineimportnumpyasnpfromclara.transpiler.mlpimportMLPCTranspilerdata=load_wine()dataset=np.column_stack((data.data,data.target))mlp=MLPClassifier(hidden_layer_sizes=(30,10))mlp.fit(ddataset.data,dataset.target)transpiler=MLPCTranspiler(mlp)code=transpiler.generate_code()withopen("mlp.c","w+")asfp:fp.write(code)MLPRegressorfromsklearn.neural_networkimportMLPRegressorfromsklearn.datasetsimportload_bostonimportnumpyasnpfromclara.transpiler.mlpimportMLPRTranspilerdata=load_boston()dataset=np.column_stack((data.data,data.target))mlp=MLPClassifier(hidden_layer_sizes=(30,10))mlp.fit(dataset.data,dataset.target)transpiler=MLPRTranspiler(mlp)code=transpiler.generate_code()withopen("mlp.c","w+")asfp:fp.write(code)Test code in Cintmain(){doubles[N_FEATURES]={14.23,1.71,2.43,15.6,127.0,2.8,3.06,0.28,2.29,5.64,1.04,3.92,1065.0};intclass;for(inti=0;i<N_FEATURES;i++){sample[i]=s[i];}class=predict(sample);return0;}Cite UsPlease, if you use our tool in any of your projects, cite us. This will help us improve and look at what people may need! Thanks!DOI:10.5281/zenodo.3930335Sérgio Branco. (2020, July 4). CLARA - Embedded ML Tools (Version v0.0.1). Zenodo. http://doi.org/10.5281/zenodo.3930336@software{sergio_branco_2020_3930336,
  author       = {Sérgio Branco},
  title        = {CLARA - Embedded ML Tools},
  month        = jul,
  year         = 2020,
  publisher    = {Zenodo},
  version      = {v0.0.1},
  doi          = {10.5281/zenodo.3930336},
  url          = {https://doi.org/10.5281/zenodo.3930336}
}