forum.alglib.net

ALGLIB forum
It is currently Fri Sep 29, 2023 3:05 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Question about random forest model
PostPosted: Sun Jul 03, 2022 7:07 am 
Offline

Joined: Sun Jul 03, 2022 7:03 am
Posts: 2
Hello,

I am evaluating ALGLIB to be used inside one of my commercial software programs in order to offer my users an easy way to use random forest models for their data. It looks ok so far but I was not able to find a way to "save" the trained model so that it can be loaded and used again. Is this possible?

Thank you all for your time!

Regards

John


Top
 Profile  
 
 Post subject: Re: Question about random forest model
PostPosted: Sun Jul 03, 2022 8:48 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 891
Hi!

It can be performed with dfserialize()/dfunserialize() pair of functions. Random forest can be serialized to string or stream. The data are written using compressed text format, i.e. you can write it to text or XML file, change EOL between Windows and Unix style, and it will work. It is also possible to move models between C++ and C# versions of the library (e.g. train in C++ and use in C#).

Sergey


Top
 Profile  
 
 Post subject: Re: Question about random forest model
PostPosted: Sun Jul 03, 2022 10:03 am 
Offline

Joined: Sun Jul 03, 2022 7:03 am
Posts: 2
That is great! I will proceed with purchasing the commercial version :)


Top
 Profile  
 
 Post subject: Re: Question about random forest model
PostPosted: Tue Aug 15, 2023 3:31 pm 
Offline

Joined: Tue Aug 15, 2023 2:28 pm
Posts: 3
hiļ¼Œcan you provide a code demo to use the dfserialize /dfunserialize function?

does it correct if i ues it as:

Code:
        Qstring outModelDir_path = "D:/out"
        decisionforest model;
        string outModelFilePath = outModelDir_path.toStdString() + "/trainedModel.txt";
   string outModel;
   dfserialize(model, outModel);
   ofstream outModelFile(outModelFilePath.c_str(), ios::out);
   outModelFile << outModel;


i am not sure whether is the right way to use it.
it seems the serialized data look like:
    10000000000 00000000000 A0000000000 10000000000 83000000000

    G-v10000000 G-v10000000 0000000XZ24 00000000000 xHXh75kUqz3

    00000009I24 00000000004 IZFKb9iDO_3 0000000qD24 00000000S04

    U0WFcfm5-y3 0000000w824 00000000m_3 ScN__emr404 0000000mN14

    00000000004 gelTaGVEf-3 0000000WJ14 00000000G04 aoxCpC6RG-3

    0000000W914 00000000K04 9aSgjX2Lwz3 00000000z04 00000000m_3

looking for your help!


Top
 Profile  
 
 Post subject: Re: Question about random forest model
PostPosted: Wed Aug 16, 2023 10:44 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 891
Hi!

Yes, it is a right way to do. The model is serialized in a portable text format which can tolerate all types of issues arising when moving text between machines and operating systems. In particular, it does not contain non-ASCII symbols, can easily tolerate conversion between Windows and Unix newlines and so on.

Sergei


Top
 Profile  
 
 Post subject: Re: Question about random forest model
PostPosted: Wed Aug 16, 2023 1:33 pm 
Offline

Joined: Tue Aug 15, 2023 2:28 pm
Posts: 3
Thats cool! Thanks for your thoughtful work to make it easy to use!
Best regards!
shaos


Top
 Profile  
 
 Post subject: Re: Question about random forest model
PostPosted: Sun Aug 20, 2023 5:01 am 
Offline

Joined: Tue Aug 15, 2023 2:28 pm
Posts: 3
hi!
By following your suggestion, i successfully serialized the model to txt.
I didnot find any demo code in guidebook, is it correct to use dfunserialize function following the code below?
i use C++ to finish this work

Code:
string loadModel(string serializedModelFileStringFormatPath) {
   ifstream serializedModelFile(serializedModelFileStringFormatPath);
   if (!serializedModelFile.is_open()) {
      cout << cannot open serialized data" << endl;
      return "cannot open serialized data";
   }
   ostringstream  tmp;
   tmp << serializedModelFile.rdbuf();
   string  data = tmp.str();
   serializedModelFile.close();
   return data;
}

int main(
  string inModelFilePath = "c:/test/inmodel.txt";
  string modelContent = loadModel(inModelFilePath);
  decisionforest model;
  dfunserialize(modelContent, model);
  double esti = dfprocess0(model, rowItem);
{


I would appreciate a lot!!
shaos


Top
 Profile  
 
 Post subject: Re: Question about random forest model
PostPosted: Thu Aug 24, 2023 12:22 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 891
Yes, you are right, it must work


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group