- Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathoml4sql-classification-regression-xgboost.sql
357 lines (321 loc) · 11.5 KB
/
oml4sql-classification-regression-xgboost.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
-----------------------------------------------------------------------
-- Oracle Machine Learning for SQL (OML4SQL) 21c
--
-- Classification and Regression - XGBoost Algorithm
--
-- Copyright (c) 2021 Oracle Corporation and/or its affilitiates.
--
-- The Universal Permissive License (UPL), Version 1.0
--
-- https://oss.oracle.com/licenses/upl/
-----------------------------------------------------------------------
SET ECHO ON
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 80
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
SET LONG 20000
-----------------------------------------------------------------------
-- (1) Use XGBoost for classification
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- SAMPLE PROBLEM
-----------------------------------------------------------------------
-- Given demographic and purchase data about a set of customers, predict
-- customer's response to an affinity card program using XGboost
--
-----------------------------------------------------------------------
-- SET UP AND ANALYZE THE DATA
-----------------------------------------------------------------------
-------
-- DATA
-------
-- The data for this sample is composed from base tables in SH Schema
-- (See Sample Schema Documentation) and presented through these views:
-- mining_data_build_v (build data)
-- mining_data_test_v (test data)
-- (See dmsh.sql for view definitions).
--
-- Cleanup old settings table
BEGIN EXECUTE IMMEDIATE 'DROP TABLE xgc_sh_settings';
EXCEPTION WHEN OTHERS THEN NULL; END;
/
-- Cleanup old model with the same name
BEGINDBMS_DATA_MINING.DROP_MODEL('XGC_SH_MODEL');
EXCEPTION WHEN OTHERS THEN NULL; END;
/
-- CREATE AND POPULATE A SETTINGS TABLE
--
set echo off
CREATETABLExgc_sh_settings (
setting_name VARCHAR2(30),
setting_value VARCHAR2(4000));
set echo on
BEGIN
-- Populate settings table
INSERT INTO xgc_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.algo_name, dbms_data_mining.algo_xgboost);
-- for 0/1 target, choose binary:logistic as objective
INSERT INTO xgc_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_objective, 'binary:logistic');
INSERT INTO xgc_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_max_depth, '3');
INSERT INTO xgc_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_eta, '1');
-- choose error and auc as eval_metric to evaluate training dataset
INSERT INTO xgc_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_eval_metric, 'error,auc');
INSERT INTO xgc_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_num_round, '10');
END;
/
---------------------
-- CREATE MODEL
BEGIN
DBMS_DATA_MINING.CREATE_MODEL(
model_name =>'XGC_SH_MODEL',
mining_function =>dbms_data_mining.classification,
data_table_name =>'mining_data_build_v',
case_id_column_name =>'cust_id',
target_column_name =>'affinity_card',
settings_table_name =>'xgc_sh_settings');
END;
/
-------------------------
-- DISPLAY MODEL SETTINGS
--
column setting_name format a30
column setting_value format a30
SELECT setting_name, setting_value
FROM user_mining_model_settings
WHERE model_name ='XGC_SH_MODEL'
ORDER BY setting_name;
--------------------------
-- DISPLAY MODEL SIGNATURE
--
column attribute_name format a40
column attribute_type format a20
SELECT attribute_name, attribute_type
FROM user_mining_model_attributes
WHERE model_name ='XGC_SH_MODEL'
ORDER BY attribute_name;
-- Get a list of model views
col view_name format a30
col view_type format a50
SELECT view_name, view_type FROM user_mining_model_views
WHERE model_name='XGC_SH_MODEL'
ORDER BY view_name;
-- Global statistics
column name format a30
column numeric_value format 9999990.999
column string_value format a20
select name, numeric_value, string_value
from DM$VGXGC_SH_MODEL
ORDER BY name;
--attribute importance: top 10 important features
column attribute_name format a25
column attribute_value format a15
column gain format 9.999
column cover format 9.999
column frequency format 9.999
select*from(
select attribute_name, attribute_value, gain, cover, frequency
from DM$VIXGC_SH_MODEL
order by gain desc)
where rownum <=10;
-----------------------------------------------------------------------
-- TEST THE MODEL
-----------------------------------------------------------------------
------------------------------------
-- COMPUTE METRICS TO TEST THE MODEL
--
-- The queries shown below demonstrate the use of new SQL data mining functions
-- along with analytic functions to compute the various test metrics.
--
-- Modelname: xgc_sh_model
-- Target attribute: affinity_card
-- Positive target value: 1
-- (Change as appropriate for a different example)
-- Compute CONFUSION MATRIX
--
-- This query demonstates how to generate a confusion matrix using the new
-- SQL prediction functions for scoring. The returned columns match the
-- schema of the table generated by COMPUTE_CONFUSION_MATRIX procedure.
--
SELECT affinity_card AS actual_target_value,
PREDICTION(XGC_SH_MODEL USING *) AS predicted_target_value,
COUNT(*) AS value
FROM mining_data_test_v
GROUP BY affinity_card, PREDICTION(XGC_SH_MODEL USING *)
ORDER BY1, 2;
-- Compute ACCURACY
--
column accuracy format 9.99
SELECTSUM(correct)/COUNT(*) AS accuracy
FROM (SELECT DECODE(affinity_card,
PREDICTION(XGC_SH_MODEL USING *), 1, 0) AS correct
FROM mining_data_test_v);
-- Compute AUC (Area Under the roc Curve)
column auc format 9.99
WITH
pos_prob_and_counts AS (
SELECT PREDICTION_PROBABILITY(XGC_SH_MODEL, 1 USING *) pos_prob,
DECODE(affinity_card, 1, 1, 0) pos_cnt
FROM mining_data_test_v
),
tpf_fpf AS (
SELECT pos_cnt,
SUM(pos_cnt) OVER (ORDER BY pos_prob DESC) /
SUM(pos_cnt) OVER () tpf,
SUM(1- pos_cnt) OVER (ORDER BY pos_prob DESC) /
SUM(1- pos_cnt) OVER () fpf
FROM pos_prob_and_counts
),
trapezoid_areas AS (
SELECT0.5* (fpf - LAG(fpf, 1, 0) OVER (ORDER BY fpf, tpf)) *
(tpf + LAG(tpf, 1, 0) OVER (ORDER BY fpf, tpf)) area
FROM tpf_fpf
WHERE pos_cnt =1
OR (tpf =1AND fpf =1)
)
SELECTSUM(area) auc
FROM trapezoid_areas;
-----------------------------------------------------------------------
-- (2) Use XGBoost for regression
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- SAMPLE PROBLEM
-----------------------------------------------------------------------
-- Given demographic and purchase data about a set of customers, predict
-- customer's response to an affinity card program using XGboost
--
-----------------------------------------------------------------------
-- SET UP AND ANALYZE THE DATA
-----------------------------------------------------------------------
-------
-- DATA
-------
-- The data for this sample is composed from base tables in SH Schema
-- (See Sample Schema Documentation) and presented through these views:
-- mining_data_build_v (build data)
-- mining_data_test_v (test data)
-- (See dmsh.sql for view definitions).
--
-----------------------------------------------------------------------
-- Cleanup old settings table
BEGIN EXECUTE IMMEDIATE 'DROP TABLE xgr_sh_settings';
EXCEPTION WHEN OTHERS THEN NULL; END;
/
-- Cleanup old model with the same name
BEGINDBMS_DATA_MINING.DROP_MODEL('XGR_SH_MODEL');
EXCEPTION WHEN OTHERS THEN NULL; END;
/
-- CREATE AND POPULATE A SETTINGS TABLE
--
set echo off
CREATETABLExgr_sh_settings (
setting_name VARCHAR2(30),
setting_value VARCHAR2(4000));
set echo on
BEGIN
-- Populate settings table
INSERT INTO xgr_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.algo_name, dbms_data_mining.algo_xgboost);
-- for 0/1 target, choose binary:logistic as objective
INSERT INTO xgr_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_booster, 'gblinear');
INSERT INTO xgr_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_alpha, '0.0001');
INSERT INTO xgr_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_lambda, '1');
INSERT INTO xgr_sh_settings (setting_name, setting_value) VALUES
(dbms_data_mining.xgboost_num_round, '100');
END;
/
---------------------
-- CREATE MODEL
BEGIN
DBMS_DATA_MINING.CREATE_MODEL(
model_name =>'XGR_SH_MODEL',
mining_function =>dbms_data_mining.regression,
data_table_name =>'mining_data_build_v',
case_id_column_name =>'cust_id',
target_column_name =>'age',
settings_table_name =>'xgr_sh_settings');
END;
/
-------------------------
-- DISPLAY MODEL SETTINGS
--
column setting_name format a30
column setting_value format a30
SELECT setting_name, setting_value
FROM user_mining_model_settings
WHERE model_name ='XGR_SH_MODEL'
ORDER BY setting_name;
--------------------------
-- DISPLAY MODEL SIGNATURE
--
column attribute_name format a40
column attribute_type format a20
SELECT attribute_name, attribute_type
FROM user_mining_model_attributes
WHERE model_name ='XGR_SH_MODEL'
ORDER BY attribute_name;
-- Get a list of model views
col view_name format a30
col view_type format a50
SELECT view_name, view_type FROM user_mining_model_views
WHERE model_name='XGR_SH_MODEL'
ORDER BY view_name;
-- Global statistics
column name format a30
column numeric_value format 9999990.999
column string_value format a20
select name, numeric_value, string_value
from DM$VGXGR_SH_MODEL
ORDER BY name;
-- attribute importance
-- show top 5
column ATTRIBUTE_NAME format a25;
column ATTRIBUTE_VALUE format a15;
column weight format 9.999
select*from(
select attribute_name, attribute_value, weight
from DM$VIXGR_SH_MODEL
order by abs(weight) desc)
where rownum <=5;
-----------------------------------------------------------------------
-- TEST THE MODEL
-----------------------------------------------------------------------
------------------------------------
-- COMPUTE METRICS TO TEST THE MODEL
--
-- The queries shown below demonstrate the use of new SQL data mining functions
-- along with analytic functions to compute the various test metrics.
--
-- Modelname: xgr_sh_model
-- Target attribute: age
------------------------------------
-- COMPUTE METRICS TO TEST THE MODEL
--
-- 1. Root Mean Square Error - Sqrt(Mean((x - x')^2))
-- 2. Mean Absolute Error - Mean(|(x - x')|)
--
column rmse format 9999.9
column mae format 9999.9
SELECT SQRT(AVG((A.pred-B.age) * (A.pred-B.age))) rmse,
AVG(ABS(a.pred-B.age)) mae
FROM (SELECT cust_id, prediction(XGR_SH_MODEL using *) pred
FROM mining_data_test_v) A,
mining_data_test_v B
WHEREA.cust_id=B.cust_id;
--- prediction
SELECT CUST_ID, age,
PREDICTION(XGR_SH_MODEL USING *) pred,
PREDICTION_DETAILS(XGR_SH_MODEL USING *) det
FROM mining_data_apply_v
WHERE CUST_ID <100010
ORDER BY CUST_ID;