[英]How to implement decision tree with c# (visual studio 2008) - Help
我有一個決策樹,我需要轉向C#中的代碼
這樣做的簡單方法是使用if-else語句,但在此解決方案中,我需要創建4-5個嵌套條件。
我正在尋找一種更好的方法來做到這一點,到目前為止我讀了一些關於規則引擎的內容。
您是否有其他建議以有效的方式開發具有4-5個嵌套條件的決策樹?
我在本書中實現了一個簡單的決策樹作為樣本。 這里的代碼可以在線獲取 ,所以也許您可以將它作為靈感來使用。 決策基本上表示為一個引用了true
branch和false
分支的類,並包含一個執行測試的函數:
class DecisionQuery : Decision {
public Decision Positive { get; set; }
public Decision Negative { get; set; }
// Primitive operation to be provided by the user
public Func<Client, bool> Test { get; set; }
public override bool Evaluate(Client client) {
// Test a client using the primitive operation
bool res = Test(client);
// Select a branch to follow
return res ? Positive.Evaluate(client) : Negative.Evaluate(client);
}
}
這里, Decision
是一個包含Evaluate
方法的基類,source包含一個額外的派生類型,它包含樹的最終決定(是/否)。 Client
類型是您使用樹分析的示例輸入數據。
要創建決策樹,您可以編寫如下內容:
var tree = new DecisionQuery {
Test = (client) => client.Income > 40000,
Positive = otherTree,
Negative = someOtherTree
};
如果您只想編寫五個嵌套的靜態if
子句,那么可能只是寫if
。 使用類似這樣的類型的好處是您可以輕松地組合樹 - 例如重用樹的一部分或模塊化構造。
以下是答案https://stackoverflow.com/a/3889544/5288052中提到的Tomas Petricek的代碼。
包含“Real-World Functional Programming”一書的所有源代碼的zip文件可在此處獲得https://www.manning.com/books/real-world-functional-programming 。
// Section 8.4.2 Decision trees in C#
// Listing 8.15 Object oriented decision tree (C#)
abstract class Decision {
// Tests the given client
public abstract void Evaluate(Client client);
}
class DecisionResult : Decision {
public bool Result { get; set; }
public override void Evaluate(Client client) {
// Print the final result
Console.WriteLine("OFFER A LOAN: {0}", Result ? "YES" : "NO");
}
}
// Listing 8.16 Simplified implementation of Template method
class DecisionQuery : Decision {
public string Title { get; set; }
public Decision Positive { get; set; }
public Decision Negative { get; set; }
// Primitive operation to be provided by the user
public Func<Client, bool> Test { get; set; }
public override void Evaluate(Client client) {
// Test a client using the primitive operation
bool res = Test(client);
Console.WriteLine(" - {0}? {1}", Title, res ? "yes" : "no");
// Select a branch to follow
if (res) Positive.Evaluate(client);
else Negative.Evaluate(client);
}
}
static void MainDecisionTrees()
{
// The tree is constructed from a query
var tree =
new DecisionQuery
{
Title = "More than $40k",
// Test is specified using a lambda function
Test = (client) => client.Income > 40000,
// Sub-trees can be 'DecisionResult' or 'DecisionQuery'
Positive = new DecisionResult { Result = true },
Negative = new DecisionResult { Result = false }
};
// Test a client using this tree
// Create client using object initializer
var john = new Client {
Name = "John Doe", Income = 40000, YearsInJob = 1,
UsesCreditCard = true, CriminalRecord = false
};
tree.Evaluate(john);
}
private static void Main(string[] args)
{
MainDecisionTrees();
}
只是因為...我有一個去,結果在這里... https://github.com/jkennerley/DeeTree
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.