public void LoadRFModel(string file_name = "") { if (file_name == "") file_name = RF_model_file_path_; decisionForest = new dforest.decisionforest(); alglib.serializer Serializer = new alglib.serializer(); string modelFile = System.IO.File.ReadAllText(file_name); Serializer.ustart_str(modelFile); dforest.dfunserialize(Serializer, decisionForest); Serializer.stop(); Console.WriteLine("Finish loading the RF model"); Console.WriteLine("Total tree size: {0}", decisionForest.trees.Length); int treeSize = (int)(decisionForest.trees.Length / decisionForest.ntrees); Console.WriteLine("single tree size:{0}", treeSize); Console.WriteLine("Number of variable: {0}", decisionForest.nvars); Console.WriteLine("ntress: {0}", decisionForest.ntrees); Console.WriteLine("nclasses: {0}", decisionForest.nclasses); // turn the tree from double type to int type to make it more efficient trees_int_ = new int[decisionForest.trees.Length]; Console.WriteLine("Length of the original tree: {0}", trees_int_.Length); for (int i = 0; i < decisionForest.trees.Length; i++) trees_int_[i] = (int)Math.Ceiling(decisionForest.trees[i]); }
public void WriteRFModel(string file_name) { double [] new_tree = new double[trees_int_.Length]; for (int i=0; i< trees_int_.Length; i++) new_tree[i] = (double) trees_int_[i]; decisionForest.trees = new_tree; alglib.serializer Serializer = new alglib.serializer(); Serializer.alloc_start(); dforest.dfalloc(Serializer, decisionForest); Serializer.sstart_str(); dforest.dfserialize(Serializer, decisionForest); Serializer.stop(); string model_file = Serializer.get_string(); Console.WriteLine("Finish serializing the random forest"); using (StreamWriter outfile = new StreamWriter(file_name)) { outfile.Write(model_file); }; Console.WriteLine("Finish writting the file {0} to the disk", file_name); }
/* ######################### */ // load the random forest model from the file to the class property decisionForest private void LoadRFModel() { string modelFilePath = feature_lib_obj_.directory + "\\FeatureVectureBlue149.rf.model"; Console.WriteLine("Model file path {0}", modelFilePath); string modelFile = System.IO.File.ReadAllText(modelFilePath); alglib.serializer Serializer = new alglib.serializer(); Serializer.ustart_str(modelFile); dforest.dfunserialize(Serializer, decisionForest); Serializer.stop(); Console.WriteLine("Finish loading the RF model"); }