2017-09-15 62 views
0

我实现了一个随机森林算法来预测一些东西。 算法后,我的数据库表:SparkSQL四倍表

id  order  order_forecast 
----------------------------------------------- 
1  yes   yes 
2  yes   no 
3  no   yes 
4  no   no 
5  yes   yes 
6  no   yes 
7  yes   no 
8  yes   yes 
9  yes   yes 

,现在我想建立某种形式的四格表的检查,如果随机森林是正确的。

例如

 yes    no 
    ------------------------------- 
yes | 4    2 
no | 2    1 


Correct classified: 5 
Wrong classified: 4 
Accuracy: 55,55% 
Error: 44,44% 

是否有任何功能或任何简短的方法来做到这一点? 正如我所说我在浏览器界面上工作,我不知道我正在使用哪个DBMS,但我发现窗口功能正在工作。

回答

0

如果你有一个表,然后:

select order, order_forecast, count(*) 
from results r 
group by order, order_forecast; 

这将产生四排,而不是两个。然后,您可以在应用程序中执行额外的计算。

为了让您的特定格式的数据,然后使用:

select order, 
     sum(case when order_forecast = 'yes' then 1 else 0 end) as yes, 
     sum(case when order_forecast = 'no' then 1 else 0 end) as no 
from results 
group by order; 

或者只是得到直接的正确性:

select order, 
     sum(case when order_forecast = order then 1 else 0 end) as correct, 
     sum(case when order_forecast = order then 0 else 1 end) as incorrect 
from results 
group by order; 

order是一列一个非常糟糕的名字,因为它是一个SQL关键字。

+0

谢谢,我会在星期一上班的时候尝试一下,不用担心我只是把这个专栏翻译成英文,在DB上它叫'auftrag' – mafin