【问题标题】:C# Accent K-nearest neighbor function parameter problemsC# Accent K-最近邻函数参数问题
【发布时间】:2017-01-25 03:14:54
【问题描述】:

我正在尝试使用 Accord 库的 K-最近邻函数,它可以与任何类型一起使用。

我的目标是将它与位图数据一起使用,但即使复制示例代码并粘贴它,我也会收到此错误:

Error   4   The best overloaded method match for 'Accord.MachineLearning.KNearestNeighbors<System.Drawing.Bitmap>.KNearestNeighbors(int, int, System.Drawing.Bitmap[], int[], Accord.Math.Distances.IDistance<System.Drawing.Bitmap>)' has some invalid arguments   D:\...\WindowsFormsApplication1\WidgetControl.cs    295 49  Project_Ochare

这真的很奇怪,因为它与示例完全相同。据我所知,参数应该可以工作。 我添加了 Accord 的主要、扩展核心、数学、数学扩展、数学代码、机器学习和统计作为参考。

我尝试搜索,但找不到任何答案..

示例代码与此相同: http://accord-framework.net/docs/html/T_Accord_MachineLearning_KNearestNeighbors_1.htm

怎么了?

这是他们出错的示例代码:

private void __Test()
{
    // The k-Nearest Neighbors algorithm can be used with
    // any kind of data. In this example, we will see how
   // it can be used to compare, for example, Strings.

    string[] inputs = 
    {
        "Car",    // class 0
        "Bar",    // class 0
        "Jar",    // class 0
        "Charm",  // class 1
        "Chair"   // class 1
    };
    int[] outputs =
    {
       0, 0, 0,  // First three are from class 0
       1, 1,     // And next two are from class 1
    };

    // Now we will create the K-Nearest Neighbors algorithm. For this
    // example, we will be choosing k = 1. This means that, for a given
    // instance, only its nearest neighbor will be used to cast a new
    // decision. 

    // In order to compare strings, we will be using Levenshtein's string distance
    KNearestNeighbors<string> knn = new KNearestNeighbors<string>(k: 1, classes: 2, inputs: inputs, outputs: outputs, distance: Distance.Levenshtein);

    // After the algorithm has been created, we can use it:
    int answer = knn.Compute("Chars"); // answer should be 1.
}

这些是我收到的错误消息。我用 KNearest 的 Accord 示例代码创建了一个新的空项目。 它要我将 Distance.Levenshtein 更改为 Distance.Levenshtein(), 然后它告诉我它需要参数,无论我如何编写或添加什么,它都会给出相同的错误。

例如 Distance.Levenshtein("", ""), Distance.Levenshtein(0, 0) Distance.Levenshtein("","")、Distance.Levenshtein(new string1, new string1) 等等……任何我能想到的尝试。

距离。提供了大量的测量功能,它们都会导致相同的错误。

错误信息:

Error   1   The best overloaded method match for 'Accord.MachineLearning.KNearestNeighbors<string>.KNearestNeighbors(int, int, string[], int[], Accord.Math.Distances.IDistance<string>)' has some invalid arguments    D:\Dropbox\C#\KNearestTest\KNearestTest\Form1.cs    48  45  KNearestTest


Error   2   Argument 5: cannot convert from 'method group' to 'Accord.Math.Distances.IDistance<string>' D:\Dropbox\C#\KNearestTest\KNearestTest\Form1.cs    49  59  KNearestTest

【问题讨论】:

  • 添加代码,但对于错误,我怀疑您正在尝试将位图传递给函数,并且它需要一个项目数组,如果您想操作像素数据,则使用修复数据LockBitmap,将数据读入字节数组,然后对字节数组使用函数。
  • @Gusman 我不确定是否与此有关。即使有他们自己的例子,错误也会弹出:/
  • 这个例子的错误只是因为一个错字,而不是Distance.Levenshtein它应该是Distance.Levenshtein(),因为它是一个方法,而不是一个属性。
  • @Gusman 我试过了,Distance.Levenshtein("","") (因为它要求参数)。同样的红色下划线和消息说有问题:(
  • 如果你收到错误添加错误信息,我们没有水晶球来看看VS告诉你什么。

标签: c# .net accord.net


【解决方案1】:

文档似乎与库不对应。 我建议编写自己的IDistance&lt;string&gt; 接口实现,使用Levenschtein 距离:

public class DistanceStringsLevenstein : IDistance<string>
{
    public double Distance(string x, string y)
    {
        return Accord.Math.Distance.Levenshtein(x, y);
    }
}

位图的实现是:

public class DistanceBitmapLevenstein : IDistance<Bitmap>
{
    public double Distance(Bitmap x, Bitmap y)
    {
        return Accord.Math.Distance.Levenshtein(ImageToByte(x), ImageToByte(y));
    }

    public static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }
}

之后你可以在那个方法中使用这个类:

KNearestNeighbors<string> knn = new KNearestNeighbors<string>(k: 1, classes: 2,
        inputs: inputs, outputs: outputs, distance: new DistanceStringsLevenstein());

【讨论】:

  • 谢谢!!这适用于示例代码。对于位图,我应该如何实现这一点?我尝试编写 public class DistanceStringsLevenstein : IDistance { public double Distance(Bitmap[] x, Bitmap[] y) { return Accord.Math.Distance.Levenshtein(x, y);但它说这个方法没有实现接口成员 Accord.Math.Distances.IDistance.Distance(Bitmap, Bitmap)
  • @MadsM 我已经用位图实现更新了我的答案
  • 再次感谢您。通过实现这一点,Visual Studio 说“'Accord.Math.Distance.Levenshtein(string, string)' 的最佳重载方法匹配有一些无效参数。
  • 我不太清楚,我会像处理字节数组一样处理位图(请参阅更新的答案),但我不知道这是否是正确的方法。
  • 感谢您的帮助。该代码有效,但每次都返回相同的答案。尽管位图非常不同。我已经验证了输入数据。我怀疑距离测量有点奇怪:/
猜你喜欢
  • 2014-05-14
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
  • 2010-10-16
  • 2014-04-12
  • 2023-01-24
相关资源
最近更新 更多