Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext();

            ClassGenerator classGenerator = new ClassGenerator("GeneratedIris", "CustomClass");

            classGenerator.AddField("SepalLength", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("SepalWidth", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("PetalLength", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("PetalWidth", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("Label", typeof(string), System.CodeDom.MemberAttributes.Public);
            classGenerator.Compile();

            List <object> generatedDataSet = new List <object>();

            dataset.ToList().ForEach((d) =>
            {
                generatedDataSet.Add(GetDynamicClass(d, classGenerator.GetInstance()));
            });

            var instance = classGenerator.GetInstance().GetType();
            DataViewGenerator listGenerator = new DataViewGenerator("ListIris", "CustomGenerator", instance, classGenerator.NamespaceName);
            var type       = listGenerator.GeneratorType;
            var methodInfo = type.GetMethod("GetDataView");
            var dataView   = methodInfo.Invoke(null, new object[] { generatedDataSet.ToList() });

            IDataView trainingDataView = (IDataView)dataView;

            trainingDataView.Schema.ToList().Add(new DataViewSchema.Column());

            var pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
                           .Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
                           .AppendCacheCheckpoint(mlContext)
                           .Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumnName: "Label", featureColumnName: "Features"))
                           .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));


            TransformerChain <Microsoft.ML.Transforms.KeyToValueMappingTransformer> model = pipeline.Fit(trainingDataView);

            var prediction = model.CreatePredictionEngine <IrisData, IrisPrediction>(mlContext).Predict(
                new IrisData()
            {
                SepalLength = 5.9f,
                SepalWidth  = 3.0f,
                PetalLength = 5.1f,
                PetalWidth  = 1.8f,
            });

            Console.WriteLine(prediction.PredictedLabels);

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public static ClassGenerator GenerateLabelClass(string className, string classNamespace)
        {
            Dictionary <string, string> attributes = new Dictionary <string, string>();

            attributes.Add("ColumnNameAttribute", "PredictedLabel");
            ClassGenerator labelClassGenerator = new ClassGenerator(className, classNamespace);

            labelClassGenerator.AddField("PredictedLabels", typeof(string), System.CodeDom.MemberAttributes.Public, attributes);
            labelClassGenerator.Compile();

            return(labelClassGenerator);
        }
Ejemplo n.º 3
0
        public static ClassGenerator GenerateDataSetClass(IEnumerable <MLFeature> features, string className, string classNamespace)
        {
            ClassGenerator classGenerator = new ClassGenerator(className, classNamespace);

            foreach (var item in features)
            {
                classGenerator.AddField(item.Name, item.Type, System.CodeDom.MemberAttributes.Public);
            }
            classGenerator.Compile();

            return(classGenerator);
        }
Ejemplo n.º 4
0
        public static ClassGenerator GenerateDataSetClass(Type classType, string className, string classNamespace)
        {
            if (classType.GetTypeInfo().IsClass)
            {
                ClassGenerator classGenerator = new ClassGenerator(className, classNamespace);

                foreach (var item in classType.GetFields())
                {
                    classGenerator.AddField(item.Name, item.FieldType, System.CodeDom.MemberAttributes.Public);
                }
                classGenerator.Compile();

                return(classGenerator);
            }
            else
            {
                throw new Exception("Type not a class");
            }
        }