如何用 C# 实现决策树 (Visual Studio 2008) - 帮助
我有一个决策树,我需要将其转换为 C# 代码。
简单的方法是使用 if-else 语句,但在此解决方案中,我需要创建 4-5 个嵌套条件。
我正在寻找一种更好的方法来做到这一点,到目前为止我读了一些关于规则引擎的内容。
对于开发具有 4-5 个嵌套条件的决策树的有效方法,您还有其他建议吗?
I have a decision tree that i need to turn to a code in C#
The simple way of doing it is using if-else statements but in this solution i will need to create 4-5 nested conditions.
I am looking for a better way to do it and so far i read a little bit about rule engines.
Do you have something else to suggest for an efficient way to develop decision tree with 4-5 nested conditions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在书中实现了一个简单的决策树作为示例。该代码可以在线获取,因此也许您可以将其用作灵感。决策本质上表示为一个类,该类引用
true
分支和false
分支,并包含执行测试的函数:此处为
Decision
是一个包含 Evaluate 方法的基类,源包含一个附加的派生类型,其中包含树的最终决策(是/否)。Client
类型是您使用树分析的示例输入数据。要创建决策树,您可以编写如下内容:
如果您只想编写五个嵌套的静态
if
子句,那么也许只编写if
就可以了。使用此类类型的好处是您可以轻松地构建树 - 例如,重用树的一部分或模块化构造。I implemented a simple decision tree as a sample in my book. The code is available online here, so perhaps you could use it as an inspiration. A decision is essentially represented as a class that has references to
true
branch andfalse
branch and contains a function that does the test:Here,
Decision
is a base class that containsEvaluate
method and the source contains one additional derived type that contains a final decision of the tree (yes/no). The typeClient
is a sample input data that you're analysing using the tree.To create a decision tree, you can write something like:
If you just want to write five nested static
if
clauses then maybe just writingif
is fine. The benefit of using a type like this one is that you can easily compose trees - e.g. reuse a part of a tree or modularize the construction.下面是答案中提到的 Tomas Petricek 的代码 https://stackoverflow.com/a/3889544/5288052 。
包含《真实世界函数式编程》一书所有源代码的 zip 可在此处获取 https://www.manning.com/books/real-world-function-programming。
Below is the Tomas Petricek's code mentioned in the answer https://stackoverflow.com/a/3889544/5288052 .
The zip containing all the source code of the book "Real-World Functional Programming" is available here https://www.manning.com/books/real-world-functional-programming .