ML.NET C#发现哪些指标最适合预测趋势

huangapple go评论42阅读模式
英文:

ML.NET C# finds out which indicators are best to predict a trend

问题

我有一个文件,其中包含股票价格和许多指标的数值(例如MACD、RSI、EMA等等)。我想使用ML.NET来找出哪些指标对于识别趋势(上涨或下跌趋势)最为重要。

有人可以帮助我吗?

英文:

I have a file that has a stock price and many indicators' values (for example MACD, RSI, EMA, and so on). I want to use ML.NET to find out which indicators are most important to identify a trend (up/down trend).

Anyone can help me?

答案1

得分: 1

以下是翻译的内容:

// 定义你的数据类
public class StockData
{
    [LoadColumn(0)]
    public float Price { get; set; }
    [LoadColumn(1)]
    public float MACD { get; set; }
    [LoadColumn(2)]
    public float RSI { get; set; }
    [LoadColumn(3)]
    public float EMA { get; set; }
    // 根据需要添加其他指标
    [LoadColumn(4)]
    public bool Trend { get; set; }
}

// 载入你的数据
var mlContext = new MLContext();
var data = mlContext.Data.LoadFromTextFile<StockData>("path/to/your/data.csv", separatorChar: ',');

// 将数据分成训练集和测试集
var trainTestSplit = mlContext.Data.TrainTestSplit(data);

// 定义特征选择管道
var pipeline = mlContext.Transforms.Concatenate("Features", "Price", "MACD", "RSI", "EMA")
    .Append(mlContext.Transforms.Conversion.MapValueToKey("Label", "Trend"))
    .Append(mlContext.Transforms.FeatureSelection.SelectBestBinaryFeatures("Features", "Label", numberOfFeatures: 2))
    .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

// 训练你的模型
var model = pipeline.Fit(trainTestSplit.TrainSet);

// 在测试集上评估你的模型
var metrics = mlContext.BinaryClassification.Evaluate(model.Transform(trainTestSplit.TestSet));

// 打印特征权重,以查看哪些指标最重要
var featureWeights = model.LastTransformer.Model.GetFeatureWeights();
Console.WriteLine("Feature weights:");
foreach (var featureWeight in featureWeights)
{
    Console.WriteLine($"{featureWeight.FeatureName}: {featureWeight.Weight}");
}

希望这个示例能为你提供一个良好的起点。

英文:

I'm going to assume your data is organized in the following csv format:

Price,MACD,RSI,EMA,Trend
100,1.23,70.5,98.5,Up
105,1.45,75.2,101.2,Up
95,1.01,60.3,94.2,Down
110,1.67,80.1,104.5,Up
92,0.85,55.2,90.5,Down

Furthermore, I will assume your Trend Labels are pre-determined through some simple heuristic such as an EMA 200 crossover, and that a human has optionally manually validated these labels.

With this established, you can use feature selection techniques in ML.NET to help you identify the most important features in your data that impact your target variable (in this case, whether the trend is up or down).

The following is an example of how you would apply feature selection in C# using ML.NET:

using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms;

// Define your data classes
public class StockData
{
    [LoadColumn(0)]
    public float Price { get; set; }
    [LoadColumn(1)]
    public float MACD { get; set; }
    [LoadColumn(2)]
    public float RSI { get; set; }
    [LoadColumn(3)]
    public float EMA { get; set; }
    // Add any other indicators as needed
    [LoadColumn(4)]
    public bool Trend { get; set; }
}

// Load your data
var mlContext = new MLContext();
var data = mlContext.Data.LoadFromTextFile&lt;StockData&gt;(&quot;path/to/your/data.csv&quot;, separatorChar: &#39;,&#39;);

// Split your data into training and testing sets
var trainTestSplit = mlContext.Data.TrainTestSplit(data);

// Define your feature selection pipeline
var pipeline = mlContext.Transforms.Concatenate(&quot;Features&quot;, &quot;Price&quot;, &quot;MACD&quot;, &quot;RSI&quot;, &quot;EMA&quot;)
    .Append(mlContext.Transforms.Conversion.MapValueToKey(&quot;Label&quot;, &quot;Trend&quot;))
    .Append(mlContext.Transforms.FeatureSelection.SelectBestBinaryFeatures(&quot;Features&quot;, &quot;Label&quot;, numberOfFeatures: 2))
    .Append(mlContext.Transforms.Conversion.MapKeyToValue(&quot;PredictedLabel&quot;));

// Train your model
var model = pipeline.Fit(trainTestSplit.TrainSet);

// Evaluate your model on the test set
var metrics = mlContext.BinaryClassification.Evaluate(model.Transform(trainTestSplit.TestSet));

// Print the feature weights to see which indicators are most important
var featureWeights = model.LastTransformer.Model.GetFeatureWeights();
Console.WriteLine(&quot;Feature weights:&quot;);
foreach (var featureWeight in featureWeights)
{
    Console.WriteLine($&quot;{featureWeight.FeatureName}: {featureWeight.Weight}&quot;);
}

This example uses the Concatenate transform to combine all of our feature series into a single Features column. The SelectBestBinaryFeatures transform is then used to select the two most important features (alternatively, you can also just adjust the numberOfFeatures parameter manually as needed).

Hopefully, this helps give you a good starting point.

huangapple
  • 本文由 发表于 2023年2月9日 03:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定