使用 sql server 2008 决策树在 C# 中进行预测

发布于 2024-12-15 09:38:25 字数 512 浏览 2 评论 0原文

我正在创建一个 C# 应用程序,我将通过给出一个简单的示例来解释我想要的内容:

考虑此表:

name   age     reply   choice 
------+-------+-------+-------
John   10-20   yes     apple
Kate   20-30   yes     orange
Sam    10-20   yes     apple
Peter  10-20   no      ----
Tom    20-30   no      ----
Mike   10-20   yes     orange

我想为所有已回复的人构建一个预测“年龄”决策树。然后预测那些没有回复的人的选择。

该表保存在 SQL Server 2008 数据库中。 SQL Server 2008 中有一个功能可以实现这一点。我搜索了微软帮助网站,但没有找到任何关于如何使用它的明确指南。

我如何在我的 C# 代码中使用它,有人有它的分步指南吗?

I am creating a C# application I will explain what I want by giving a simple example:

consider this table:

name   age     reply   choice 
------+-------+-------+-------
John   10-20   yes     apple
Kate   20-30   yes     orange
Sam    10-20   yes     apple
Peter  10-20   no      ----
Tom    20-30   no      ----
Mike   10-20   yes     orange

I would like to put together a predictive "age" decision tree for all the persons who have replied. and then predict the choices of the ones who didn't reply.

The table is saved in an SQL Server 2008 database. And there is a feature in the SQL Server 2008 that allows to do that. I searched the Microsoft help website but I didn't find any clear guide on how to use it.

How can I use it in my C# code, Anyone got a step by step guide for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不及他 2024-12-22 09:38:25

这样就可以解决问题了:

-- create table
    declare @t table (name varchar(50), age varchar(50), reply varchar(3), answer varchar(50))
    insert @t (name, age, reply, answer)
    values ('John', '10-20', 'yes', 'apple'),
    ('Kate', '20-30', 'yes', 'orange'),
    ('Sam', '10-20', 'yes', 'apple'),
    ('Peter', '10-20', 'no', '----'),
    ('Tom', '20-30', 'no', '----'),
    ('Mike', '10-20', 'yes', 'orange')

-- get answer
    select  t.name, t.age, t.reply, case t.reply when 'yes' then t.answer else w.answer end answer
    from    @t t
            left join (
                select age, answer
                from (
                    select  age, answer, count(*) cnt, row_number() over (partition by age order by count(*) desc) rnk
                    from    @t
                    where   reply = 'yes' 
                    group by age, answer
                ) s
                where rnk = 1
            ) w on t.age = w.age 

只要找出每个年龄段提供最多的答案,然后如果没有给出,就选择那个答案。

当两个答案之间存在平局时,它只会选择一个。我认为是第一位的,但不能保证它总是会这样做。

请注意,如果您有一组答案为 A:B = 55%:45%,那么所有没有答案的人都会得到答案 A,因此您可以通过这样做来改变总体的平均值。只是让你知道。

This would do the trick:

-- create table
    declare @t table (name varchar(50), age varchar(50), reply varchar(3), answer varchar(50))
    insert @t (name, age, reply, answer)
    values ('John', '10-20', 'yes', 'apple'),
    ('Kate', '20-30', 'yes', 'orange'),
    ('Sam', '10-20', 'yes', 'apple'),
    ('Peter', '10-20', 'no', '----'),
    ('Tom', '20-30', 'no', '----'),
    ('Mike', '10-20', 'yes', 'orange')

-- get answer
    select  t.name, t.age, t.reply, case t.reply when 'yes' then t.answer else w.answer end answer
    from    @t t
            left join (
                select age, answer
                from (
                    select  age, answer, count(*) cnt, row_number() over (partition by age order by count(*) desc) rnk
                    from    @t
                    where   reply = 'yes' 
                    group by age, answer
                ) s
                where rnk = 1
            ) w on t.age = w.age 

Just find out what answer as provided the most for each age and then pick that answer if none was given.

When there is a tie between 2 answers, it just picks one. I think the one that comes first but the are no guarantees that it will always do that.

Be aware tho that if you have a group where the answers are A:B = 55%:45%, then all the people with no answer get answer A, so you'd change the averages of the population by doing this. Just so you know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文