cyberprophet / Algorithmic-Trading-Package

Building an MSIX package
MIT License
3 stars 4 forks source link

ml.net #9

Open cyberprophet opened 1 year ago

cyberprophet commented 1 year ago

// 1. Initalize ML.NET environment MLContext mlContext = new MLContext();

// 2. Load training data IDataView trainData = mlContext.Data.LoadFromTextFile("taxi-fare-train.csv", separatorChar:',');

// 3. Add data transformations var dataProcessPipeline = mlContext.Transforms.Categorical.OneHotEncoding( outputColumnName:"PaymentTypeEncoded", "PaymentType") .Append(mlContext.Transforms.Concatenate(outputColumnName:"Features", "PaymentTypeEncoded","PassengerCount","TripTime","TripDistance"));

// 4. Add algorithm var trainer = mlContext.Regression.Trainers.Sdca(labelColumnName: "FareAmount", featureColumnName: "Features");

var trainingPipeline = dataProcessPipeline.Append(trainer);

// 5. Train model var model = trainingPipeline.Fit(trainData);

// 6. Evaluate model on test data IDataView testData = mlContext.Data.LoadFromTextFile("taxi-fare-test.csv"); IDataView predictions = model.Transform(testData); var metrics = mlContext.Regression.Evaluate(predictions,"FareAmount");

// 7. Predict on sample data and print results var input = new ModelInput { PassengerCount = 1, TripTime = 1150, TripDistance = 4, PaymentType = "CRD" };

var result = mlContext.Model.CreatePredictionEngine<ModelInput,ModelOutput>(model).Predict(input);

Console.WriteLine($"Predicted fare: {result.FareAmount}\nModel Quality (RSquared): {metrics.RSquared}");

cyberchacha commented 1 year ago

using Microsoft.ML; using Microsoft.ML.AutoML;

class Program { static void Main(string[] args) { // 데이터를 불러옵니다. var context = new MLContext(); var data = context.Data.LoadFromTextFile("iris-data.txt", separatorChar: ',');

    // AutoML 구성을 설정합니다.
    var experimentSettings = new RegressionExperimentSettings
    {
        MaxExperimentTimeInSeconds = 60, // 최대 실행 시간
        OptimizingMetric = RegressionMetric.RSquared // 최적화할 메트릭
    };

    // AutoML 실험을 실행합니다.
    var experiment = context.Auto().CreateRegressionExperiment(experimentSettings);
    var result = experiment.Execute(data);

    // 최상의 모델을 가져옵니다.
    var bestModel = result.BestRun.Model;

    // 예측을 수행합니다.
    var predictionEngine = context.Model.CreatePredictionEngine<IrisData, IrisPrediction>(bestModel);
    var prediction = predictionEngine.Predict(new IrisData { SepalLength = 5.1f, SepalWidth = 3.5f, PetalLength = 1.4f, PetalWidth = 0.2f });

    // 결과 출력
    Console.WriteLine($"Predicted Label: {prediction.Prediction}");
}

}

// 데이터 클래스 class IrisData { [LoadColumn(0)] public float SepalLength;

[LoadColumn(1)]
public float SepalWidth;

[LoadColumn(2)]
public float PetalLength;

[LoadColumn(3)]
public float PetalWidth;

[LoadColumn(4)]
public string Label;

}

// 예측 클래스 class IrisPrediction { [ColumnName("Score")] public float Prediction; }

cyberchacha commented 1 year ago

https://learn.microsoft.com/ko-kr/dotnet/machine-learning/how-to-guides/how-to-use-the-automl-api