该模块提供了一组度量来评估模型预测的质量。除非另有说明,典型的函数将采用一组“预测”和“观察”值,并使用它们来计算所需的度量。所有功能都支持分组(混淆矩阵除外)。
table_in:TEXT。输入表的名称。
table_out:TEXT。输出表的名称。出于一致性,即使在不使用分组的情况下,也会为所有度量输出创建一个表,这可能意味着在某些情况下输出表中只有一个值。
prediction_col:TEXT。输入表中预测值列的名称。
observed_col:TEXT。输入表中观察值列的名称。
num_predictors(只对adjusted_r2_score):INTEGER。不含常数项的预测模型中的参数个数。
training_size(只对adjusted_r2_score):INTEGER。用于训练的行数,不包括任何空行。
grouping_cols(可选):TEXT。缺省值为NULL。输入表中分组列的名称。
该函数返回预测值和观测值之间的决定系数(R2)。R2为1表示回归线与数据完全吻合,而R2为0表示该线完全不适合数据。当将非线性函数拟合为数据时,R2可能出现负值。详情请参阅参考资料[1]。
该函数返回对上述R2调整后的评分。当模型中加入额外的解释变量时,调整后的R2得分用来抵消R2自动增加的问题。它需要两个额外参数描述模型的自由度(num_predictors)和函数训练集的大小(training_size):
这些参数都不能从预测值和测试数据中推断出来,这就是它们是显式输入的原因。详情请参阅参考资料[1]。
该函数返回一个输出表,其中包含一些二进制分类常用的度量指标。各度量的定义如下:
该函数返回二元分类(AUC)下接收者操作特征曲线下的面积。ROC曲线是曲线与分类器的TPR和FPR度量。(这些度量定义见上面的二进制分类)。详情请参阅参考资料[2]。注意二分类函数可以用来获取绘制ROC曲线要求的数据(TPR和FPR值)。
注意:
对于‘binary_classifier’和‘area_under_roc’函数:
该函数返回多类分类的混淆矩阵。矩阵的每一列表示一个预测类中的实例,而每一行代表实际类中的实例。这比精确猜测(准确率)允许更详细的分析。详情请参阅参考资料[3]。请注意,混淆矩阵不支持分组。
drop table if exists test_set;
create table test_set(
pred float8,
obs float8
);
insert into test_set values
(37.5,53.1), (12.3,34.2), (74.2,65.4), (91.1,82.1);
drop table if exists table_out;
select madlib.mean_abs_error( 'test_set', 'table_out', 'pred', 'obs');
select * from table_out;
结果:
mean_abs_error
----------------
13.825
(1 row)
drop table if exists table_out;
select madlib.mean_abs_perc_error( 'test_set', 'table_out', 'pred', 'obs');
select * from table_out;
结果:
avg
-------------------
0.294578793636013
(1 row)
drop table if exists table_out;
select madlib.mean_perc_error( 'test_set', 'table_out', 'pred', 'obs');
select * from table_out;
结果:
mean_perc_error
-------------------
-0.17248930032771
(1 row)
drop table if exists table_out;
select madlib.mean_squared_error( 'test_set', 'table_out', 'pred', 'obs');
select * from table_out;
结果:
mean_squared_error
--------------------
220.3525
(1 row)
drop table if exists table_out;
select madlib.r2_score( 'test_set', 'table_out', 'pred', 'obs');
select * from table_out;
结果:
r2_score
-------------------
0.279929088443375
(1 row)
drop table if exists table_out;
select madlib.adjusted_r2_score( 'test_set', 'table_out', 'pred', 'obs', 3, 100);
select * from table_out;
结果:
r2_score | adjusted_r2_score
-------------------+-------------------
0.279929088443375 | 0.257426872457231
(1 row)
drop table if exists test_set;
create table test_set as
select ((a*8)::integer)/8.0 pred,
((a*0.5+random()*0.5)>0.5) obs
from (select random() as a from generate_series(1,100)) x;
drop table if exists table_out;
select madlib.binary_classifier( 'test_set', 'table_out', 'pred', 'obs');
select threshold, tpr, fpr from table_out order by threshold;
结果:
threshold | tpr | fpr
------------------------+-------------------+--------------------
0.00000000000000000000 | 1 | 1
0.12500000000000000000 | 1 | 0.882352941176471
0.25000000000000000000 | 0.979591836734694 | 0.745098039215686
0.37500000000000000000 | 0.897959183673469 | 0.568627450980392
0.50000000000000000000 | 0.836734693877551 | 0.431372549019608
0.62500000000000000000 | 0.693877551020408 | 0.313725490196078
0.75000000000000000000 | 0.551020408163265 | 0.176470588235294
0.87500000000000000000 | 0.428571428571429 | 0.0980392156862745
1.00000000000000000000 | 0.163265306122449 | 0.0196078431372549
(9 rows)
-- Set extended display on for easier reading of output
\x on
select * from table_out where threshold=0.5;
结果:
-[ RECORD 1 ]---------------------
threshold | 0.50000000000000000000
tp | 41
fp | 22
fn | 8
tn | 29
tpr | 0.836734693877551
tnr | 0.568627450980392
ppv | 0.650793650793651
npv | 0.783783783783784
fpr | 0.431372549019608
fdr | 0.349206349206349
fnr | 0.163265306122449
acc | 0.7
f1 | 0.73214285714285714286
drop table if exists table_out;
select madlib.area_under_roc( 'test_set', 'table_out', 'pred', 'obs');
select * from table_out;
结果:
area_under_roc
---------------------------------------------
0.77691076430572228891752501000400160064025
(1 row)
drop table if exists test_set;
create table test_set as
select (x+y)%5+1 as pred,
(x*y)%5 as obs
from generate_series(1,5) x,
generate_series(1,5) y;
drop table if exists table_out;
select madlib.confusion_matrix( 'test_set', 'table_out', 'pred', 'obs');
select * from table_out order by class;
结果:
row_id | class | confusion_arr
--------+-------+---------------
1 | 0 | {0,1,2,2,2,2}
2 | 1 | {0,2,0,1,1,0}
3 | 2 | {0,0,0,2,2,0}
4 | 3 | {0,0,2,0,0,2}
5 | 4 | {0,2,1,0,0,1}
6 | 5 | {0,0,0,0,0,0}
(6 rows)
[1]https://en.wikipedia.org/wiki/Coefficient_of_determination
[2]https://en.wikipedia.org/wiki/Receiver_operating_characteristic