Ejemplo n.º 1
0
        public void saveTest(string pFilePath)
        {
            // Load
            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);
            string temp_file = Path.GetTempFileName();

            // Save
            pmml.save(temp_file);

            Pmml pmml2 = Pmml.loadModels(temp_file);

            // Test data dictionnary
            Assert.AreEqual(pmml.DataDictionary.DataFields.Count, pmml2.DataDictionary.DataFields.Count);

            // Test models
            Assert.AreEqual(pmml.Models.Count, pmml2.Models.Count);
            for (int j = 0; j < pmml.Models.Count; j++)
            {
                ModelElement model  = pmml.Models[j];
                ModelElement model2 = pmml2.Models[j];

                Assert.IsInstanceOf(model.GetType(), model2);

                if (model is TreeModel)
                {
                }
            }
        }
Ejemplo n.º 2
0
        public void ScoreExample7Test()
        {
            string pFilePath = "test-golfing2.xml";
            string modelname = "golfing";
            string paramList = "outlook=\"sunny\"";

            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            TreeModel tree = (TreeModel)pmml.getByName(modelname);

            Assert.NotNull(tree);

            // Modification for NullPrediction
            tree.MissingValueStrategy = MissingValueStrategy.NullPrediction;

            Dictionary <string, object> lDict = parseParams(paramList);

            ScoreResult result = tree.Score(lDict);

            Assert.NotNull(result);

            Assert.IsNull(result.Value);
        }
Ejemplo n.º 3
0
        public int ar = 0;                                    //Number Of AR Fields

        //this class gets the XML Document file and a Dictionary of input values with Field name
        //the data preparations are done

        public Fields(Pmml doc)
        {
            xmlDoc      = doc;
            MiningField = doc.getList("MiningField");

            //to get the target field name
            for (int i = 0; i < MiningField.Size(); i++)
            {
                if (MiningField.getNode(i).Attribute("usageType") != null && MiningField.getNode(i).Attribute("usageType") == "predicted")
                {
                    targetName = MiningField.getNode(i).Attribute("name");
                }
                else
                {
                    FieldName.Add(MiningField.getNode(i).Attribute("name"));
                }
            }

            for (int i = 0; i < FieldName.Count; i++)
            {
                for (int j = 1; j <= FieldName.Count; j++)
                {
                    if (FieldName[i].Equals("AR" + j))
                    {
                        ar = ar + 1;
                        break;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void SimplePredicateIsNotMissingTest()
        {
            string      pmmlStr = @"<?xml version=""1.0"" ?>
<PMML version=""4.1"" xmlns=""http://www.dmg.org/PMML-4_1"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
	<Header copyright=""www.dmg.org"" description=""A very small tree model to test isNotMissing operator.""/>
	<DataDictionary numberOfFields=""2"" >
		<DataField name=""in"" optype=""continuous"" dataType=""double""/>
		<DataField name=""out"" optype=""continuous"" dataType=""double""/>
	</DataDictionary>
	<TreeModel modelName=""SimpleIsNotMissingTest"" functionName=""classification"">
		<MiningSchema>
			<MiningField name=""in""/>
			<MiningField name=""out"" usageType=""predicted""/>
		</MiningSchema>
		<Node score=""0"">
			<True/>
			<Node score=""1"">
				<SimplePredicate field=""in"" operator=""isNotMissing"" />
			</Node>
		</Node>
	</TreeModel>
</PMML>
";
            XmlDocument xml     = new XmlDocument();

            xml.LoadXml(pmmlStr);
            Pmml pmml = Pmml.loadModels(xml);

            ScoreResult res = pmml.getByName("SimpleIsNotMissingTest").Score(parseParams("  in=\"foo\"  "));

            Assert.AreEqual("1", res.Value);
        }
Ejemplo n.º 5
0
                  "Recurrent", 0)]         // confidence = 83.18%
        public void ScoreTest(string pFilePath, string modelname, string paramList, string res, decimal confidence)
        {
            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            ModelElement model = pmml.getByName(modelname);

            Assert.NotNull(model);

            Assert.IsInstanceOf(typeof(TreeModel), model);
            TreeModel tree = (TreeModel)model;

            Dictionary <string, object> lDict = parseParams(paramList);

            ScoreResult result = tree.Score(lDict);

            Assert.NotNull(result);


            /*foreach(Node item in result.Nodes)
             * {
             *      Console.WriteLine("Node {0} = score {1}", item.Id, item.Score);
             *
             *      foreach(ScoreDistribution it2 in item.ScoreDistributions)
             *              Console.WriteLine("\tScore Dist. {0} ({1}) = {2}", it2.Value, it2.RecordCount, it2.Confidence);
             * }*/

            Assert.AreEqual(res, result.Value);
            Assert.AreEqual(confidence, result.Confidence);
        }
Ejemplo n.º 6
0
        public void RandomTest(string pFilePath)
        {
            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            ScoreResult res = pmml.Models[0].Score(new Dictionary <string, object>());

            Assert.IsNotNull(res);
        }
Ejemplo n.º 7
0
        public void TreeModelsTest(string filePath, string modelName, int nbTreemodels)
        {
            Pmml pmml = Pmml.loadModels(filePath);

            Assert.NotNull(pmml);

            Assert.NotNull(pmml.getByName(modelName));

            Assert.AreEqual(nbTreemodels, pmml.Models.Count);
        }
Ejemplo n.º 8
0
        public Fields(Dictionary <string, object> inputDic, Pmml doc)
        {
            xmlDoc      = doc;
            MiningField = doc.getList("MiningField");

            //to get the target field name
            for (int i = 0; i < MiningField.Size(); i++)
            {
                if (MiningField.getNode(i).Attribute("usageType") != null && MiningField.getNode(i).Attribute("usageType") == "predicted")
                {
                    targetName = MiningField.getNode(i).Attribute("name");
                }
                else
                {
                    FieldName.Add(MiningField.getNode(i).Attribute("name"));
                }
            }

            for (int i = 0; i < FieldName.Count; i++)
            {
                for (int j = 1; j <= FieldName.Count; j++)
                {
                    if (FieldName[i].Equals("AR" + j))
                    {
                        ar = ar + 1;
                        break;
                    }
                }
            }

            DerivedField = doc.getList("DerivedField");

            //to add transformed values to list
            for (int i = 0; i < DerivedField.Size() / 2; i++)
            {
                double value = 0;
                if (DerivedField.getNode(i).Child(0).Attribute("field") != targetName)
                {
                    object inp = inputDic[DerivedField.getNode(i).Child(0).Attribute("field")];
                    switch (DerivedField.getNode(i).Child(0).Name())
                    {
                    case "NormDiscrete": value = NormDiscrete(Convert.ToString(inp), DerivedField.getNode(i));
                        break;

                    case "NormContinuous": value = LinearNorm(Convert.ToDouble(inp), DerivedField.getNode(i));
                        break;
                    }
                    derFieldsList.Add(value);
                }
            }

            netInputs = derFieldsList.ToArray();
        }
Ejemplo n.º 9
0
        public void ScoreExample1Test()
        {
            string  pFilePath  = "test-ruleset1.xml";
            string  modelname  = "NestedDrug";
            string  paramList  = "BP='HIGH', K=0.0621, Age = 36, Na = 0.5023";
            string  res        = "may play";
            decimal confidence = 0.47M;

            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            ModelElement model = pmml.getByName(modelname);

            Assert.NotNull(model);

            Assert.IsInstanceOf <RuleSetModel>(model);

            RuleSetModel rs = (RuleSetModel)model;

            // Check exemple 1 as 3 RuleSelectionMethod for first node
            Assert.IsNotNull(rs.RuleSet);
            Assert.IsNotNull(rs.RuleSet.RuleSelectionMethods);
            Assert.AreEqual(3, rs.RuleSet.RuleSelectionMethods.Count);

            // Check 3 Rule
            Assert.AreEqual(3, rs.RuleSet.Rules.Count);

            // Modification for aggregateNode
            //tree.MissingValueStrategy = MissingValueStrategy.AggregateNodes;

            Dictionary <string, object> lDict = parseParams(paramList);

            ScoreResult result = rs.Score(lDict);

            Assert.NotNull(result);


            /*foreach(Node item in result.Nodes)
             * {
             *      Console.WriteLine("Node {0} = score {1}", item.Id, item.Score);
             *
             *      foreach(ScoreDistribution it2 in item.ScoreDistributions)
             *              Console.WriteLine("\tScore Dist. {0} ({1}) = {2}", it2.Value, it2.RecordCount, it2.Confidence);
             * }*/

            Assert.AreEqual(res, result.Value);
            Assert.AreEqual(confidence, result.Confidence);
        }
Ejemplo n.º 10
0
        string cMeasure; //Comparison Measure Name

        public ClusterModel(string dir)
        {
            doc = new Pmml(dir);
            NodeList ClusteringModel = doc.getList("ClusteringModel");

            for (int i = 0; i < ClusteringModel.getNode(0).ChildCount("Cluster"); i++)
            {
                clusterList.Add(new Cluster(ClusteringModel.getNode(0).Child(i, "Cluster")));
            }

            NodeList ComparisonMeasure = doc.getList("ComparisonMeasure");

            kind     = ComparisonMeasure.getNode(0).Attribute("kind");
            cMeasure = ComparisonMeasure.getNode(0).Child(0).Name();
        }
Ejemplo n.º 11
0
        //the constructor of Model gets the derivedField from XML file, the default Activation Function
        //and inserts the layers of network to a dynamic list
        public NNModel(string dir)
        {
            doc    = new Pmml(dir);
            fields = new Fields(doc);

            nNet        = doc.getList("NeuralNetwork");
            defualtFunc = nNet.getNode(0).Attribute("activationFunction");

            for (int i = 0; i < doc.getList("NeuralLayer").Size(); i++)
            {
                Layer layer = new Layer(doc.getList("NeuralLayer").getNode(i));
                layer.defFunc = defualtFunc;
                layers.Add(layer);
            }
        }
Ejemplo n.º 12
0
        public void ScoreCarfTest(string paramList, string res, decimal confidence)
        {
            string pFilePath = "models\\RuleSetCarrefour.xml";
            string modelname = "CARF-20140124";

            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            ModelElement model = pmml.getByName(modelname);

            Assert.NotNull(model);

            Assert.IsInstanceOf <RuleSetModel>(model);

            RuleSetModel rs = (RuleSetModel)model;

            // Check CARF as 1 RuleSelectionMethod for first node
            Assert.IsNotNull(rs.RuleSet);
            Assert.IsNotNull(rs.RuleSet.RuleSelectionMethods);
            Assert.AreEqual(1, rs.RuleSet.RuleSelectionMethods.Count);
            Assert.AreEqual("firstHit", rs.RuleSet.RuleSelectionMethods[0].Criterion);

            // Check 11 Rule
            Assert.AreEqual(11, rs.RuleSet.Rules.Count);

            // Modification for aggregateNode
            //tree.MissingValueStrategy = MissingValueStrategy.AggregateNodes;

            Dictionary <string, object> lDict = parseParams(paramList);
            //for

            ScoreResult result = rs.Score(lDict);

            Assert.NotNull(result);


            /*foreach(Node item in result.Nodes)
             * {
             *      Console.WriteLine("Node {0} = score {1}", item.Id, item.Score);
             *
             *      foreach(ScoreDistribution it2 in item.ScoreDistributions)
             *              Console.WriteLine("\tScore Dist. {0} ({1}) = {2}", it2.Value, it2.RecordCount, it2.Confidence);
             * }*/

            Assert.AreEqual(res, result.Value);
            Assert.AreEqual(confidence, result.Confidence);
        }
Ejemplo n.º 13
0
        public void LoadModelsTest(string pFilePath)
        {
            // Try from string
            Assert.NotNull(Pmml.loadModels(pFilePath));

            // Same but with file info
            FileInfo info = new FileInfo(pFilePath);

            Assert.NotNull(Pmml.loadModels(info));

            // Test the Xml constructor
            XmlDocument xml = new XmlDocument();

            xml.Load(pFilePath);
            Assert.NotNull(Pmml.loadModels(xml));
        }
Ejemplo n.º 14
0
        public void LoadMiningModelTest(string pFilePath)
        {
            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            Assert.AreEqual(1, pmml.Models.Count);

            ModelElement model = pmml.Models[0];

            Assert.IsInstanceOf(typeof(MiningModel), pmml.Models[0]);

            MiningModel miningModel = (MiningModel)model;

            Assert.AreEqual(3, miningModel.Segmentation.Segments.Count);
        }
Ejemplo n.º 15
0
        public TreeModel(string dir)
        {
            Pmml     doc   = new Pmml(dir);
            NodeList nodes = doc.getList("Node");

            fields = new Fields(doc.getList("DataField"));

            for (int i = 0; i < nodes.Size(); i++)
            {
                TreeNode n  = new TreeNode(nodes.getNode(i));
                int      id = Convert.ToInt16(nodes.getNode(i).Attribute("id"));
                n.id   = id;
                n.cID  = getChildren(nodes.getNode(i));
                n.type = fields.type;
                nodesDic.Add(id, n);
            }
        }
Ejemplo n.º 16
0
        public void SimpleSetPredicateTest(string pFilePath, string paramList, string res)
        {
            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            TreeModel tree = (TreeModel)pmml.getByName("SimpleSetTest");

            Assert.NotNull(tree);

            Dictionary <string, object> lDict = parseParams(paramList);

            ScoreResult result = tree.Score(lDict);

            Assert.NotNull(result);


            Assert.AreEqual(2, result.Nodes.Count);

            Assert.AreEqual(res, result.Value);
        }
Ejemplo n.º 17
0
        public Fields(Dictionary <string, object> inputDic, Pmml doc)
        {
            DerivedField    = doc.getList("DerivedField");
            ClusteringField = doc.getList("ClusteringField");
            derivedFields   = new double[DerivedField.Size()];


            for (int i = 0; i < DerivedField.Size(); i++)
            {
                switch (DerivedField.getNode(i).Child(0).Name())
                {
                case "NormContinuous":
                {
                    object inp = inputDic[DerivedField.getNode(i).Child(0).Attribute("field")];
                    derivedFields[i] = LinearNorm(Convert.ToDouble(inp), DerivedField.getNode(i));
                }
                break;

                case "MapValues":
                {
                    object inp = inputDic[DerivedField.getNode(i).Child(0).Child(0, "FieldColumnPair").Attribute("field")];
                    derivedFields[i] = MapValue(Convert.ToString(inp), DerivedField.getNode(i).Child(0));
                }
                break;
                }
            }

            ClusteringField = doc.getList("ClusteringField");
            List <string> tmp = new List <string>();

            for (int i = 0; i < ClusteringField.Size(); i++)
            {
                if (ClusteringField.getNode(i).Attribute("isCenterField") != null && ClusteringField.getNode(i).Attribute("isCenterField") == "true")
                {
                    tmp.Add(ClusteringField.getNode(i).Attribute("compareFunction"));
                }
            }
            compareFunction = tmp.ToArray();
        }
Ejemplo n.º 18
0
        public void loadTest(string pFilePath)
        {
            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            TreeModel tree = (TreeModel)pmml.getByName("golfing");

            Assert.NotNull(tree);

            // Test first Node
            Node nodeFirst = tree.Node;

            Assert.NotNull(nodeFirst);
            Assert.NotNull(nodeFirst.Predicate);
            Assert.IsTrue(nodeFirst.Predicate is TruePredicate);
            Assert.AreEqual(2, nodeFirst.Nodes.Count);

            //
            Assert.AreEqual(2, tree.Node.Nodes[0].Nodes.Count);
            Assert.AreEqual(2, tree.Node.Nodes[1].Nodes.Count);
        }
Ejemplo n.º 19
0
        public void NoTrueChildStrategyReturnLastPredictionTest()
        {
            string      pmmlStr = @"<?xml version=""1.0"" ?>
<PMML version=""4.1"" xmlns=""http://www.dmg.org/PMML-4_1"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
	<Header copyright="""" description=""A very small tree model to test noTrueChildStrategy attribute.""/>
	<DataDictionary numberOfFields=""2"" >
		<DataField name=""prob1"" optype=""continuous"" dataType=""double""/>
		<DataField name=""out"" optype=""continuous"" dataType=""double""/>
	</DataDictionary>
	<TreeModel modelName=""Test"" functionName=""classification"" noTrueChildStrategy=""returnLastPrediction"" >
		<MiningSchema>
			<MiningField name=""prob1""/>
			<MiningField name=""out"" usageType=""predicted""/>
		</MiningSchema>
		<Node id=""N1"" score=""0"">
			<True/>
			<Node id=""T1"" score=""1"">
				<SimplePredicate field=""prob1"" operator=""greaterThan"" value=""0.33"" />
			</Node>
		</Node>
	</TreeModel>
</PMML>
";
            XmlDocument xml     = new XmlDocument();

            xml.LoadXml(pmmlStr);
            Pmml pmml = Pmml.loadModels(xml);

            TreeModel model = (TreeModel)pmml.getByName("Test");

            // Test that = returnNullPrediction
            Assert.AreEqual(NoTrueChildStrategy.ReturnLastPrediction, model.NoTrueChildStrategy);

            ScoreResult res = model.Score(parseParams("  prob1=0.25  "));

            Assert.AreEqual("0", res.Value);
        }
Ejemplo n.º 20
0
        public void ScoreExample8Test()
        {
            string  pFilePath  = "test-golfing2.xml";
            string  modelname  = "golfing";
            string  paramList  = "temperature=45, humidity=90";
            string  res        = "may play";
            decimal confidence = 0.47M;

            Pmml pmml = Pmml.loadModels(pFilePath);

            Assert.NotNull(pmml);

            TreeModel tree = (TreeModel)pmml.getByName(modelname);

            Assert.NotNull(tree);

            // Modification for aggregateNode
            tree.MissingValueStrategy = MissingValueStrategy.AggregateNodes;

            Dictionary <string, object> lDict = parseParams(paramList);

            ScoreResult result = tree.Score(lDict);

            Assert.NotNull(result);


            /*foreach(Node item in result.Nodes)
             * {
             *      Console.WriteLine("Node {0} = score {1}", item.Id, item.Score);
             *
             *      foreach(ScoreDistribution it2 in item.ScoreDistributions)
             *              Console.WriteLine("\tScore Dist. {0} ({1}) = {2}", it2.Value, it2.RecordCount, it2.Confidence);
             * }*/

            Assert.AreEqual(res, result.Value);
            Assert.AreEqual(confidence, result.Confidence);
        }
        public ActionResult Consultar(List <ConsultaPmml> atributos)
        {
            try
            {
                ViewBag.PossuiNosFilhos = true;
                var         arvoreGeradaId = atributos[0].ArvoreGeradaId;
                var         arvoreGerada   = db.ArvoreGerada.FirstOrDefault(arvore => arvore.ID == arvoreGeradaId);
                XmlDocument doc;
                var         xml = new System.Xml.Serialization.XmlSerializer(typeof(XmlDocument));
                using (TextReader reader = new StringReader(arvoreGerada.XmlPmml))
                {
                    doc = (XmlDocument)xml.Deserialize(reader);
                }

                doc.Save(HostingEnvironment.MapPath("/Content/pmml.xml"));
                Pmml pmml = Pmml.loadModels(HostingEnvironment.MapPath("/Content/pmml.xml"));
                //busca o modelo
                ModelElement model = pmml.Models[0];

                Dictionary <string, object> dict = new Dictionary <string, object>();
                foreach (var atr in atributos)
                {
                    //Somente utiliza os fields ativos, pois o de predição não deve ser preenchido
                    if (!atr.ClasseMeta)
                    {
                        if (atr.Valor != null)
                        {
                            dict.Add(atr.Nome, atr.Valor);
                        }
                        else
                        {
                            ModelState.AddModelError("", "Campo " + atr.Label + " não foi preenchido.");
                            return(View(atributos));
                        }
                    }
                }

                ScoreResult result = model.Score(dict);
                if (result.Value != null)
                {
                    Session.Clear();
                    Session.Add("ConsultaPMML", atributos);
                    ViewBag.Resultado = result.Value;

                    Arvore arv;
                    var    xmlArvore = new System.Xml.Serialization.XmlSerializer(typeof(Arvore));
                    using (StringReader textWriter = new StringReader(arvoreGerada.XML))
                    {
                        arv = xmlArvore.Deserialize(textWriter) as Arvore;
                    }

                    //Verificar se possui filhos para esconder o avançar
                    foreach (var no in arv.NoRaiz.NosFilhos)
                    {
                        if (atributos[1].Nome == no.Nome && no.Valor == atributos[1].Valor)
                        {
                            if (no.NosFilhos.Count <= 0)
                            {
                                ViewBag.PossuiNosFilhos = false;
                            }

                            if (atributos.Count > 2)
                            {
                                VerificaProximoNivel(no, atributos, 2);
                            }

                            break;
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Consulta PMML não obteve resultado. Favor verificar as informações preenchidas.");
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Consulta PMML não foi possível. Favor verificar as informações preenchidas.");
            }

            return(View(atributos));
        }