65 _currentModelName(
""),
70 xmlKeepBlanksDefault(0);
79 throw string(
"Unable to read PMML file.");
81 catch ( std::string msg )
102 _currentModelName(
""),
117 cout <<
"~PMMLlib" << endl;
125 void PMMLlib::SetCurrentModel(std::string modelName,
139 throw string(
"Unknown PMML type.");
143 throw string(
"Model not found.");
151 void PMMLlib::SetCurrentModel(std::string modelName)
154 throw string(
"No PMML file set.");
155 xmlNodePtr node = NULL;
160 string nodeModelName =
_getProp(node,
string(
"modelName"));
161 if ( nodeModelName == modelName )
172 std::ostringstream oss;
174 string msg =
"SetCurrentModel(modelName) : found " + oss.str() +
" model(s) in PMML file.\n";
175 msg +=
"Use SetCurrentModel(modelName,type).";
184 void PMMLlib::SetCurrentModel()
189 std::ostringstream oss;
191 string msg =
"SetCurrentModel() : found " + oss.str() +
" model(s) in PMML file.\n";
192 msg +=
"Use SetCurrentModel(modelName) or SetCurrentModel(modelName,type).";
204 string msg(
"Couldn't get node in SetCurrentModel().");
214 std::string PMMLlib::makeLog()
const
217 out <<
"**\n**** Display of PMMLlib ****" << endl;
218 out <<
" ** _pmmlFile[" <<
_pmmlFile <<
"]" << endl;
219 out <<
" ** _log[" << (
_log?1:0) <<
"]" << endl;
220 out <<
"**\n**** End of display of PMMLlib ****" << endl;
227 void PMMLlib::printLog()
const
239 void PMMLlib::SetRootNode()
242 _doc = xmlNewDoc(xs);
249 xmlNewProp(
_rootNode, (
const xmlChar*)
"xmlns", (
const xmlChar*)
"http://www.dmg.org/PMML-4_1");
250 xmlNewProp(
_rootNode, (
const xmlChar*)
"version", (
const xmlChar*)
"4.1");
264 void PMMLlib::SetHeader(std::string copyright,
265 std::string description,
267 std::string appVersion,
268 std::string annotation)
270 xmlNodePtr headerNode = xmlNewChild(
_rootNode, 0, (
const xmlChar*)
"Header", 0);
271 xmlNewProp(headerNode, (
const xmlChar*)
"copyright", (
const xmlChar*)(copyright.c_str()));
272 xmlNewProp(headerNode, (
const xmlChar*)
"description", (
const xmlChar*)(description.c_str()));
274 xmlNodePtr appNode = xmlNewChild(headerNode, 0, (
const xmlChar*)
"Application", 0);
275 xmlNewProp(appNode, (
const xmlChar*)
"name", (
const xmlChar*)(appName.c_str()));
276 xmlNewProp(appNode, (
const xmlChar*)
"version", (
const xmlChar*)(appVersion.c_str()));
278 xmlNewChild(headerNode, 0, (
const xmlChar*)
"Annotation", (
const xmlChar*)(annotation.c_str()));
287 void PMMLlib::AddMiningSchema(std::string name,
288 std::string usageType)
293 xmlNodePtr miningSchemaNode =
GetChildByName(netNode,
"MiningSchema");
294 if(!miningSchemaNode)
296 miningSchemaNode = xmlNewChild(netNode, 0, (
const xmlChar*)
"MiningSchema", 0);
300 xmlNodePtr miningFieldNode = xmlNewChild(miningSchemaNode, 0, (
const xmlChar*)
"MiningField", 0);
301 xmlNewProp(miningFieldNode, (
const xmlChar*)
"name", (
const xmlChar*)(name.c_str()) );
302 xmlNewProp(miningFieldNode, (
const xmlChar*)
"usageType", (
const xmlChar*)(usageType.c_str()) );
311 xmlNodePtr PMMLlib::GetChildByName(xmlNodePtr node,
312 std::string nodeName)
317 xmlNodePtr childNode = node->children;
318 if ( childNode == NULL )
321 const xmlChar* name = childNode->name;
326 while( (childNode != NULL) && (strName != nodeName) )
328 childNode = childNode->next;
329 if ( childNode == NULL )
331 name = childNode->name;
342 void PMMLlib::CountModels()
347 cout <<
" ** End Of Count Models nCount[" << nCount <<
"]" << endl;
355 int PMMLlib::CountNeuralNetModels()
364 cout <<
" ** nCount[" << nCount <<
"]" << endl;
368 cout <<
" ** End Of CountNetworks nCount[" << nCount <<
"]" << endl;
376 int PMMLlib::CountRegressionModels()
385 cout <<
" ** nCount[" << nCount <<
"]" << endl;
389 cout <<
" ** End Of CountRegressions nCount[" << nCount <<
"]" << endl;
397 int PMMLlib::GetModelsNb()
407 std::string PMMLlib::GetModelName(xmlNodePtr node)
410 name =
_getProp(node,
string(
"modelName") );
420 xmlNodePtr PMMLlib::GetPtr(
int index,
423 xmlNodePtr node = NULL;
431 while ((i != index) && (node != NULL))
446 xmlNodePtr PMMLlib::GetPtr(std::string myModelName,
447 std::string nodeName)
449 xmlNodePtr node = NULL;
455 string modelName =
_getProp(node,
string(
"modelName"));
457 while ( (node != NULL) && modelName != myModelName )
462 modelName =
_getProp(node,
string(
"modelName"));
474 std::string PMMLlib::GetTypeString()
480 name =
"NeuralNetwork";
483 name =
"RegressionModel";
486 throw string(
"Unknown PMML type.");
503 if ( name ==
"NeuralNetwork" )
505 else if ( name ==
"RegressionModel" )
515 std::string PMMLlib::GetCurrentModelName()
526 void PMMLlib::UnlinkNode()
529 xmlUnlinkNode( ptr );
536 void PMMLlib::BackupNode()
544 xmlNodePtr ptr =
GetPtr(ss.str(), name);
549 cout <<
" ** nCrtIndex[" << nCrtIndex <<
"]" << endl;
553 ptr =
GetPtr(ss.str(), name);
556 cout <<
" *** Node \"" <<
_currentModelName <<
"\" found, then backup it with index [" << nCrtIndex <<
"]" << endl;
559 xmlNewProp(
_currentModelNode, (
const xmlChar*)
"modelName", (
const xmlChar*)(ss.str().c_str()));
565 void PMMLlib::Write()
577 void PMMLlib::Write(std::string file)
580 int ret = xmlSaveFormatFile( file.c_str(),
_doc, 1);
583 std::string msg(
" *** Error :: unable to write the PMML file \"" + file +
"\"") ;
588 cout <<
" *** Write the PMML file \"" << file <<
"\"" << endl;
597 void PMMLlib::ExportCpp(std::string file,
598 std::string functionName,
608 throw string(
"ExportCpp : PMML type not handled.");
617 void PMMLlib::ExportFortran(std::string file,
618 std::string functionName,
626 throw string(
"ExportFortran : PMML type not handled.");
635 void PMMLlib::ExportPython(std::string file,
636 std::string functionName,
644 throw string(
"ExportPython : PMML type not handled.");
654 std::string PMMLlib::ExportPyStr(std::string functionName,
662 throw string(
"ExportPyStr : PMML type not handled.");
670 std::string PMMLlib::_xmlCharToString(
const xmlChar *xs)
const
672 size_t i, L = xmlStrlen(xs);
675 for (i=0; *xs; s[i++] = *xs++);
684 xmlChar * PMMLlib::_stringToXmlChar(
const std::string &s)
const
686 return xmlCharStrdup(s.c_str());
695 std::string PMMLlib::_getProp(
const xmlNodePtr node,
696 std::string
const & prop )
const
698 std::string name(
"");
703 attr = xmlGetProp(node, xp );
729 void PMMLlib::CheckNeuralNetwork()
732 throw string(
"Use this method with NeuralNetwork models.");
740 xmlNodePtr PMMLlib::GetNeuralNetPtr(
int index)
750 xmlNodePtr PMMLlib::GetNeuralNetPtr(std::string name)
760 std::string PMMLlib::ReadNetworkStructure()
764 string structure(
"");
767 if ( inputNodes != NULL )
770 if ( inputNode != NULL )
772 while (inputNode != NULL)
777 xmlNodePtr fieldName = child->children;
778 if ( fieldName != NULL )
780 string field =
_getProp(fieldName,
string(
"field"));
785 inputNode = inputNode->next;
788 structure.erase(structure.size()-1);
793 if ( node_layer != NULL )
795 string name = string((
const char*)(node_layer->name));
798 while ( node_layer != NULL &&
799 (
string((
const char*)(node_layer->name)) ==
"NeuralLayer") &&
800 node_layer->next != NULL &&
801 (
string((
const char*)(node_layer->next->name)) !=
"NeuralOutputs") )
804 string nbneurons =
_getProp(node_layer,
string(
"numberOfNeurons"));
805 structure += nbneurons;
807 node_layer = node_layer->next;
812 if ( node_outputs != NULL )
814 xmlNodePtr node_output =
GetChildByName(node_outputs,
"NeuralOutput");
815 if ( node_output != NULL )
817 while (node_output != NULL)
823 xmlNodePtr fieldName = child->children;
824 if ( fieldName != NULL )
826 if (
string((
const char*)(fieldName->name)) ==
"NormContinuous")
829 string field =
_getProp(fieldName,
string(
"field"));
834 node_output = node_output->next;
837 structure.erase(structure.size()-1);
848 int PMMLlib::GetNbInputs()
854 if ( node_inputs == NULL )
857 node_inputs = node_inputs->children;
858 while (node_inputs != NULL)
861 node_inputs = node_inputs->next;
872 int PMMLlib::GetNbOutputs()
878 if ( node_outputs == NULL )
881 node_outputs = node_outputs->children;
883 while (node_outputs != NULL)
886 node_outputs = node_outputs->next;
898 std::string PMMLlib::GetNameInput(
int index)
904 if ( node_inputs == NULL )
907 node_inputs = node_inputs->children;
908 if ( node_inputs == NULL )
911 for(
int i = 0;
i<index;
i++)
913 node_inputs = node_inputs->next;
914 if ( node_inputs == NULL )
918 node_inputs = node_inputs->children;
919 if ( node_inputs == NULL )
922 node_inputs = node_inputs->children;
923 if ( node_inputs == NULL )
926 name =
_getProp(node_inputs,
string(
"field"));
937 std::string PMMLlib::GetNameOutput(
int index)
943 if ( node_outputs == NULL )
945 node_outputs = node_outputs->children;
946 if ( node_outputs == NULL )
948 for(
int i = 0;
i<index;
i++)
950 node_outputs = node_outputs->next;
951 if ( node_outputs == NULL )
955 node_outputs = node_outputs->children;
956 if ( node_outputs == NULL )
958 node_outputs = node_outputs->children;
959 if ( node_outputs == NULL )
962 name =
_getProp(node_outputs,
string(
"field") );
972 int PMMLlib::GetNormalizationType()
979 xmlNodePtr node_field = nodeTmp->children;
980 xmlNodePtr node_linearnorm;
982 double dorig1, dnorm1;
983 double dorig2, dnorm2;
984 if (
string((
const char*)(node_field->name)) ==
"NormContinuous")
987 node_linearnorm = node_field->children;
988 str_tmp =
_getProp(node_linearnorm,
string(
"orig"));
989 dorig1 = atof(str_tmp.c_str());
990 str_tmp =
_getProp(node_linearnorm,
string(
"norm"));
991 dnorm1 = atof(str_tmp.c_str());
992 node_linearnorm = node_linearnorm->next;
993 str_tmp =
_getProp(node_linearnorm,
string(
"orig"));
994 dorig2 = atof(str_tmp.c_str());
995 str_tmp =
_getProp(node_linearnorm,
string(
"norm"));
996 dnorm2 = atof(str_tmp.c_str());
997 if ( dnorm1 * dnorm2 < -0.5 )
1006 string msg(
"Unable to retrieve the normalization type.");
1017 void PMMLlib::GetNormalisationInput(
int index,
1024 if ( node_inputs == NULL )
1027 if ( node_inputs == NULL )
1030 for(
int i=0;
i<index;
i++)
1032 node_inputs = node_inputs->next;
1033 if ( node_inputs == NULL )
1037 if ( tmpNode == NULL )
1039 xmlNodePtr node_field =
GetChildByName(tmpNode,
"NormContinuous");
1040 if ( node_field == NULL )
1042 if (
string((
const char*)(node_field->name)) ==
"NormContinuous")
1046 xmlNodePtr node_linearnorm = node_field->children;
1047 str_tmp =
_getProp(node_linearnorm,
string(
"orig"));
1048 double dorig1 = atof(str_tmp.c_str());
1049 str_tmp =
_getProp(node_linearnorm,
string(
"norm"));
1050 double dnorm1 = atof(str_tmp.c_str());
1051 node_linearnorm = node_linearnorm->next;
1052 str_tmp =
_getProp(node_linearnorm,
string(
"orig"));
1053 double dorig2 = atof(str_tmp.c_str());
1054 str_tmp =
_getProp(node_linearnorm,
string(
"norm"));
1055 double dnorm2 = atof(str_tmp.c_str());
1056 if ( dnorm1 * dnorm2 < -0.5 )
1066 dnorm[1] = -1.0 * dnorm1 * dorig2;
1077 void PMMLlib::GetNormalisationOutput(
int index,
1085 if ( node_outputs == NULL )
1088 if ( node_outputs == NULL )
1091 for(
int i=0;
i< index;
i++)
1093 node_outputs = node_outputs->next;
1094 if ( node_outputs == NULL )
1097 xmlNodePtr tmpNode =
GetChildByName(node_outputs,
"DerivedField");
1098 if ( tmpNode == NULL )
1100 xmlNodePtr node_field =
GetChildByName(tmpNode,
"NormContinuous");
1101 if ( node_field == NULL )
1104 if (
string((
const char*)(node_field->name)) ==
"NormContinuous")
1108 xmlNodePtr node_linearnorm = node_field->children;
1109 str_tmp =
_getProp(node_linearnorm,
string(
"orig"));
1110 double dorig1 = atof(str_tmp.c_str());
1111 str_tmp =
_getProp(node_linearnorm,
string(
"norm"));
1112 double dnorm1 = atof(str_tmp.c_str());
1113 node_linearnorm = node_linearnorm->next;
1114 str_tmp =
_getProp(node_linearnorm,
string(
"orig"));
1115 double dorig2 = atof(str_tmp.c_str());
1116 str_tmp =
_getProp(node_linearnorm,
string(
"norm"));
1117 double dnorm2 = atof(str_tmp.c_str());
1118 if ( dnorm1 * dnorm2 < -0.5 )
1128 dnorm[1] = -1.0 * dorig2 * dnorm1;
1138 int PMMLlib::GetNbHiddenLayers()
1144 if ( node_layers == NULL )
1147 while (
string((
const char*)(node_layers->name)) ==
"NeuralLayer")
1150 node_layers = node_layers->next;
1151 if ( node_layers == NULL )
1161 int PMMLlib::GetNbLayers()
1171 int PMMLlib::GetNbNeuronsAtLayer(
int index)
1177 if ( node_layers == NULL )
1181 for(
int i=0;
i<index;
i++)
1183 node_layers = node_layers->next;
1184 if ( node_layers == NULL )
1189 while(node_neurons != NULL)
1192 node_neurons = node_neurons->next;
1205 double PMMLlib::GetNeuronBias(
int layer_index,
1212 if ( node_layers == NULL )
1215 for(
int i=0;
i<layer_index;
i++)
1217 node_layers = node_layers->next;
1218 if ( node_layers == NULL )
1223 for(
int j=0;j<neu_index;j++)
1225 node_neurons = node_neurons->next;
1226 if ( node_neurons == NULL )
1229 string str_tmp =
_getProp(node_neurons,
string(
"bias"));
1230 bias = atof(str_tmp.c_str());
1242 double PMMLlib::GetPrecNeuronSynapse(
int layer_index,
1250 if ( node_layers == NULL )
1253 for(
int i=0;
i<layer_index;
i++)
1255 node_layers = node_layers->next;
1256 if ( node_layers == NULL )
1261 for(
int i=0;
i<neu_index;
i++)
1263 node_neurons = node_neurons->next;
1264 if ( node_neurons == NULL )
1269 for(
int i=0;
i<prec_index;
i++)
1271 node_con = node_con->next;
1272 if ( node_con == NULL )
1275 string str_tmp =
_getProp(node_con,
string(
"weight"));
1276 weight = atof(str_tmp.c_str());
1287 void PMMLlib::SetNeuralNetName(
int index,
1296 while ((i != index) && (node_ann != NULL))
1298 node_ann = node_ann->next;
1301 xmlNewProp(node_ann, (
const xmlChar*)
"modelName", (
const xmlChar*)(name.c_str()));
1303 xmlSaveFormatFile(
string(
_pmmlFile+
".pmml").c_str(),
_doc, 1);
1318 void PMMLlib::AddDataField(std::string fieldName,
1319 std::string displayName,
1321 std::string dataType,
1322 std::string closure,
1331 dataDictNode = xmlNewChild(
_rootNode, 0, (
const xmlChar*)
"DataDictionary", 0);
1335 xmlNodePtr dataFieldNode = xmlNewChild(dataDictNode, 0, (
const xmlChar*)
"DataField", 0);
1336 xmlNewProp(dataFieldNode, (
const xmlChar*)
"name", (
const xmlChar*)(fieldName.c_str()) );
1337 xmlNewProp(dataFieldNode, (
const xmlChar*)
"displayName", (
const xmlChar*)(displayName.c_str()) );
1338 xmlNewProp(dataFieldNode, (
const xmlChar*)
"optype", (
const xmlChar*)(optype.c_str()) );
1339 xmlNewProp(dataFieldNode, (
const xmlChar*)
"dataType", (
const xmlChar*)(dataType.c_str()) );
1343 xmlNodePtr intervalNode = xmlNewChild(dataFieldNode, 0, (
const xmlChar*)
"Interval", 0);
1344 xmlNewProp(intervalNode, (
const xmlChar*)
"closure", (
const xmlChar*)(closure.c_str()) );
1346 ss << scientific << leftMargin;
1347 xmlNewProp(intervalNode, (
const xmlChar*)
"leftMargin", (
const xmlChar*)(ss.str().c_str()) );
1349 ss << scientific << rightMargin;
1350 xmlNewProp(intervalNode, (
const xmlChar*)
"rightMargin", (
const xmlChar*)(ss.str().c_str()) );
1360 void PMMLlib::AddNeuralNetwork(std::string modelName,
1369 switch(functionName)
1372 function =
"regression";
1376 xmlNodePtr netNode = xmlNewChild(
_rootNode, 0, (
const xmlChar*)
"NeuralNetwork", 0);
1377 xmlNewProp(netNode, (
const xmlChar*)
"modelName", (
const xmlChar*)(
_currentModelName.c_str()) );
1378 xmlNewProp(netNode, (
const xmlChar*)
"functionName", (
const xmlChar*)(
function.c_str()) );
1379 xmlNewProp(netNode, (
const xmlChar*)
"numberOfLayers", (
const xmlChar*)
"0" );
1395 void PMMLlib::AddNeuralInput(
int id,
1396 std::string inputName,
1398 std::string dataType,
1399 double orig1,
double norm1,
1400 double orig2,
double norm2)
1406 xmlNodePtr neuralInputsNode =
GetChildByName(netNode,
"NeuralInputs");
1407 if(!neuralInputsNode)
1409 neuralInputsNode = xmlNewChild(netNode, 0, (
const xmlChar*)
"NeuralInputs", 0);
1410 xmlNewProp(neuralInputsNode, (
const xmlChar*)
"numberOfInputs", (
const xmlChar*)
"0" );
1413 string numberOfInputsStr =
_getProp(neuralInputsNode,
string(
"numberOfInputs"));
1415 istringstream( numberOfInputsStr ) >> numberOfInputs;
1418 ss << numberOfInputs;
1419 xmlSetProp(neuralInputsNode, (
const xmlChar*)
"numberOfInputs", (
const xmlChar*)(ss.str().c_str()) );
1421 xmlNodePtr neuralInputNode = xmlNewChild(neuralInputsNode, 0, (
const xmlChar*)
"NeuralInput", 0);
1422 ss.str(
""); ss << id;
1423 xmlNewProp(neuralInputNode, (
const xmlChar*)
"id", (
const xmlChar*)(ss.str().c_str()) );
1425 xmlNodePtr derivedFieldNode = xmlNewChild(neuralInputNode, 0, (
const xmlChar*)
"DerivedField", 0);
1426 xmlNewProp(derivedFieldNode, (
const xmlChar*)
"optype", (
const xmlChar*)(optype.c_str()) );
1427 xmlNewProp(derivedFieldNode, (
const xmlChar*)
"dataType", (
const xmlChar*)(dataType.c_str()) );
1429 xmlNodePtr normcontNode = xmlNewChild(derivedFieldNode, 0, (
const xmlChar*)
"NormContinuous", 0);
1430 xmlNewProp(normcontNode, (
const xmlChar*)
"field", (
const xmlChar*)(inputName.c_str()) );
1432 xmlNodePtr node_linearnorm1 = xmlNewChild(normcontNode, 0, (
const xmlChar*)
"LinearNorm", 0);
1433 ss.str(
""); ss << scientific << orig1;
1434 xmlNewProp(node_linearnorm1, (
const xmlChar*)
"orig", (
const xmlChar*)(ss.str().c_str()) );
1435 ss.str(
""); ss << scientific << norm1;
1436 xmlNewProp(node_linearnorm1, (
const xmlChar*)
"norm", (
const xmlChar*)(ss.str().c_str()) );
1437 xmlNodePtr node_linearnorm2 = xmlNewChild(normcontNode, 0, (
const xmlChar*)
"LinearNorm", 0);
1438 ss.str(
""); ss << scientific << orig2;
1439 xmlNewProp(node_linearnorm2, (
const xmlChar*)
"orig", (
const xmlChar*)(ss.str().c_str()) );
1440 ss.str(
""); ss << scientific << norm2;
1441 xmlNewProp(node_linearnorm2, (
const xmlChar*)
"norm", (
const xmlChar*)(ss.str().c_str()) );
1456 void PMMLlib::AddNeuralOutput(
int outputNeuron,
1457 std::string outputName,
1459 std::string dataType,
1460 double orig1,
double norm1,
1461 double orig2,
double norm2)
1467 xmlNodePtr neuralOutputsNode =
GetChildByName(netNode,
"NeuralOutputs");
1468 if(!neuralOutputsNode)
1470 neuralOutputsNode = xmlNewChild(netNode, 0, (
const xmlChar*)
"NeuralOutputs", 0);
1471 xmlNewProp(neuralOutputsNode, (
const xmlChar*)
"numberOfOutputs", (
const xmlChar*)
"0" );
1474 string numberOfOutputsStr =
_getProp(neuralOutputsNode,
string(
"numberOfOutputs"));
1475 int numberOfOutputs;
1476 istringstream( numberOfOutputsStr ) >> numberOfOutputs;
1479 ss << numberOfOutputs;
1480 xmlSetProp(neuralOutputsNode, (
const xmlChar*)
"numberOfOutputs", (
const xmlChar*)(ss.str().c_str()) );
1483 xmlNodePtr neuralOutputNode = xmlNewChild(neuralOutputsNode, 0, (
const xmlChar*)
"NeuralOutput", 0);
1484 ss.str(
""); ss << outputNeuron;
1485 xmlNewProp(neuralOutputNode, (
const xmlChar*)
"outputNeuron", (
const xmlChar*)(ss.str().c_str()) );
1487 xmlNodePtr derivedFieldNode = xmlNewChild(neuralOutputNode, 0, (
const xmlChar*)
"DerivedField", 0);
1488 xmlNewProp(derivedFieldNode, (
const xmlChar*)
"optype", (
const xmlChar*)(optype.c_str()) );
1489 xmlNewProp(derivedFieldNode, (
const xmlChar*)
"dataType", (
const xmlChar*)(dataType.c_str()) );
1491 xmlNodePtr normcontNode = xmlNewChild(derivedFieldNode, 0, (
const xmlChar*)
"NormContinuous", 0);
1492 xmlNewProp(normcontNode, (
const xmlChar*)
"field", (
const xmlChar*)(outputName.c_str()) );
1494 xmlNodePtr node_linearnorm1 = xmlNewChild(normcontNode, 0, (
const xmlChar*)
"LinearNorm", 0);
1495 ss.str(
""); ss << scientific << orig1;
1496 xmlNewProp(node_linearnorm1, (
const xmlChar*)
"orig", (
const xmlChar*)(ss.str().c_str()) );
1497 ss.str(
""); ss << scientific << norm1;
1498 xmlNewProp(node_linearnorm1, (
const xmlChar*)
"norm", (
const xmlChar*)(ss.str().c_str()) );
1499 xmlNodePtr node_linearnorm2 = xmlNewChild(normcontNode, 0, (
const xmlChar*)
"LinearNorm", 0);
1500 ss.str(
""); ss << scientific << orig2;
1501 xmlNewProp(node_linearnorm2, (
const xmlChar*)
"orig", (
const xmlChar*)(ss.str().c_str()) );
1502 ss.str(
""); ss << scientific << norm2;
1503 xmlNewProp(node_linearnorm2, (
const xmlChar*)
"norm", (
const xmlChar*)(ss.str().c_str()) );
1515 string functionName;
1516 switch(activationFunction)
1519 functionName =
"identity";
1522 functionName =
"tanh";
1525 functionName =
"logistic";
1532 istringstream( numberOfLayersStr ) >> numberOfLayers;
1535 ss << numberOfLayers;
1536 xmlSetProp(netNode, (
const xmlChar*)
"numberOfLayers", (
const xmlChar*)(ss.str().c_str()) );
1538 xmlNodePtr neuralLayerNode = xmlNewChild(netNode, 0, (
const xmlChar*)
"NeuralLayer", 0);
1539 xmlNewProp(neuralLayerNode, (
const xmlChar*)
"activationFunction", (
const xmlChar*)(functionName.c_str()) );
1540 xmlNewProp(neuralLayerNode, (
const xmlChar*)
"numberOfNeurons", (
const xmlChar*)
"0" );
1554 void PMMLlib::AddNeuron(
int id,
1558 vector<double> weights)
1566 int numberOfNeurons;
1567 istringstream( numberOfNeuronsStr ) >> numberOfNeurons;
1569 ss << numberOfNeurons;
1570 xmlSetProp(
_currentNode, (
const xmlChar*)
"numberOfNeurons", (
const xmlChar*)(ss.str().c_str()) );
1573 xmlNodePtr neuronNode = xmlNewChild(
_currentNode, 0, (
const xmlChar*)
"Neuron", 0);
1574 ss.str(
""); ss << id;
1575 xmlNewProp(neuronNode, (
const xmlChar*)
"id", (
const xmlChar*)(ss.str().c_str()) );
1576 ss.str(
""); ss << scientific << bias;
1577 xmlNewProp(neuronNode, (
const xmlChar*)
"bias", (
const xmlChar*)(ss.str().c_str()) );
1580 for(
int k=0 ; k<conNb ; k++)
1582 xmlNodePtr conNode = xmlNewChild(neuronNode, 0, (
const xmlChar*)
"Con", 0);
1583 ss.str(
""); ss << firstFrom+k;
1584 xmlNewProp(conNode, (
const xmlChar*)
"from", (
const xmlChar*)(ss.str().c_str()) );
1585 ss.str(
""); ss << scientific << weights[k];
1586 xmlNewProp(conNode, (
const xmlChar*)
"weight", (
const xmlChar*)(ss.str().c_str()) );
1603 void PMMLlib::fillVectorsForExport(
int nInput,
1607 vector<double> &minInput,
1608 vector<double> &maxInput,
1609 vector<double> &minOutput,
1610 vector<double> &maxOutput,
1611 vector<double> &valW )
1618 for(
int i=0 ;
i<nInput ;
i++)
1621 node_inputs = node_inputs->children;
1622 for(
int j = 0;j<
i;j++)
1624 node_inputs = node_inputs->next;
1626 node_inputs = node_inputs->children;
1627 node_inputs = node_inputs->children;
1628 node_inputs = node_inputs->children;
1629 string strOrig1 =
_getProp(node_inputs,
string(
"orig") );
1630 double orig1 = atof( strOrig1.c_str() );
1631 string strNorm1 =
_getProp(node_inputs,
string(
"norm") );
1632 double norm1 = atof( strNorm1.c_str() );
1633 node_inputs = node_inputs->next;
1634 string strOrig2 =
_getProp(node_inputs,
string(
"orig") );
1635 double orig2 = atof( strOrig2.c_str() );
1636 string strNorm2 =
_getProp(node_inputs,
string(
"norm") );
1639 minInput[
i] = orig1;
1640 maxInput[
i] = orig2;
1644 minInput[
i] = orig2;
1645 maxInput[
i] = -1.0*norm1*orig2;
1648 xmlNodePtr node_outputs =
GetChildByName(netNode,
"NeuralOutputs");
1649 node_outputs = node_outputs->children;
1650 node_outputs = node_outputs->children;
1651 node_outputs = node_outputs->children;
1652 node_outputs = node_outputs->children;
1653 string strOrig1 =
_getProp(node_outputs,
string(
"orig") );
1654 double orig1 = atof( strOrig1.c_str() );
1655 string strNorm1 =
_getProp(node_outputs,
string(
"norm") );
1656 double norm1 = atof( strNorm1.c_str() );
1657 node_outputs = node_outputs->next;
1658 string strOrig2 =
_getProp(node_outputs,
string(
"orig") );
1659 double orig2 = atof( strOrig2.c_str() );
1662 minOutput[0] = orig1;
1663 maxOutput[0] = orig2;
1667 minOutput[0] = orig2;
1668 maxOutput[0] = -1.0*norm1*orig2;
1671 for(
int j=0 ; j<nHidden ; j++)
1674 for(
int i=0 ;
i<nInput ;
i++)
1679 for(
int j=0 ; j<nOutput ; j++)
1682 for(
int i=0 ;
i<nHidden ;
i++)
1696 void PMMLlib::ExportNeuralNetworkCpp(std::string file,
1697 std::string functionName,
1706 int nNeurons = nInput+nOutput+nHidden;
1707 int nWeights = nHidden*(nInput+nOutput+1)+nOutput;
1710 vector<double> minInput(nInput);
1711 vector<double> maxInput(nInput);
1712 vector<double> minOutput(nOutput);
1713 vector<double> maxOutput(nOutput);
1714 vector<double> valW(nWeights);
1715 fillVectorsForExport(nInput,nOutput,nHidden,normType,minInput,maxInput,minOutput,maxOutput,valW);
1717 ofstream sourcefile(file.c_str());
1721 sourcefile <<
"#define ActivationFunction(sum) ( tanh(sum) )" << endl;
1725 sourcefile <<
"#define ActivationFunction(sum) ( 1.0 / ( 1.0 + exp( -1.0 * sum )) )" << endl;
1728 sourcefile <<
"void " << functionName <<
"(double *param, double *res)" << endl;
1729 sourcefile <<
"{" << endl;
1731 sourcefile <<
" ////////////////////////////// " << endl;
1732 sourcefile <<
" //" << endl;
1734 header =
" // " + header;
1736 while ((pos = header.find(
"\n", pos)) != std::string::npos)
1738 header.replace(pos, 1,
"\n //");
1741 sourcefile << header << endl;
1742 sourcefile <<
" //" << endl;
1743 sourcefile <<
" ////////////////////////////// " << endl;
1745 sourcefile <<
" int nInput = " << nInput <<
";" << endl;
1746 sourcefile <<
" int nOutput = " << nOutput <<
";" << endl;
1748 sourcefile <<
" int nHidden = " << nHidden <<
";" << endl;
1749 sourcefile <<
" const int nNeurones = " << nNeurons <<
";" << endl;
1750 sourcefile <<
" double " << functionName <<
"_act[nNeurones];" << endl;
1752 sourcefile <<
" // --- Preprocessing of the inputs and outputs" << endl;
1753 sourcefile <<
" double " << functionName <<
"_minInput[] = {" << endl <<
" ";
1754 for(
int i=0 ;
i<nInput ;
i++)
1756 sourcefile << minInput[
i] <<
", ";
1758 sourcefile <<
"\n ";
1762 sourcefile <<
" };" << endl;
1764 sourcefile <<
" double " << functionName <<
"_minOutput[] = {" << endl <<
" ";
1765 sourcefile << minOutput[0] <<
", ";
1766 sourcefile <<
" };" << endl;
1768 sourcefile <<
" double " << functionName <<
"_maxInput[] = {" << endl <<
" ";
1769 for(
int i=0 ;
i<nInput ;
i++)
1771 sourcefile << maxInput[
i] <<
", ";
1773 sourcefile <<
"\n ";
1777 sourcefile <<
" };" << endl;
1779 sourcefile <<
" double " << functionName <<
"_maxOutput[] = {" << endl <<
" ";
1780 sourcefile << maxOutput[0] <<
", ";
1781 sourcefile <<
" };" << endl;
1784 sourcefile <<
" // --- Values of the weights" << endl;
1785 sourcefile <<
" double " << functionName <<
"_valW[] = {" << endl <<
" ";
1786 for(
int i=0 ;
i<nWeights ;
i++)
1788 sourcefile << valW[
i] <<
", ";
1790 sourcefile << endl <<
" ";
1792 sourcefile << endl <<
" };"<<endl;
1794 sourcefile <<
" // --- Constants";
1796 sourcefile <<
" int indNeurone = 0;"<<endl;
1797 sourcefile <<
" int CrtW;"<<endl;
1798 sourcefile <<
" double sum;"<<endl;
1802 sourcefile <<
" // --- Input Layers"<<endl;
1803 sourcefile <<
" for(int i = 0; i < nInput; i++) {"<<endl;
1806 sourcefile <<
" " << functionName <<
"_act[indNeurone++] = 2.0 * ( param[i] - "
1807 << functionName <<
"_minInput[i] ) / ( " << functionName <<
"_maxInput[i] - "
1808 << functionName <<
"_minInput[i] ) - 1.0;"<<endl;
1812 sourcefile <<
" " << functionName <<
"_act[indNeurone++] = ( param[i] - "
1813 << functionName <<
"_minInput[i] ) / " << functionName <<
"_maxInput[i];"
1816 sourcefile <<
" }"<<endl;
1821 sourcefile <<
" // --- Hidden Layers"<<endl;
1822 sourcefile <<
" for (int member = 0; member < nHidden; member++) {"<<endl;
1823 sourcefile <<
" int CrtW = member * ( nInput + 2) + 2;" << endl;
1824 sourcefile <<
" sum = " << functionName <<
"_valW[CrtW++];" << endl;
1825 sourcefile <<
" for (int source = 0; source < nInput; source++) {" << endl;
1826 sourcefile <<
" sum += " << functionName <<
"_act[source] * " << functionName <<
"_valW[CrtW++];" << endl;
1827 sourcefile <<
" }" << endl;
1828 sourcefile <<
" " << functionName <<
"_act[indNeurone++] = ActivationFunction(sum);" << endl;
1829 sourcefile <<
" }"<<endl;
1832 sourcefile <<
" // --- Output"<<endl;
1833 sourcefile <<
" for (int member = 0; member < nOutput; member++) {"<<endl;
1834 sourcefile <<
" sum = " << functionName <<
"_valW[0];"<<endl;
1835 sourcefile <<
" for (int source = 0; source < nHidden; source++) {"<<endl;
1836 sourcefile <<
" CrtW = source * ( nInput + 2) + 1;"<<endl;
1837 sourcefile <<
" sum += " << functionName <<
"_act[nInput+source] * " << functionName <<
"_valW[CrtW];"<<endl;
1838 sourcefile <<
" }"<<endl;
1839 sourcefile <<
" " << functionName <<
"_act[indNeurone++] = sum;"<<endl;
1842 sourcefile <<
" res[member] = " << functionName
1843 <<
"_minOutput[member] + 0.5 * ( " << functionName
1844 <<
"_maxOutput[member] - " << functionName
1845 <<
"_minOutput[member] ) * ( sum + 1.0);" << endl;
1849 sourcefile <<
" res[member] = " << functionName
1850 <<
"_minOutput[member] + " << functionName
1851 <<
"_maxOutput[member] * sum;" << endl;
1853 sourcefile <<
" }"<<endl;
1855 sourcefile <<
"}" << endl;
1866 void PMMLlib::ExportNeuralNetworkFortran(std::string file,
1867 std::string functionName,
1876 int nWeights = nHidden*(nInput+nOutput+1)+nOutput;
1879 vector<double> minInput(nInput);
1880 vector<double> maxInput(nInput);
1881 vector<double> minOutput(nOutput);
1882 vector<double> maxOutput(nOutput);
1883 vector<double> valW(nWeights);
1884 fillVectorsForExport(nInput,nOutput,nHidden,normType,minInput,maxInput,minOutput,maxOutput,valW);
1886 ofstream sourcefile(file.c_str());
1888 sourcefile <<
" SUBROUTINE " << functionName <<
"(";
1895 sourcefile <<
"C --- *********************************************" << endl;
1896 sourcefile <<
"C --- " << endl;
1898 header =
"C --- " + header;
1900 while ((pos = header.find(
"\n", pos)) != std::string::npos)
1902 header.replace(pos, 1,
"\nC --- ");
1905 sourcefile << header << endl;
1906 sourcefile <<
"C --- " << endl;
1907 sourcefile <<
"C --- *********************************************" << endl;
1909 sourcefile <<
" IMPLICIT DOUBLE PRECISION (V)" << endl;
1912 sourcefile <<
" DOUBLE PRECISION " <<
GetNameInput(
i) << endl;
1914 sourcefile <<
" DOUBLE PRECISION " <<
GetNameOutput(0) << endl;
1917 sourcefile <<
"C --- Preprocessing of the inputs" << endl;
1924 sourcefile <<
"2.D0 * ( " <<
GetNameInput(
i) <<
" - " << minInput[
i] <<
"D0 ) / " << maxInput[
i] - minInput[
i] <<
"D0 - 1.0" << endl;
1928 sourcefile <<
"( " <<
GetNameInput(
i) <<
" - " << minInput[
i] <<
"D0 ) / " << maxInput[
i] <<
"D0" << endl;
1934 sourcefile <<
"C --- Values of the weights" << endl;
1935 for(
int i=0 ;
i<nWeights ;
i++)
1937 sourcefile <<
" VW" <<
i+1 <<
" = " << valW[
i] << endl;
1941 for(
int member = 0; member < nHidden; member++)
1943 sourcefile <<
"C --- hidden neural number " << member+1 << endl;
1944 int CrtW = member * ( nInput + 2) + 3;
1945 sourcefile <<
" VAct" << member+1 <<
" = VW" << CrtW++ << endl;
1946 for (
int source = 0; source < nInput; source++)
1948 sourcefile <<
" 1 + VW"<< CrtW++ <<
" * VXN" <<
GetNameInput(source) << endl;
1955 sourcefile <<
" VPot" << member+1 <<
" = 2.D0 / (1.D0 + DEXP(-2.D0 * VAct" << member+1 <<
")) - 1.D0" << endl;
1959 sourcefile <<
" VPot" << member+1 <<
" = 1.D0 / (1.D0 + DEXP(-1.D0 * VAct" << member+1 <<
"))" << endl;
1965 sourcefile <<
"C --- Output" << endl;
1966 sourcefile <<
" VOut = VW1" << endl;
1967 for(
int source=0 ; source < nHidden ; source++)
1969 int CrtW = source * ( nInput + 2) + 2;
1970 sourcefile <<
" 1 + VW"<< CrtW <<
" * VPot" << source+1 << endl;
1975 sourcefile <<
"C --- Pretraitment of the output" << endl;
1978 sourcefile <<
" VDelta = " << 0.5*(maxOutput[0]-minOutput[0]) <<
"D0 * ( VOut + 1.0D0)" << endl;
1979 sourcefile <<
" " <<
GetNameOutput(0) <<
" = " << minOutput[0] <<
"D0 + VDelta" << endl;
1984 sourcefile <<
" " <<
GetNameOutput(0) <<
" = "<< minOutput[0] <<
"D0 + " << maxOutput[0] <<
"D0 * VOut;" << endl;
1988 sourcefile <<
"C --- " << endl;
1989 sourcefile <<
" RETURN" << endl;
1990 sourcefile <<
" END" << endl;
2002 void PMMLlib::ExportNeuralNetworkPython(std::string file,
2003 std::string functionName,
2008 ofstream exportfile(file.c_str());
2021 std::string PMMLlib::ExportNeuralNetworkPyStr(std::string functionName,
2032 int nNeurons = nInput+nOutput+nHidden;
2033 int nWeights = nHidden*(nInput+nOutput+1)+nOutput;
2036 vector<double> minInput(nInput);
2037 vector<double> maxInput(nInput);
2038 vector<double> minOutput(nOutput);
2039 vector<double> maxOutput(nOutput);
2040 vector<double> valW(nWeights);
2041 fillVectorsForExport(nInput,nOutput,nHidden,normType,minInput,maxInput,minOutput,maxOutput,valW);
2044 out <<
"#!/usr/bin/env python" << endl;
2045 out <<
"# -*- coding: utf-8 -*-" << endl;
2047 out <<
"from math import tanh, exp" << endl;
2053 out <<
"def ActivationFunction(sum): " << endl;
2054 out <<
" return tanh(sum); " << endl;
2058 out <<
"def ActivationFunction(sum): " << endl;
2059 out <<
" return ( 1.0 / ( 1.0 + exp( -1.0 * sum ) ) ); " << endl;
2063 out <<
"def " << functionName <<
"(param):" << endl;
2067 out <<
" ############################## " << endl;
2068 out <<
" #" << endl;
2070 header =
" # " + header;
2072 while ((pos = header.find(
"\n", pos)) != std::string::npos)
2074 header.replace(pos, 1,
"\n #");
2077 out << header << endl;
2078 out <<
" #" << endl;
2079 out <<
" ############################## " << endl;
2083 out <<
" nInput = " << nInput <<
";" << endl;
2084 out <<
" nOutput = " << nOutput <<
";" << endl;
2085 out <<
" nHidden = " << nHidden <<
";" << endl;
2086 out <<
" nNeurones = " << nNeurons <<
";" << endl;
2087 out <<
" " << functionName <<
"_act = [];" << endl;
2088 out <<
" res = [];" << endl;
2091 out <<
" # --- Preprocessing of the inputs and outputs" << endl;
2092 out <<
" " << functionName <<
"_minInput = [" << endl <<
" ";
2094 for(
int i=0 ;
i<nInput ;
i++)
2096 out << minInput[
i] <<
", ";
2103 out << endl <<
" ];" << endl;
2105 out <<
" " << functionName <<
"_minOutput = [" << endl <<
" ";
2106 out <<
" " << minOutput[0] ;
2107 out << endl <<
" ];" << endl;
2109 out <<
" " << functionName <<
"_maxInput = [" << endl <<
" ";
2110 for(
int i=0 ;
i<nInput ;
i++)
2112 out << maxInput[
i] <<
", ";
2119 out << endl <<
" ];" << endl;
2121 out <<
" " << functionName <<
"_maxOutput = [" << endl <<
" ";
2122 out <<
" " << maxOutput[0] ;
2123 out << endl <<
" ];" << endl;
2126 out <<
" # --- Values of the weights" << endl;
2127 out <<
" " << functionName <<
"_valW = [" << endl <<
" ";
2128 for(
int i=0 ;
i<nWeights ;
i++)
2130 out << valW[
i] <<
", ";
2137 out << endl <<
" ];"<<endl;
2139 out <<
" # --- Constants" << endl;
2140 out <<
" indNeurone = 0;" << endl;
2144 out <<
" # --- Input Layers" << endl;
2145 out <<
" for i in range(nInput) :" << endl;
2148 out <<
" " << functionName <<
"_act.append( 2.0 * ( param[i] - "
2149 << functionName <<
"_minInput[i] ) / ( " << functionName <<
"_maxInput[i] - "
2150 << functionName <<
"_minInput[i] ) - 1.0 ) ;"
2155 out <<
" " << functionName <<
"_act.append( ( param[i] - "
2156 << functionName <<
"_minInput[i] ) / " << functionName <<
"_maxInput[i] ) ;"
2159 out <<
" indNeurone += 1 ;" << endl;
2160 out <<
" pass" << endl;
2164 out <<
" # --- Hidden Layers" << endl;
2165 out <<
" for member in range(nHidden):" << endl;
2166 out <<
" CrtW = member * ( nInput + 2) + 2;" << endl;
2167 out <<
" sum = " << functionName <<
"_valW[CrtW];" << endl;
2168 out <<
" CrtW += 1 ;" << endl;
2169 out <<
" for source in range(nInput) :" << endl;
2170 out <<
" sum += " << functionName <<
"_act[source] * " << functionName <<
"_valW[CrtW];" << endl;
2171 out <<
" CrtW += 1 ;" << endl;
2172 out <<
" pass" << endl;
2173 out <<
" " << functionName <<
"_act.append( ActivationFunction(sum) ) ;" << endl;
2174 out <<
" indNeurone += 1 ;" << endl;
2175 out <<
" pass" << endl;
2179 out <<
" # --- Output"<<endl;
2180 out <<
" for member in range(nOutput):" << endl;
2181 out <<
" sum = " << functionName <<
"_valW[0];" << endl;
2182 out <<
" for source in range(nHidden):" << endl;
2183 out <<
" CrtW = source * ( nInput + 2) + 1;"<<endl;
2184 out <<
" sum += " << functionName <<
"_act[nInput+source] * " << functionName <<
"_valW[CrtW];" << endl;
2185 out <<
" pass" << endl;
2186 out <<
" " << functionName <<
"_act.append( sum );" << endl;
2187 out <<
" indNeurone += 1 ;" << endl;
2190 out <<
" res[member] = " << functionName
2191 <<
"_minOutput[member] + 0.5 * ( " << functionName
2192 <<
"_maxOutput[member] - " << functionName
2193 <<
"_minOutput[member] ) * ( sum + 1.0);" << endl;
2197 out <<
" res.append( " << functionName
2198 <<
"_minOutput[member] + " << functionName
2199 <<
"_maxOutput[member] * sum );" << endl;
2201 out <<
" pass" << endl;
2205 out <<
" return res;" << endl << endl;
2226 void PMMLlib::CheckRegression()
2229 throw string(
"Use this method with Regression models.");
2237 xmlNodePtr PMMLlib::GetRegressionPtr(std::string name)
2249 void PMMLlib::AddRegressionModel(std::string modelName,
2251 std::string targetFieldName)
2259 switch(functionName)
2262 function =
"regression";
2265 xmlNodePtr netNode = xmlNewChild(
_rootNode, 0, (
const xmlChar*)
"RegressionModel", 0);
2266 xmlNewProp(netNode, (
const xmlChar*)
"functionName", (
const xmlChar*)(
function.c_str()) );
2267 xmlNewProp(netNode, (
const xmlChar*)
"modelName", (
const xmlChar*)(
_currentModelName.c_str()) );
2268 xmlNewProp(netNode, (
const xmlChar*)
"targetFieldName", (
const xmlChar*)(targetFieldName.c_str()) );
2277 void PMMLlib::AddRegressionTable()
2280 xmlNodePtr tableNode = xmlNewChild(
_currentModelNode, 0, (
const xmlChar*)
"RegressionModel", 0);
2289 void PMMLlib::AddRegressionTable(
double intercept)
2294 xmlNodePtr tableNode = xmlNewChild(
_currentModelNode, 0, (
const xmlChar*)
"RegressionTable", 0);
2297 ss << scientific << intercept;
2298 xmlNewProp(tableNode, (
const xmlChar*)
"intercept", (
const xmlChar*)(ss.str().c_str()) );
2310 void PMMLlib::AddNumericPredictor(std::string neuronName,
2316 xmlNodePtr numPrecNode = xmlNewChild(
_currentNode, 0, (
const xmlChar*)
"NumericPredictor", 0);
2317 xmlNewProp(numPrecNode, (
const xmlChar*)
"name", (
const xmlChar*)(neuronName.c_str()) );
2318 ss.str(
""); ss << exponent;
2319 xmlNewProp(numPrecNode, (
const xmlChar*)
"exponent", (
const xmlChar*)(ss.str().c_str()) );
2320 ss.str(
""); ss << scientific << coefficient;
2321 xmlNewProp(numPrecNode, (
const xmlChar*)
"coefficient", (
const xmlChar*)(ss.str().c_str()) );
2330 void PMMLlib::AddPredictorTerm(
double coefficient,
2331 std::vector<std::string> fieldRef)
2335 xmlNodePtr predTermNode = xmlNewChild(
_currentNode, 0, (
const xmlChar*)
"PredictorTerm", 0);
2336 ss.str(
""); ss << scientific << coefficient;
2337 xmlNewProp(predTermNode, (
const xmlChar*)
"coefficient", (
const xmlChar*)(ss.str().c_str()) );
2338 vector<string>::iterator it;
2339 for(it=fieldRef.begin() ; it!=fieldRef.end() ; it++)
2341 xmlNodePtr fieldRefNode = xmlNewChild(predTermNode, 0, (
const xmlChar*)
"FieldRef", 0);
2342 ss.str(
""); ss << (*it);
2343 xmlNewProp(fieldRefNode, (
const xmlChar*)
"field", (
const xmlChar*)(ss.str().c_str()) );
2352 bool PMMLlib::HasIntercept()
2357 if ( tableNode == NULL )
2361 attr = xmlGetProp(tableNode, xp);
2377 double PMMLlib::GetRegressionTableIntercept()
2382 if ( tableNode == NULL )
2384 string strValue =
_getProp(tableNode,
string(
"intercept") );
2385 return atof(strValue.c_str());
2393 int PMMLlib::GetNumericPredictorNb()
2399 if ( tableNode == NULL )
2401 xmlNodePtr numPredNodes = tableNode->children;
2402 while (numPredNodes != NULL )
2404 if (
string((
const char*)(numPredNodes->name)) ==
"NumericPredictor" )
2406 numPredNodes = numPredNodes->next;
2416 int PMMLlib::GetPredictorTermNb()
2421 if ( tableNode == NULL )
2423 xmlNodePtr numPredNodes = tableNode->children;
2424 while ( numPredNodes != NULL )
2426 if (
string((
const char*)(numPredNodes->name)) ==
"PredictorTerm" )
2428 numPredNodes = numPredNodes->next;
2439 std::string PMMLlib::GetNumericPredictorName(
int num_pred_index)
2444 if ( numPredNodes == NULL )
2448 if ( numPredNodes == NULL )
2451 for(
int i=0;
i<num_pred_index;
i++)
2453 numPredNodes = numPredNodes->next;
2454 if ( numPredNodes == NULL ||
2455 string((
const char*)(numPredNodes->name)) !=
"NumericPredictor" )
2458 strName =
_getProp(numPredNodes,
string(
"name"));
2468 std::string PMMLlib::GetPredictorTermName(
int pred_term_index)
2473 if ( fieldRefNodes == NULL )
2477 if ( fieldRefNodes == NULL )
2480 for(
int i=0;
i<pred_term_index;
i++)
2482 fieldRefNodes = fieldRefNodes->next;
2483 if ( fieldRefNodes == NULL ||
2484 string((
const char*)(fieldRefNodes->name)) !=
"PredictorTerm" )
2488 fieldRefNodes = fieldRefNodes->children;
2489 while (fieldRefNodes != NULL)
2491 strName +=
_getProp(fieldRefNodes,
string(
"field"));
2492 fieldRefNodes = fieldRefNodes->next;
2504 double PMMLlib::GetNumericPredictorCoefficient(
int num_pred_index)
2510 if ( numPredNodes == NULL )
2513 if ( numPredNodes == NULL )
2516 for(
int i=0;
i<num_pred_index;
i++)
2518 numPredNodes = numPredNodes->next;
2519 if ( numPredNodes == NULL ||
2520 string((
const char*)(numPredNodes->name)) !=
"NumericPredictor" )
2523 string strValue =
_getProp(numPredNodes,
string(
"coefficient"));
2524 coef = atof(strValue.c_str());
2535 double PMMLlib::GetPredictorTermCoefficient(
int pred_term_index)
2541 if ( predTermNodes == NULL )
2544 if ( predTermNodes == NULL )
2547 for(
int i=0;
i<pred_term_index;
i++)
2549 predTermNodes = predTermNodes->next;
2550 if ( predTermNodes == NULL ||
2551 string((
const char*)(predTermNodes->name)) !=
"PredictorTerm" )
2554 string strValue =
_getProp(predTermNodes,
string(
"coefficient"));
2555 coef = atof(strValue.c_str());
2565 int PMMLlib::GetPredictorTermFieldRefNb(
int index)
2571 if ( fieldRefNodes == NULL )
2574 if ( fieldRefNodes == NULL )
2577 for(
int i=0;
i<index;
i++)
2579 fieldRefNodes = fieldRefNodes->next;
2580 if ( fieldRefNodes == NULL ||
2581 string((
const char*)(fieldRefNodes->name)) !=
"PredictorTerm" )
2584 fieldRefNodes = fieldRefNodes->children;
2585 while (fieldRefNodes != NULL)
2588 fieldRefNodes = fieldRefNodes->next;
2601 std::string PMMLlib::GetPredictorTermFieldRefName(
int pred_term_index,
int field_index)
2607 if ( fieldRefNodes == NULL )
2610 if ( fieldRefNodes == NULL )
2613 for(
int i=0;
i<pred_term_index;
i++)
2615 fieldRefNodes = fieldRefNodes->next;
2616 if ( fieldRefNodes == NULL ||
2617 string((
const char*)(fieldRefNodes->name)) !=
"PredictorTerm" )
2620 fieldRefNodes = fieldRefNodes->children;
2621 if ( fieldRefNodes == NULL )
2624 for(
int i=0;
i<field_index;
i++)
2626 fieldRefNodes = fieldRefNodes->next;
2627 if ( fieldRefNodes == NULL )
2630 strName =
_getProp(fieldRefNodes,
string(
"field"));
2641 void PMMLlib::ExportLinearRegressionCpp(std::string file,
2642 std::string functionName,
2648 ofstream exportfile(file.c_str());
2650 exportfile <<
"void " << functionName <<
"(double *param, double *res)" << endl;
2651 exportfile <<
"{" << endl;
2653 exportfile <<
" ////////////////////////////// " << endl;
2654 exportfile <<
" //" << endl;
2656 header =
" // " + header;
2658 while ((pos = header.find(
"\n", pos)) != std::string::npos)
2660 header.replace(pos, 1,
"\n //");
2663 exportfile << header << endl;
2664 exportfile <<
" //" << endl;
2665 exportfile <<
" ////////////////////////////// " << endl << endl;
2667 double intercept = 0.0;
2670 exportfile <<
" // Intercept"<< endl;
2674 exportfile <<
" // No Intercept"<< endl;
2675 exportfile <<
" double y = " << intercept <<
";";
2676 exportfile << endl << endl;
2679 for (
int i=0;
i<nPred;
i++)
2683 exportfile << endl << endl;
2686 for (
int i=0;
i<nPred;
i++)
2690 exportfile << endl << endl;
2693 exportfile <<
" // Return the value"<< endl;
2694 exportfile <<
" res[0] = y;" << endl;
2695 exportfile <<
"}" << endl;
2706 void PMMLlib::ExportLinearRegressionFortran(std::string file,
2707 std::string functionName,
2714 vector<string>strParam(nNumPred+nPredTerm);
2715 for(
int i=0;
i<(nNumPred+nPredTerm);
i++)
2721 ofstream exportfile(file.c_str());
2723 exportfile <<
" SUBROUTINE " << functionName <<
"(";
2724 for(
int i=0;
i<(nNumPred+nPredTerm);
i++)
2726 exportfile << strParam[
i] <<
", ";
2728 exportfile <<
"RES)" << endl;
2731 exportfile <<
"C --- *********************************************" << endl;
2732 exportfile <<
"C --- " << endl;
2734 header =
"C --- " + header;
2736 while ((pos = header.find(
"\n", pos)) != std::string::npos)
2738 header.replace(pos, 1,
"\nC --- ");
2741 exportfile << header << endl;
2742 exportfile <<
"C --- " << endl;
2743 exportfile <<
"C --- *********************************************" << endl << endl;
2745 exportfile <<
" IMPLICIT DOUBLE PRECISION (P)" << endl;
2746 exportfile <<
" DOUBLE PRECISION RES" << endl;
2747 exportfile <<
" DOUBLE PRECISION Y" << endl;
2750 double intercept = 0.0;
2753 exportfile <<
"C --- Intercept"<< endl;
2757 exportfile <<
"C --- No Intercept"<< endl;
2758 exportfile <<
" Y = " << intercept <<
";";
2759 exportfile << endl << endl;
2761 for (
int i=0;
i<nNumPred;
i++)
2765 exportfile << endl << endl;
2768 for (
int i=0;
i<nPredTerm;
i++)
2772 exportfile << endl << endl;
2775 exportfile <<
"C --- Return the value"<< endl;
2776 exportfile <<
" RES = Y " << endl;
2777 exportfile <<
" RETURN" << endl;
2778 exportfile <<
" END" << endl;
2789 void PMMLlib::ExportLinearRegressionPython(std::string file,
2790 std::string functionName,
2795 ofstream exportfile(file.c_str());
2806 std::string PMMLlib::ExportLinearRegressionPyStr(std::string functionName,
2814 out <<
"#!/usr/bin/env python" << endl;
2815 out <<
"# -*- coding: utf-8 -*-" << endl;
2819 out <<
"def " << functionName <<
"(param):" << endl;
2823 out <<
" ############################## " << endl;
2824 out <<
" # " << endl;
2826 header =
" # " + header;
2828 while ((pos = header.find(
"\n", pos)) != std::string::npos)
2830 header.replace(pos, 1,
"\n #");
2833 out << header << endl;
2834 out <<
" # " << endl;
2835 out <<
" ############################## " << endl << endl;
2837 double intercept = 0.0;
2840 out <<
" # Intercept"<< endl;
2844 out <<
" # No Intercept"<< endl;
2845 out <<
" y = " << intercept <<
";";
2846 out << endl << endl;
2849 for (
int i=0;
i<nPred;
i++)
2853 out << endl << endl;
2856 for (
int i=0;
i<nPred;
i++)
2860 out << endl << endl;
2863 out <<
" # Return the value"<< endl;
2864 out <<
" return [y];" << endl;
2874 std::string PMMLlib::ReadRegressionStructure()
2878 string structure(
"");
2879 string structureActive(
"");
2880 string structurePredicted(
"@");
2883 if ( mNode != NULL )
2886 while (dNode != NULL)
2888 string name =
_getProp(dNode,
string(
"name"));
2889 string usage =
_getProp(dNode,
string(
"usageType"));
2890 if ( usage ==
"active" )
2892 structureActive += name;
2893 structureActive +=
":";
2895 else if ( usage ==
"predicted" )
2897 structurePredicted += name;
2898 structurePredicted +=
":";
2901 dNode = dNode->next;
2904 if ( structureActive.length() > 0 )
2905 structureActive.erase(structureActive.size()-1);
2906 structurePredicted.erase(structurePredicted.size()-1);
2908 std::ostringstream oss;
2910 structure = structureActive +
"," + oss.str() +
"," + structurePredicted;