无法理解这部分C#代码

时间:2015-01-08 10:04:00

标签: c# machine-learning

这部分代码取自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" };
    }
}

提前谢谢!

2 个答案:

答案 0 :(得分:2)

唯一远程复杂的位是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;
}

答案 1 :(得分:0)

这是创建一个实现IClassifierMapping接口的Mapping类。

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

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

关于代码的复杂性,你可以看看westons的回答。