From e1b0c3f258c65aa386266cd72b2ecc67dd9e77da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Maldonado=20S=C3=A1nchez?= Date: Fri, 18 Nov 2022 14:04:18 +0100 Subject: [PATCH] Agregar archivos de proyecto. --- testML.sln | 25 ++++++ testML/App.config | 14 ++++ testML/Data.cs | 36 +++++++++ testML/Program.cs | 124 +++++++++++++++++++++++++++++ testML/Properties/AssemblyInfo.cs | 36 +++++++++ testML/packages.config | 16 ++++ testML/testML.csproj | 126 ++++++++++++++++++++++++++++++ 7 files changed, 377 insertions(+) create mode 100644 testML.sln create mode 100644 testML/App.config create mode 100644 testML/Data.cs create mode 100644 testML/Program.cs create mode 100644 testML/Properties/AssemblyInfo.cs create mode 100644 testML/packages.config create mode 100644 testML/testML.csproj diff --git a/testML.sln b/testML.sln new file mode 100644 index 0000000..ba74504 --- /dev/null +++ b/testML.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32922.545 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testML", "testML\testML.csproj", "{C3E80D81-F268-427C-B77D-DB08422C978F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C3E80D81-F268-427C-B77D-DB08422C978F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C3E80D81-F268-427C-B77D-DB08422C978F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C3E80D81-F268-427C-B77D-DB08422C978F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C3E80D81-F268-427C-B77D-DB08422C978F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0128A1B5-B749-41FC-A339-B9B621240682} + EndGlobalSection +EndGlobal diff --git a/testML/App.config b/testML/App.config new file mode 100644 index 0000000..e77a99d --- /dev/null +++ b/testML/App.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/testML/Data.cs b/testML/Data.cs new file mode 100644 index 0000000..dd02692 --- /dev/null +++ b/testML/Data.cs @@ -0,0 +1,36 @@ +using Microsoft.ML.Data; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace testML +{ + public class Data + { + public string Accession { get; set; } + + public float Enum1 { get; set; } + + public float Enum2 { get; set; } + + public float Enum3 { get; set; } + + public float Enum4 { get; set; } + + public float DecimalNumber { get; set; } + + public float IntegerNumber { get; set; } + + public float OrigenResultNumber { get; set; } + + public string StringTest { get; set; } + } + + public class DataPrediction + { + [ColumnName("Score")] + public float IntegerNumber { get; set; } + } +} diff --git a/testML/Program.cs b/testML/Program.cs new file mode 100644 index 0000000..3a96d18 --- /dev/null +++ b/testML/Program.cs @@ -0,0 +1,124 @@ +using Microsoft.ML; +using Microsoft.ML.Data; +using Microsoft.ML.Trainers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace testML +{ + internal class Program + { + static string[] tags = new string[] { "Rojo", "Amarillo", "Verde claro", "Verde oscuro", "Violeta", "Naranja", "Azul", "Blanco" }; + static Random rnd = new Random(); + + static void Main(string[] args) + { + + + + var tmpData = new List(); + + + for (var c = 0; c < 300; c++) + { + var d = CreateRandomData(); + tmpData.Add(d); + } + + + + MLContext mlContext = new MLContext(); + + var data = mlContext.Data.LoadFromEnumerable(tmpData); + + + DataOperationsCatalog.TrainTestData dataSplit = mlContext.Data.TrainTestSplit(data, testFraction: 0.2); + IDataView trainData = dataSplit.TrainSet; + IDataView testData = dataSplit.TestSet; + + + var trainer = mlContext.Regression.Trainers.Sdca(maximumNumberOfIterations:1000); + //var trainer = mlContext.Regression.Trainers.OnlineGradientDescent(numberOfIterations: 100, learningRate: 0.01f ); + + + var pipeline = mlContext.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: "IntegerNumber") + .Append(mlContext.Transforms.Text.NormalizeText("StringTest")) + .Append(mlContext.Transforms.Text.FeaturizeText("StringTest")) + .Append(mlContext.Transforms.Concatenate("Features", "Enum1", "Enum2", "Enum3", "Enum4", "StringTest")) + .Append(mlContext.Transforms.NormalizeMinMax("Features")) + .Append(trainer); + + + + ITransformer model = pipeline.Fit(trainData); + + + // Use trained model to make inferences on test data + IDataView testDataPredictions = model.Transform(testData); + + // Extract model metrics and get RSquared + RegressionMetrics trainedModelMetrics = mlContext.Regression.Evaluate(testDataPredictions); + double rSquared = trainedModelMetrics.RSquared; + + Console.WriteLine("ModelMetrics: {0}", rSquared); + + + + var predictionFunction = mlContext.Model.CreatePredictionEngine(model); + + + for (var c = 0; c < 1000; c++) + { + + var test = CreateRandomData(); + var expected = test.IntegerNumber; + test.IntegerNumber = 0; + + var p = predictionFunction.Predict(test); + + Console.WriteLine("Found: {0:#,##0.00}\tExpected: {1:#,##0.00}\t\tDiff: {2:#,##0.00}", p.IntegerNumber, expected , expected- p.IntegerNumber); + } + + } + + private static Data CreateRandomData() + { + var d = new Data() + { + Accession = rnd.Next(0, 99999999).ToString("00000000"), + Enum1 = rnd.Next(1, 4), + Enum2 = rnd.Next(1, 11), + Enum3 = rnd.Next(1, 6), + Enum4 = rnd.Next(1, 6), + StringTest = tags[rnd.Next(0, tags.Length)] + }; + + // Ponemos algunos datos que tengan alguna relación (la red neuronal debería calibrarse para comprender esta formula) + d.IntegerNumber = (((d.Enum1 + d.Enum2) - (d.Enum3 + d.Enum4)) * 5.25f) + d.StringTest.Length; + + d.DecimalNumber = (d.Enum2 / d.Enum1) * (2.0f + (1.0f / d.StringTest.Length)); + + if (d.StringTest == "Azul") + { + d.IntegerNumber += 10; + d.OrigenResultNumber = 1; + } + + if (d.StringTest == "Rojo") + { + d.IntegerNumber += 5f; + d.OrigenResultNumber = 1; + } + + return d; + + } + } + + +} diff --git a/testML/Properties/AssemblyInfo.cs b/testML/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b6d3991 --- /dev/null +++ b/testML/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// La información general de un ensamblado se controla mediante el siguiente +// conjunto de atributos. Cambie estos valores de atributo para modificar la información +// asociada a un ensamblado. +[assembly: AssemblyTitle("testML")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("testML")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Si establece ComVisible en false, los tipos de este ensamblado no estarán visibles +// para los componentes COM. Si es necesario obtener acceso a un tipo en este ensamblado desde +// COM, establezca el atributo ComVisible en true en este tipo. +[assembly: ComVisible(false)] + +// El siguiente GUID sirve como id. de typelib si este proyecto se expone a COM. +[assembly: Guid("c3e80d81-f268-427c-b77d-db08422c978f")] + +// La información de versión de un ensamblado consta de los cuatro valores siguientes: +// +// Versión principal +// Versión secundaria +// Número de compilación +// Revisión +// +// Puede especificar todos los valores o usar los valores predeterminados de número de compilación y de revisión +// utilizando el carácter "*", como se muestra a continuación: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/testML/packages.config b/testML/packages.config new file mode 100644 index 0000000..1116d1d --- /dev/null +++ b/testML/packages.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testML/testML.csproj b/testML/testML.csproj new file mode 100644 index 0000000..b5a7157 --- /dev/null +++ b/testML/testML.csproj @@ -0,0 +1,126 @@ + + + + + + + Debug + AnyCPU + {C3E80D81-F268-427C-B77D-DB08422C978F} + Exe + testML + testML + v4.8 + 512 + true + + + + + + x64 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + testML.Program + + + + ..\packages\Microsoft.ML.2.0.0\lib\netstandard2.0\Microsoft.ML.dll + + + ..\packages\Microsoft.ML.2.0.0\lib\netstandard2.0\Microsoft.ML.Core.dll + + + ..\packages\Microsoft.ML.CpuMath.2.0.0\lib\netstandard2.0\Microsoft.ML.CpuMath.dll + + + ..\packages\Microsoft.ML.2.0.0\lib\netstandard2.0\Microsoft.ML.Data.dll + + + ..\packages\Microsoft.ML.DataView.2.0.0\lib\netstandard2.0\Microsoft.ML.DataView.dll + + + ..\packages\Microsoft.ML.2.0.0\lib\netstandard2.0\Microsoft.ML.KMeansClustering.dll + + + ..\packages\Microsoft.ML.2.0.0\lib\netstandard2.0\Microsoft.ML.PCA.dll + + + ..\packages\Microsoft.ML.2.0.0\lib\netstandard2.0\Microsoft.ML.StandardTrainers.dll + + + ..\packages\Microsoft.ML.2.0.0\lib\netstandard2.0\Microsoft.ML.Transforms.dll + + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + + ..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll + + + ..\packages\System.CodeDom.4.5.0\lib\net461\System.CodeDom.dll + + + ..\packages\System.Collections.Immutable.1.5.0\lib\netstandard2.0\System.Collections.Immutable.dll + + + + ..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Channels.4.7.1\lib\net461\System.Threading.Channels.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + + + + + + + + + + + + + + + + + + + + + Este proyecto hace referencia a los paquetes NuGet que faltan en este equipo. Use la restauración de paquetes NuGet para descargarlos. Para obtener más información, consulte http://go.microsoft.com/fwlink/?LinkID=322105. El archivo que falta es {0}. + + + + + + + \ No newline at end of file