本文整理汇总了C#中Codification.Apply方法的典型用法代码示例。如果您正苦于以下问题:C# Codification.Apply方法的具体用法?C# Codification.Apply怎么用?C# Codification.Apply使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Codification
的用法示例。
在下文中一共展示了Codification.Apply方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ApplyTest1
public void ApplyTest1()
{
DataTable table = ProjectionFilterTest.CreateTable();
// Show the start data
//Accord.Controls.DataGridBox.Show(table);
// Create a new data projection (column) filter
var filter = new Codification(table, "Category");
// Apply the filter and get the result
DataTable result = filter.Apply(table);
// Show it
//Accord.Controls.DataGridBox.Show(result);
Assert.AreEqual(5, result.Columns.Count);
Assert.AreEqual(5, result.Rows.Count);
Assert.AreEqual(0, result.Rows[0]["Category"]);
Assert.AreEqual(1, result.Rows[1]["Category"]);
Assert.AreEqual(1, result.Rows[2]["Category"]);
Assert.AreEqual(0, result.Rows[3]["Category"]);
Assert.AreEqual(2, result.Rows[4]["Category"]);
}
示例2: CreateMitchellExample
public static void CreateMitchellExample(out DecisionTree tree, out double[][] inputs, out int[] outputs)
{
DataTable data = new DataTable("Mitchell's Tennis Example");
data.Columns.Add("Day", typeof(string));
data.Columns.Add("Outlook", typeof(string));
data.Columns.Add("Temperature", typeof(double));
data.Columns.Add("Humidity", typeof(double));
data.Columns.Add("Wind", typeof(string));
data.Columns.Add("PlayTennis", typeof(string));
data.Rows.Add("D1", "Sunny", 85, 85, "Weak", "No");
data.Rows.Add("D2", "Sunny", 80, 90, "Strong", "No");
data.Rows.Add("D3", "Overcast", 83, 78, "Weak", "Yes");
data.Rows.Add("D4", "Rain", 70, 96, "Weak", "Yes");
data.Rows.Add("D5", "Rain", 68, 80, "Weak", "Yes");
data.Rows.Add("D6", "Rain", 65, 70, "Strong", "No");
data.Rows.Add("D7", "Overcast", 64, 65, "Strong", "Yes");
data.Rows.Add("D8", "Sunny", 72, 95, "Weak", "No");
data.Rows.Add("D9", "Sunny", 69, 70, "Weak", "Yes");
data.Rows.Add("D10", "Rain", 75, 80, "Weak", "Yes");
data.Rows.Add("D11", "Sunny", 75, 70, "Strong", "Yes");
data.Rows.Add("D12", "Overcast", 72, 90, "Strong", "Yes");
data.Rows.Add("D13", "Overcast", 81, 75, "Weak", "Yes");
data.Rows.Add("D14", "Rain", 71, 80, "Strong", "No");
// Create a new codification codebook to
// convert strings into integer symbols
Codification codebook = new Codification(data);
DecisionVariable[] attributes =
{
new DecisionVariable("Outlook", codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain)
new DecisionVariable("Temperature", DecisionVariableKind.Continuous), // continuous values
new DecisionVariable("Humidity", DecisionVariableKind.Continuous), // continuous values
new DecisionVariable("Wind", codebook["Wind"].Symbols) // 2 possible values (Weak, strong)
};
int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no)
tree = new DecisionTree(attributes, classCount);
C45Learning c45 = new C45Learning(tree);
// Extract symbols from data and train the classifier
DataTable symbols = codebook.Apply(data);
inputs = symbols.ToArray("Outlook", "Temperature", "Humidity", "Wind");
outputs = symbols.ToArray<int>("PlayTennis");
double error = c45.Run(inputs, outputs);
}
示例3: ConstantDiscreteVariableTest
public void ConstantDiscreteVariableTest()
{
DecisionTree tree;
double[][] inputs;
int[] outputs;
DataTable data = new DataTable("Degenerated Tennis Example");
data.Columns.Add("Day", typeof(string));
data.Columns.Add("Outlook", typeof(string));
data.Columns.Add("Temperature", typeof(double));
data.Columns.Add("Humidity", typeof(double));
data.Columns.Add("Wind", typeof(string));
data.Columns.Add("PlayTennis", typeof(string));
data.Rows.Add("D1", "Sunny", 50, 85, "Weak", "No");
data.Rows.Add("D2", "Sunny", 50, 90, "Weak", "No");
data.Rows.Add("D3", "Overcast", 83, 78, "Weak", "Yes");
data.Rows.Add("D4", "Rain", 70, 96, "Weak", "Yes");
data.Rows.Add("D5", "Rain", 68, 80, "Weak", "Yes");
data.Rows.Add("D6", "Rain", 65, 70, "Weak", "No");
data.Rows.Add("D7", "Overcast", 64, 65, "Weak", "Yes");
data.Rows.Add("D8", "Sunny", 50, 95, "Weak", "No");
data.Rows.Add("D9", "Sunny", 69, 70, "Weak", "Yes");
data.Rows.Add("D10", "Rain", 75, 80, "Weak", "Yes");
data.Rows.Add("D11", "Sunny", 75, 70, "Weak", "Yes");
data.Rows.Add("D12", "Overcast", 72, 90, "Weak", "Yes");
data.Rows.Add("D13", "Overcast", 81, 75, "Weak", "Yes");
data.Rows.Add("D14", "Rain", 50, 80, "Weak", "No");
// Create a new codification codebook to
// convert strings into integer symbols
Codification codebook = new Codification(data);
DecisionVariable[] attributes =
{
new DecisionVariable("Outlook", codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain)
new DecisionVariable("Temperature", DecisionVariableKind.Continuous), // continuous values
new DecisionVariable("Humidity", DecisionVariableKind.Continuous), // continuous values
new DecisionVariable("Wind", codebook["Wind"].Symbols + 1) // 1 possible value (Weak)
};
int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no)
tree = new DecisionTree(attributes, classCount);
C45Learning c45 = new C45Learning(tree);
// Extract symbols from data and train the classifier
DataTable symbols = codebook.Apply(data);
inputs = symbols.ToArray("Outlook", "Temperature", "Humidity", "Wind");
outputs = symbols.ToArray<int>("PlayTennis");
double error = c45.Run(inputs, outputs);
for (int i = 0; i < inputs.Length; i++)
{
int y = tree.Compute(inputs[i]);
Assert.AreEqual(outputs[i], y);
}
}
示例4: Run
public void Run()
{
DataTable data = new DataTable("Mitchell's Tennis Example");
data.Columns.Add("Day");
data.Columns.Add("Outlook");
data.Columns.Add("Temperature");
data.Columns.Add("Humidity");
data.Columns.Add("Wind");
data.Columns.Add("PlayTennis");
data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes");
data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No");
data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes");
data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes");
data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes");
data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes");
data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");
// Create a new codification codebook to
// convert strings into integer symbols
Codification codebook = new Codification(data, "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
// Translate our training data into integer symbols using our codebook:
DataTable symbols = codebook.Apply(data);
CreateDic("Outlook", symbols);
CreateDic("Temperature", symbols);
CreateDic("Humidity", symbols);
CreateDic("Wind", symbols);
CreateDic("PlayTennis", symbols);
int[][] inputs = (from p in symbols.AsEnumerable()
select new int[]
{
GetIndex("Outlook", p["Outlook"].ToString()),
GetIndex("Temperature", p["Temperature"].ToString()),
GetIndex("Humidity", p["Humidity"].ToString()),
GetIndex("Wind", p["Wind"].ToString())
}).Cast<int[]>().ToArray();
int[] outputs = (from p in symbols.AsEnumerable()
select GetIndex("PlayTennis", p["PlayTennis"].ToString())).Cast<int>().ToArray();
/*
// Gather information about decision variables
DecisionVariable[] attributes =
{
new DecisionVariable("Outlook", 3), // 3 possible values (Sunny, overcast, rain)
new DecisionVariable("Temperature", 3), // 3 possible values (Hot, mild, cool)
new DecisionVariable("Humidity", 2), // 2 possible values (High, normal)
new DecisionVariable("Wind", 2) // 2 possible values (Weak, strong)
};
*/
DecisionVariable[] attributes =
{
new DecisionVariable("Outlook", GetCount("Outlook")), // 3 possible values (Sunny, overcast, rain)
new DecisionVariable("Temperature", GetCount("Temperature")), // 3 possible values (Hot, mild, cool)
new DecisionVariable("Humidity", GetCount("Humidity")), // 2 possible values (High, normal)
new DecisionVariable("Wind", GetCount("Wind")) // 2 possible values (Weak, strong)
};
int classCount = GetCount("PlayTennis"); // 2 possible output values for playing tennis: yes or no
//Create the decision tree using the attributes and classes
DecisionTree tree = new DecisionTree(attributes, classCount);
// Create a new instance of the ID3 algorithm
ID3Learning id3learning = new ID3Learning(tree);
// Learn the training instances!
id3learning.Run(inputs, outputs);
string answer = codebook.Translate("PlayTennis",
tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong")));
Console.WriteLine("Calculate for: Sunny, Hot, High, Strong");
Console.WriteLine("Answer: " + answer);
var expression = tree.ToExpression();
Console.WriteLine(tree.ToCode("ClassTest"));
DecisionSet s = tree.ToRules();
Console.WriteLine(s.ToString());
// Compiles the expression to IL
var func = expression.Compile();
}
示例5: ApplyTest
public void ApplyTest()
{
Codification target = new Codification();
DataTable input = new DataTable("Sample data");
input.Columns.Add("Age", typeof(int));
input.Columns.Add("Classification", typeof(string));
input.Rows.Add(10, "child");
input.Rows.Add(7, "child");
input.Rows.Add(4, "child");
input.Rows.Add(21, "adult");
input.Rows.Add(27, "adult");
input.Rows.Add(12, "child");
input.Rows.Add(79, "elder");
input.Rows.Add(40, "adult");
input.Rows.Add(30, "adult");
DataTable expected = new DataTable("Sample data");
expected.Columns.Add("Age", typeof(int));
expected.Columns.Add("Classification", typeof(int));
expected.Rows.Add(10, 0);
expected.Rows.Add(7, 0);
expected.Rows.Add(4, 0);
expected.Rows.Add(21, 1);
expected.Rows.Add(27, 1);
expected.Rows.Add(12, 0);
expected.Rows.Add(79, 2);
expected.Rows.Add(40, 1);
expected.Rows.Add(30, 1);
// Detect the mappings
target.Detect(input);
// Apply the categorization
DataTable actual = target.Apply(input);
for (int i = 0; i < actual.Rows.Count; i++)
for (int j = 0; j < actual.Columns.Count; j++)
Assert.AreEqual(expected.Rows[i][j], actual.Rows[i][j]);
}
示例6: ApplyTest4
public void ApplyTest4()
{
string path = @"Resources\intrusion.xls";
ExcelReader db = new ExcelReader(path, false, true);
DataTable table = db.GetWorksheet("test");
Codification codebook = new Codification(table);
DataTable result = codebook.Apply(table);
Assert.IsNotNull(result);
foreach (DataColumn col in result.Columns)
Assert.AreNotEqual(col.DataType, typeof(string));
Assert.IsTrue(result.Rows.Count > 0);
}
示例7: CreateMitchellExample
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion
public static void CreateMitchellExample(out DecisionTree tree, out int[][] inputs, out int[] outputs)
{
DataTable data = new DataTable("Mitchell's Tennis Example");
data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes");
data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No");
data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes");
data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes");
data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes");
data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes");
data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");
// Create a new codification codebook to
// convert strings into integer symbols
Codification codebook = new Codification(data);
DecisionVariable[] attributes =
{
new DecisionVariable("Outlook", codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain)
new DecisionVariable("Temperature", codebook["Temperature"].Symbols), // 3 possible values (Hot, mild, cool)
new DecisionVariable("Humidity", codebook["Humidity"].Symbols), // 2 possible values (High, normal)
new DecisionVariable("Wind", codebook["Wind"].Symbols) // 2 possible values (Weak, strong)
};
int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no)
tree = new DecisionTree(attributes, classCount);
ID3Learning id3 = new ID3Learning(tree);
// Extract symbols from data and train the classifier
DataTable symbols = codebook.Apply(data);
inputs = symbols.ToArray<int>("Outlook", "Temperature", "Humidity", "Wind");
outputs = symbols.ToArray<int>("PlayTennis");
double error = id3.Run(inputs, outputs);
Assert.AreEqual(0, error);
foreach (DataRow row in data.Rows)
{
var x = codebook.Translate(row, "Outlook", "Temperature", "Humidity", "Wind");
int y = tree.Compute(x);
string actual = codebook.Translate("PlayTennis", y);
string expected = row["PlayTennis"] as string;
Assert.AreEqual(expected, actual);
}
{
string answer = codebook.Translate("PlayTennis",
tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong")));
Assert.AreEqual("No", answer);
}
}
示例8: ConstantDiscreteVariableTest
public void ConstantDiscreteVariableTest()
{
DecisionTree tree;
int[][] inputs;
int[] outputs;
DataTable data = new DataTable("Degenerated Tennis Example");
data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
data.Rows.Add("D4", "Rain", "Hot", "High", "Weak", "Yes");
data.Rows.Add("D5", "Rain", "Hot", "Normal", "Weak", "Yes");
data.Rows.Add("D6", "Rain", "Hot", "Normal", "Strong", "No");
data.Rows.Add("D7", "Overcast", "Hot", "Normal", "Strong", "Yes");
data.Rows.Add("D8", "Sunny", "Hot", "High", "Weak", "No");
data.Rows.Add("D9", "Sunny", "Hot", "Normal", "Weak", "Yes");
data.Rows.Add("D10", "Rain", "Hot", "Normal", "Weak", "Yes");
data.Rows.Add("D11", "Sunny", "Hot", "Normal", "Strong", "Yes");
data.Rows.Add("D12", "Overcast", "Hot", "High", "Strong", "Yes");
data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
data.Rows.Add("D14", "Rain", "Hot", "High", "Strong", "No");
// Create a new codification codebook to
// convert strings into integer symbols
Codification codebook = new Codification(data);
DecisionVariable[] attributes =
{
new DecisionVariable("Outlook", codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain)
new DecisionVariable("Temperature", codebook["Temperature"].Symbols), // 1 constant value (Hot)
new DecisionVariable("Humidity", codebook["Humidity"].Symbols), // 2 possible values (High, normal)
new DecisionVariable("Wind", codebook["Wind"].Symbols) // 2 possible values (Weak, strong)
};
int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no)
bool thrown = false;
try
{
tree = new DecisionTree(attributes, classCount);
}
catch
{
thrown = true;
}
Assert.IsTrue(thrown);
attributes[1] = new DecisionVariable("Temperature", 2);
tree = new DecisionTree(attributes, classCount);
ID3Learning id3 = new ID3Learning(tree);
// Extract symbols from data and train the classifier
DataTable symbols = codebook.Apply(data);
inputs = symbols.ToArray<int>("Outlook", "Temperature", "Humidity", "Wind");
outputs = symbols.ToArray<int>("PlayTennis");
double error = id3.Run(inputs, outputs);
for (int i = 0; i < inputs.Length; i++)
{
int y = tree.Compute(inputs[i]);
Assert.AreEqual(outputs[i], y);
}
}
示例9: ComputeTest2
public void ComputeTest2()
{
DataTable data = new DataTable("Mitchell's Tennis Example");
data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
// We will set Temperature and Humidity to be continuous
data.Columns["Temperature"].DataType = typeof(double);
data.Columns["Humidity"].DataType = typeof(double);
data.Rows.Add("D1", "Sunny", 38.0, 96.0, "Weak", "No");
data.Rows.Add("D2", "Sunny", 39.0, 90.0, "Strong", "No");
data.Rows.Add("D3", "Overcast", 38.0, 75.0, "Weak", "Yes");
data.Rows.Add("D4", "Rain", 25.0, 87.0, "Weak", "Yes");
data.Rows.Add("D5", "Rain", 12.0, 30.0, "Weak", "Yes");
data.Rows.Add("D6", "Rain", 11.0, 35.0, "Strong", "No");
data.Rows.Add("D7", "Overcast", 10.0, 40.0, "Strong", "Yes");
data.Rows.Add("D8", "Sunny", 24.0, 90.0, "Weak", "No");
data.Rows.Add("D9", "Sunny", 12.0, 26.0, "Weak", "Yes");
data.Rows.Add("D10", "Rain", 25, 30.0, "Weak", "Yes");
data.Rows.Add("D11", "Sunny", 26.0, 40.0, "Strong", "Yes");
data.Rows.Add("D12", "Overcast", 27.0, 97.0, "Strong", "Yes");
data.Rows.Add("D13", "Overcast", 39.0, 41.0, "Weak", "Yes");
data.Rows.Add("D14", "Rain", 23.0, 98.0, "Strong", "No");
// Create a new codification codebook to
// convert strings into discrete symbols
Codification codebook = new Codification(data);
int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no)
int inputCount = 4; // 4 variables (Outlook, Temperature, Humidity, Wind)
IUnivariateDistribution[] priors =
{
new GeneralDiscreteDistribution(codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain)
new NormalDistribution(), // Continuous value (celsius)
new NormalDistribution(), // Continuous value (percentage)
new GeneralDiscreteDistribution(codebook["Wind"].Symbols) // 2 possible values (Weak, strong)
};
// Create a new Naive Bayes classifiers for the two classes
var target = new NaiveBayes<IUnivariateDistribution>(classCount, inputCount, priors);
// Extract symbols from data and train the classifier
DataTable symbols = codebook.Apply(data);
double[][] inputs = symbols.ToArray("Outlook", "Temperature", "Humidity", "Wind");
int[] outputs = symbols.ToArray<int>("PlayTennis");
// Compute the Naive Bayes model
target.Estimate(inputs, outputs);
double logLikelihood;
double[] responses;
// Compute the result for a sunny, cool, humid and windy day:
double[] instance = new double[]
{
codebook.Translate(columnName:"Outlook", value:"Sunny"),
12.0,
90.0,
codebook.Translate(columnName:"Wind", value:"Strong")
};
int c = target.Compute(instance, out logLikelihood, out responses);
string result = codebook.Translate("PlayTennis", c);
Assert.AreEqual("No", result);
Assert.AreEqual(0, c);
Assert.AreEqual(0.840, responses[0], 1e-3);
Assert.AreEqual(1, responses.Sum(), 1e-10);
Assert.IsFalse(double.IsNaN(responses[0]));
Assert.AreEqual(2, responses.Length);
}
示例10: bayes
private string bayes(DataTable tbl)
{
Codification codebook = new Codification(tbl,
"Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses", "Class");
// Translate our training data into integer symbols using our codebook:
DataTable symbols = codebook.Apply(tbl);
int[][] inputs = symbols.ToIntArray("Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses");
int[] outputs = symbols.ToIntArray("Class").GetColumn(0);
// Gather information about decision variables
int[] symbolCounts =
{
codebook["Clump Thickness"].Symbols, // 3 possible values (Sunny, overcast, rain)
codebook["Uniformity of Cell Size"].Symbols, // 3 possible values (Hot, mild, cool)
codebook["Uniformity of Cell Shape"].Symbols, // 2 possible values (High, normal)
codebook["Marginal Adhesion"].Symbols , // 2 possible values (Weak, strong)
codebook["Single Epithelial Cell Size"].Symbols ,
codebook["Bare Nuclei"].Symbols ,
codebook["Bland Chromatin"].Symbols ,
codebook["Normal Nucleoli"].Symbols ,
codebook["Mitoses"].Symbols
};
int classCount = codebook["Class"].Symbols; // 2 possible values (yes, no)
// Create a new Naive Bayes classifiers for the two classes
NaiveBayes target = new NaiveBayes(classCount, symbolCounts);
// Compute the Naive Bayes model
target.Estimate(inputs, outputs);
// We will be computing the label for a sunny, cool, humid and windy day:
int[] instance = codebook.Translate(inputlar[0], inputlar[1], inputlar[2], inputlar[3],
inputlar[4], inputlar[5], inputlar[6], inputlar[7], inputlar[8]);
// Now, we can feed this instance to our model
int output = target.Compute(instance);
// Finally, the result can be translated back to one of the codewords using
string result = codebook.Translate("Class", output); // result is "No"
return result;
}
示例11: knn
public string knn(DataTable tbl)
{
Codification codebook = new Codification(tbl);
DataTable symbols = codebook.Apply(tbl);
double[][] inputs = symbols.ToIntArray("Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses").ToDouble();
int sayac = 0;
int[] outputs = symbols.ToIntArray("Class").GetColumn(0);
KNearestNeighbors knn = new KNearestNeighbors(k: 4, classes: 2,
inputs: inputs, outputs: outputs);
int answer = knn.Compute(new double[] { Convert.ToInt32(inputlar[0]), Convert.ToInt32(inputlar[1]),
Convert.ToInt32( inputlar[2]), Convert.ToInt32( inputlar[3]), Convert.ToInt32( inputlar[4]),
Convert.ToInt32( inputlar[5]), Convert.ToInt32( inputlar[6]), Convert.ToInt32( inputlar[7]), Convert.ToInt32( inputlar[8]) }); // answer will be 2.
if (answer == 0)
answer = 4;
else
answer = 2;
return answer.ToString();
}
示例12: kmeans
public string kmeans(DataTable tbl)
{
Codification codebook = new Codification(tbl);
DataTable symbols = codebook.Apply(tbl);
double[][] inputs = symbols.ToIntArray("Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses").ToDouble();
int sayac = 0;
int[] outputs = symbols.ToIntArray("Class").GetColumn(0);
// Declare some observations
//double[][] observations =
// {
// new double[] { -5, -2, -1 },
// new double[] { -5, -5, -6 },
// new double[] { 2, 1, 1 },
// new double[] { 1, 1, 2 },
// new double[] { 1, 2, 2 },
// new double[] { 3, 1, 2 },
// new double[] { 11, 5, 4 },
// new double[] { 15, 5, 6 },
// new double[] { 10, 5, 6 },
// };
KMeans kmeans = new KMeans(2);
int[] labels = kmeans.Compute(inputs);
int c = kmeans.Clusters.Nearest(new double[] { Convert.ToInt32(inputlar[0]), Convert.ToInt32(inputlar[1]),
Convert.ToInt32( inputlar[2]), Convert.ToInt32( inputlar[3]), Convert.ToInt32( inputlar[4]),
Convert.ToInt32( inputlar[5]), Convert.ToInt32( inputlar[6]), Convert.ToInt32( inputlar[7]), Convert.ToInt32( inputlar[8]) });
return c.ToString(); ;
}
示例13: learn_test_mitchell
public void learn_test_mitchell()
{
#region doc_mitchell_1
// We will represent Mitchell's Tennis example using a DataTable. However,
// the use of a DataTable is not required in order to use the Naive Bayes.
// Please take a look at the other examples below for simpler approaches.
DataTable data = new DataTable("Mitchell's Tennis Example");
data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
// We will set Temperature and Humidity to be continuous
data.Columns["Temperature"].DataType = typeof(double);
data.Columns["Humidity"].DataType = typeof(double);
// Add some data
data.Rows.Add("D1", "Sunny", 38.0, 96.0, "Weak", "No");
data.Rows.Add("D2", "Sunny", 39.0, 90.0, "Strong", "No");
data.Rows.Add("D3", "Overcast", 38.0, 75.0, "Weak", "Yes");
data.Rows.Add("D4", "Rain", 25.0, 87.0, "Weak", "Yes");
data.Rows.Add("D5", "Rain", 12.0, 30.0, "Weak", "Yes");
data.Rows.Add("D6", "Rain", 11.0, 35.0, "Strong", "No");
data.Rows.Add("D7", "Overcast", 10.0, 40.0, "Strong", "Yes");
data.Rows.Add("D8", "Sunny", 24.0, 90.0, "Weak", "No");
data.Rows.Add("D9", "Sunny", 12.0, 26.0, "Weak", "Yes");
data.Rows.Add("D10", "Rain", 25, 30.0, "Weak", "Yes");
data.Rows.Add("D11", "Sunny", 26.0, 40.0, "Strong", "Yes");
data.Rows.Add("D12", "Overcast", 27.0, 97.0, "Strong", "Yes");
data.Rows.Add("D13", "Overcast", 39.0, 41.0, "Weak", "Yes");
data.Rows.Add("D14", "Rain", 23.0, 98.0, "Strong", "No");
#endregion
#region doc_mitchell_2
// Create a new codification codebook to
// convert strings into discrete symbols
Codification codebook = new Codification(data);
#endregion
#region doc_mitchell_3
// Some distributions require constructor parameters, and as such, cannot
// be automatically initialized by the learning algorithm. For this reason,
// we might need to specify how each component should be initialized:
IUnivariateFittableDistribution[] priors =
{
new GeneralDiscreteDistribution(codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain)
new NormalDistribution(), // Continuous value (Celsius)
new NormalDistribution(), // Continuous value (percentage)
new GeneralDiscreteDistribution(codebook["Wind"].Symbols) // 2 possible values (Weak, strong)
};
// Create a new Naive Bayes classifiers for the two classes
var learner = new NaiveBayesLearning<IUnivariateFittableDistribution>()
{
// Tell the learner how to initialize the distributions
Distribution = (classIndex, variableIndex) => priors[variableIndex]
};
// Extract symbols from data and train the classifier
DataTable symbols = codebook.Apply(data);
double[][] inputs = symbols.ToArray("Outlook", "Temperature", "Humidity", "Wind");
int[] outputs = symbols.ToArray<int>("PlayTennis");
// Learn the Naive Bayes model
var naiveBayes = learner.Learn(inputs, outputs);
#endregion
#region doc_mitchell_4
// Create an instance representing a "sunny, cool, humid and windy day":
double[] instance = new double[]
{
codebook.Translate(columnName:"Outlook", value:"Sunny"), //n 0
12.0,
90.0,
codebook.Translate(columnName:"Wind", value:"Strong") // 1
};
// We can obtain a class prediction using
int predicted = naiveBayes.Decide(instance);
// Or compute probabilities of each class using
double[] probabilities = naiveBayes.Probabilities(instance);
// Or obtain the log-likelihood of prediction
double ll = naiveBayes.LogLikelihood(instance);
// Finally, the result can be translated back using
string result = codebook.Translate("PlayTennis", predicted); // Should be "No"
#endregion
Assert.AreEqual("No", result);
Assert.AreEqual(0, predicted);
Assert.AreEqual(0.840, probabilities[0], 1e-3);
Assert.AreEqual(-10.493243476691351, ll, 1e-6);
Assert.AreEqual(1, probabilities.Sum(), 1e-10);
Assert.AreEqual(2, probabilities.Length);
}
示例14: Run
public void Run(String filename)
{
ReadFile(filename);
// Now, we have to convert the textual, categorical data found
// in the table to a more manageable discrete representation.
//
// For this, we will create a codebook to translate text to
// discrete integer symbols:
//
Codification codebook = new Codification(data);
// And then convert all data into symbols
//
DataTable symbols = codebook.Apply(data);
for (int i = 0; i < inputColumns.Count; i++)
if (inputTypes[i] == "string")
CreateDic(inputColumns[i], symbols);
CreateDic(outputColumn, symbols);
double[][] inputs = (from p in symbols.AsEnumerable()
select GetInputRow(p)
).Cast<double[]>().ToArray();
int[] outputs = (from p in symbols.AsEnumerable()
select GetIndex(outputColumn, p[outputColumn].ToString())).Cast<int>().ToArray();
// From now on, we can start creating the decision tree.
//
var attributes = DecisionVariable.FromCodebook(codebook, inputColumns.ToArray());
DecisionTree tree = new DecisionTree(attributes, 5); //outputClasses: 5
// Now, let's create the C4.5 algorithm
C45Learning c45 = new C45Learning(tree);
// and learn a decision tree. The value of
// the error variable below should be 0.
//
double error = c45.Run(inputs, outputs);
// To compute a decision for one of the input points,
// such as the 25-th example in the set, we can use
//
//int y = tree.Compute(inputs[25]);
// Finally, we can also convert our tree to a native
// function, improving efficiency considerably, with
//
//Func<double[], int> func = tree.ToExpression().Compile();
// Again, to compute a new decision, we can just use
//
//int z = func(inputs[25]);
var expression = tree.ToExpression();
Console.WriteLine(tree.ToCode("ClassTest"));
DecisionSet s = tree.ToRules();
Console.WriteLine(s.ToString());
}
示例15: Run
public void Run(String filename)
{
ReadFile(filename);
// Create a new codification codebook to
// convert strings into integer symbols
Codification codebook = new Codification(data, inputColumns.ToArray());
// Translate our training data into integer symbols using our codebook:
DataTable symbols = codebook.Apply(data);
foreach (String s in inputColumns)
CreateDic(s, symbols);
CreateDic(outputColumn, symbols);
int[][] inputs = (from p in symbols.AsEnumerable()
select GetInputRow(p)
).Cast<int[]>().ToArray();
int[] outputs = (from p in symbols.AsEnumerable()
select GetIndex(outputColumn, p[outputColumn].ToString())).Cast<int>().ToArray();
// Gather information about decision variables
DecisionVariable[] attributes = GetDecisionVariables();
int classCount = GetCount(outputColumn); // 2 possible output values for playing tennis: yes or no
//Create the decision tree using the attributes and classes
DecisionTree tree = new DecisionTree(attributes, classCount);
// Create a new instance of the ID3 algorithm
ID3Learning id3learning = new ID3Learning(tree);
//C45Learning c45learning = new C45Learning(tree);
// Learn the training instances!
id3learning.Run(inputs, outputs);
//c45learning.Run(inputs2, outputs);
/*
string answer = codebook.Translate(outputColumn,
tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong")));
Console.WriteLine("Calculate for: Sunny, Hot, High, Strong");
Console.WriteLine("Answer: " + answer);
*/
var expression = tree.ToExpression();
Console.WriteLine(tree.ToCode("ClassTest"));
DecisionSet rules = tree.ToRules();
Console.WriteLine(rules.ToString());
// Compiles the expression to IL
var func = expression.Compile();
}