C# Accent K-最近邻函数参数问题
C# Accent K-nearest neighbor function parameter problems
我正在尝试使用 Accord 库的 K-最近邻函数,它可以处理任何类型。
我的objective是将它与位图数据一起使用,但即使复制示例代码并粘贴它,我也会收到此错误:
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
文档似乎与图书馆不对应。
我建议编写您自己的 IDistance<string>
接口实现,它使用 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[]));
}
}
之后你可以在那个方法中使用这个 class:
KNearestNeighbors<string> knn = new KNearestNeighbors<string>(k: 1, classes: 2,
inputs: inputs, outputs: outputs, distance: new DistanceStringsLevenstein());
我正在尝试使用 Accord 库的 K-最近邻函数,它可以处理任何类型。
我的objective是将它与位图数据一起使用,但即使复制示例代码并粘贴它,我也会收到此错误:
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
文档似乎与图书馆不对应。
我建议编写您自己的 IDistance<string>
接口实现,它使用 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[]));
}
}
之后你可以在那个方法中使用这个 class:
KNearestNeighbors<string> knn = new KNearestNeighbors<string>(k: 1, classes: 2,
inputs: inputs, outputs: outputs, distance: new DistanceStringsLevenstein());