I have a training data set of 136 records, 25 of them are positive examples and the remaining negative. The goal of the project is to build a predictive model that gives the probabilities of data points being positive. A logistic regression model is selected for its structural and implementation simplicity. To make the model more robust and able to perform reasonably well on new data set, I decide to build 20 logistic models, each based on a randomly sampled set of the original 136 records with replacement. The prediction probabilities produced by these 20 models are averaged to arrive at the final score. My first step is to generate 20 random sampling sets of 136 records from the original training set. The sampled set will have the same size but some records will be picked more zero, one or more than one times. I write the following PL/SQL to do the 20 rounds of random sampling with replacement.

create table t_bagging (iter number, id number); declare i number; begin for i in 1..20 loop insert into t_bagging select i, 1+mod(abs(dbms_random.random),136) from t_train; dbms_output.put_line(i); end loop; commit; end; /In the above script, t_trian is the original training set having 136 records with unique identifier starting from 1 to 136. The function dbms_random.random generates a uniformly distributed random integer from from -2^^31 to 2^^31. I make the random number positive by taking the absolute value using abs() function. Mod() function forces the random number to be within the range of 0 and 135. I also add 1 after applying mod function so that its range becomes from 1 to 136. Next, I write the following script to create 20 views which will be used as the new training sets for building 20 models.

declare sqlstr varchar2(512); begin for i in (select distinct iter from T_BAGGING order by iter) loop sqlstr:='create or replace view v_tr_bag'||i.iter||' as select '|| '* from t_training a, T_BAGGING c'|| ' where a.id=c.id and c.iter='||i.iter; --dbms_output.put_line(sqlstr); execute immediate sqlstr; end loop; end; /