看不懂这部分C#代码

Can't understand this part of C# code

这部分代码取自 this tutorial 关于在 Infer.NET 库中使用学习者的内容。我打算将它用于机器学习,但不幸的是我无法获得此代码片段。请帮帮我。

/// <summary>
/// A mapping for the Bayes Point Machine classifier tutorial.
/// </summary>
public class ClassifierMapping 
    : IClassifierMapping<IList<Vector>, int, IList<string>, string, Vector>
{
    public IEnumerable<int> GetInstances(IList<Vector> featureVectors)
    {
        for (int instance = 0; instance < featureVectors.Count; instance++)
        {
            yield return instance;
        }
    }

    public Vector GetFeatures(int instance, IList<Vector> featureVectors)
    {
        return featureVectors[instance];
    }

    public string GetLabel(
        int instance, IList<Vector> featureVectors, IList<string> labels)
    {
        return labels[instance];
    }

    public IEnumerable<string> GetClassLabels(
        IList<Vector> featureVectors = null, IList<string> labels = null)
    {
        return new[] { "Female", "Male" };
    }
}

提前致谢!

唯一有点复杂的是 yield return:

public IEnumerable<int> GetInstances(IList<Vector> featureVectors)
{
    for (int instance = 0; instance < featureVectors.Count; instance++)
    {
        yield return instance;
    }
}

可以替换为:

public IEnumerable<int> GetInstances(IList<Vector> featureVectors)
{
    var result = new List<int>();
    for (int instance = 0; instance < featureVectors.Count; instance++)
    {
       result.Add(instance);
    }
    return result;
}

这正在创建一个映射 class,它正在实现 IClassifierMapping 接口。

http://research.microsoft.com/en-us/um/cambridge/projects/infernet/docs/Infer.NET%20Learners%20-%20Bayes%20Point%20Machine%20classifiers%20-%20API%20-%20Mappings%20-%20Standard%20Data%20Format%20Mapping.aspx

还有其他的映射接口也可以在这里看到。

http://research.microsoft.com/en-us/um/cambridge/projects/infernet/docs/Infer.NET%20Learners%20-%20Bayes%20Point%20Machine%20classifiers%20-%20API%20-%20Mappings.aspx

使用此映射 class,您现在可以创建贝叶斯点机器 classifier。

关于代码的复杂程度,可以看westons的回答。