Añadido algo para cambiar el tipo del Label
This commit is contained in:
@@ -16,7 +16,7 @@ namespace testML
|
||||
{
|
||||
public static class DictionaryToObjectConverter
|
||||
{
|
||||
public static IEnumerable<object> Convert(List<Dictionary<string, object>> data, out Type classType, out DataViewSchema schema)
|
||||
public static IEnumerable<object> Convert(List<Dictionary<string, object>> data, string toPredict, out Type classType, out DataViewSchema schema)
|
||||
{
|
||||
var schemaBuilder = new DataViewSchema.Builder();
|
||||
|
||||
@@ -32,6 +32,12 @@ namespace testML
|
||||
if (sampleValue != null)
|
||||
{
|
||||
var keyType = sampleValue.GetType();
|
||||
|
||||
if (key == toPredict)
|
||||
{
|
||||
keyType = typeof(float);
|
||||
}
|
||||
|
||||
definition.Add(key, keyType);
|
||||
|
||||
if (keyType == typeof(string))
|
||||
@@ -61,6 +67,7 @@ namespace testML
|
||||
var converter = new DictionaryToObjectConverterClass()
|
||||
{
|
||||
ClassName = "OBJ" + Guid.NewGuid().ToString("N").ToUpper(),
|
||||
ToPredict = toPredict,
|
||||
Definition = definition
|
||||
};
|
||||
|
||||
@@ -100,14 +107,36 @@ namespace testML
|
||||
|
||||
var result =(IList) Activator.CreateInstance(genericType) ;
|
||||
|
||||
Dictionary<string, float> translate = new Dictionary<string, float>();
|
||||
translate.Add(string.Empty, 0);
|
||||
foreach (var inputData in data)
|
||||
{
|
||||
if (inputData.ContainsKey(toPredict) && inputData[toPredict] != null)
|
||||
{
|
||||
if (!translate.ContainsKey(inputData[toPredict] as string))
|
||||
{
|
||||
var max = translate.Values.Max()+1;
|
||||
translate.Add(inputData[toPredict] as string, max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach (var inputData in data)
|
||||
{
|
||||
var outputData = (IDictionaryToObjectConverter)Activator.CreateInstance(classType);
|
||||
result.Add(outputData);
|
||||
|
||||
foreach (var key in inputData.Keys)
|
||||
{
|
||||
outputData[key] = inputData[key];
|
||||
if (key == toPredict)
|
||||
{
|
||||
outputData[key] = translate[inputData[key] as string ?? string.Empty];
|
||||
}
|
||||
else
|
||||
{
|
||||
outputData[key] = inputData[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace testML
|
||||
|
||||
for (var r = 1; r < sheet.LastRowNum - 1; r++)
|
||||
{
|
||||
if (r == 30) break;
|
||||
if (r == 300) break;
|
||||
Console.WriteLine(string.Format("{0} / {1}", r, sheet.LastRowNum - 1));
|
||||
var row = sheet.GetRow(r);
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace testML
|
||||
|
||||
MLContext mlContext = new MLContext();
|
||||
|
||||
var dataConverted = DictionaryToObjectConverter.Convert(tmpData, out Type classType, out DataViewSchema schema);
|
||||
var dataConverted = DictionaryToObjectConverter.Convert(tmpData, "DESCENDIENTE_S4i001", out Type classType, out DataViewSchema schema);
|
||||
|
||||
var loadMethod = mlContext.Data.GetType().GetMethods().Where(x => x.Name == "LoadFromEnumerable" && x.IsGenericMethodDefinition).FirstOrDefault();
|
||||
|
||||
@@ -174,24 +174,24 @@ namespace testML
|
||||
experiment
|
||||
.SetPipeline(pipeline)
|
||||
.SetRegressionMetric(RegressionMetric.RSquared, labelColumn: columnInference.LabelColumnName)
|
||||
.SetTrainingTimeInSeconds(60)
|
||||
.SetTrainingTimeInSeconds(10)
|
||||
.SetDataset(trainData);
|
||||
|
||||
var result = experiment.Run();
|
||||
|
||||
#endregion
|
||||
|
||||
/*
|
||||
|
||||
//Entrenamos el modelo
|
||||
ITransformer model = pipe.Fit(trainData);
|
||||
//ITransformer model = pipe.Fit(trainData);
|
||||
|
||||
#region Hacemos un test para medir el % de error
|
||||
|
||||
// Use trained model to make inferences on test data
|
||||
IDataView testDataPredictions = model.Transform(testData);
|
||||
IDataView testDataPredictions = result.Model.Transform(testData);
|
||||
|
||||
// Extract model metrics and get RSquared
|
||||
RegressionMetrics trainedModelMetrics = mlContext.Regression.Evaluate(testDataPredictions);
|
||||
RegressionMetrics trainedModelMetrics = mlContext.Regression.Evaluate(testDataPredictions, labelColumnName: columnInference.LabelColumnName);
|
||||
double rSquared = trainedModelMetrics.RSquared;
|
||||
|
||||
Console.WriteLine("ModelMetrics: {0}", rSquared);
|
||||
@@ -201,7 +201,7 @@ namespace testML
|
||||
|
||||
#region Ponemos a prueba haciendo algunas predicciones
|
||||
|
||||
var predictionFunction = mlContext.Model.CreatePredictionEngine<Data, DataPrediction>(model);
|
||||
var predictionFunction = mlContext.Model.CreatePredictionEngine<Data, DataPrediction>(result.Model);
|
||||
|
||||
for (var c = 0; c < 25; c++)
|
||||
{
|
||||
@@ -215,7 +215,7 @@ namespace testML
|
||||
}
|
||||
|
||||
#endregion
|
||||
*/
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Press enter to Exit");
|
||||
Console.ReadLine();
|
||||
|
||||
Reference in New Issue
Block a user