我正在开发一个dot net core 1.1 app,我正在尝试使用Accord.Net。根据此页面中的示例(Naive Bayes),我需要将从DB检索到的数据转换为DataTable。
问题是,在使用DataTable时出现了这个错误:
'Shim,...'和'中存在'DataTable'类型 'System.Data.Common,...'
即使我使用它:
DataTable learningDataNotCodifiedAsDataTable = new DataTable();
或者这个:
System.Data.DataTable learningDataNotCodifiedAsDataTable = new System.Data.DataTable();
TG
答案 0 :(得分:1)
如果你在程序集中有System.Data
程序集并且不想删除它或者不能删除它,那么你可以使用extern alias绕过它,但是当我使用它绕过这个错误时我得到{ {1}}错误,如果相信this discussion,原因是:
System.Data.DataTable作为空类出现在.Net core(1.0,1.1)中 完成接口实现。这个问题是跟踪的 需要引入API以在.Net中提供类似API的DataTable 芯
仅在.NET Core 2.0中更改,请参阅this SO post。我尝试了.NET Core 2.0项目中的代码(在VS 2017 15.3中),然后才运行正常。
<强>更新强> 我的意思是这个集会。
但正如你所说,你只有NUGET包,那么你也可以在'DataTable' does not contain a constructor that takes 0/1 arguments
文件中使用别名,如下面的Nuget包(我使用csproj
你可以在需要的时候用你的Shim包替换它):
System.Data.Common
然后用C#引用它:
<Target Name="DataAlias" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'System.Data.Common'">
<Aliases>MyData</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
但是你仍然无法使用,因为你会得到我上面写的构造函数的错误。在这里你有2个选项来解决这个问题:
extern alias MyData; //1st line in .cs file
...
using MyData::System.Data;
...
DataTable datatable = new DataTable();
解决方案from this post 答案 1 :(得分:1)
虽然DataTable在.NET Core 1.1中不可用,但现在可以在.NET Core 2.0中使用。如果您可以将项目升级到.NET Core 2.0,那么您将能够在代码中使用它。
但是,如果您现在无法切换到.NET Core 2.0,请注意您不需要将DataTable与Accord.NET框架中的任何方法一起使用。给出或显示它们只是因为它们可以提供一些额外的便利,但它们并不是真正需要的,如下例所示:
string[] columnNames = { "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis" };
string[][] data =
{
new string[] { "Sunny", "Hot", "High", "Weak", "No" },
new string[] { "Sunny", "Hot", "High", "Strong", "No" },
new string[] { "Overcast", "Hot", "High", "Weak", "Yes" },
new string[] { "Rain", "Mild", "High", "Weak", "Yes" },
new string[] { "Rain", "Cool", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Cool", "Normal", "Strong", "No" },
new string[] { "Overcast", "Cool", "Normal", "Strong", "Yes" },
new string[] { "Sunny", "Mild", "High", "Weak", "No" },
new string[] { "Sunny", "Cool", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Mild", "Normal", "Weak", "Yes" },
new string[] { "Sunny", "Mild", "Normal", "Strong", "Yes" },
new string[] { "Overcast", "Mild", "High", "Strong", "Yes" },
new string[] { "Overcast", "Hot", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Mild", "High", "Strong", "No" },
};
// Create a new codification codebook to
// convert strings into discrete symbols
Codification codebook = new Codification(columnNames, data);
// Extract input and output pairs to train
int[][] symbols = codebook.Transform(data);
int[][] inputs = symbols.Get(null, 0, -1); // Gets all rows, from 0 to the last (but not the last)
int[] outputs = symbols.GetColumn(-1); // Gets only the last column
// Create a new Naive Bayes learning
var learner = new NaiveBayesLearning();
NaiveBayes nb = learner.Learn(inputs, outputs);
// Consider we would like to know whether one should play tennis at a
// sunny, cool, humid and windy day. Let us first encode this instance
int[] instance = codebook.Translate("Sunny", "Cool", "High", "Strong");
// Let us obtain the numeric output that represents the answer
int c = nb.Decide(instance); // answer will be 0
// Now let us convert the numeric output to an actual "Yes" or "No" answer
string result = codebook.Translate("PlayTennis", c); // answer will be "No"
// We can also extract the probabilities for each possible answer
double[] probs = nb.Probabilities(instance); // { 0.795, 0.205 }