FOREST Procedure
Example 11.2 Generating an Isolation Forest
This example illustrates how to create an isolation forest and generate an anomaly score for each observation. The score estimates how atypical the observation is.
The following DATA step generates a data table and loads the table into your CAS session. The table consists of 456 observations in 13 clumps, which are arranged in a spiral that is defined by the two input variables, x1 and x2. The farther the clump is along the spiral curve from the center, the larger the clump. The data also contain a target variable (y) and an identification variable (id).
data mylib.spiral;
keep x1 x2 y id;
nNow = 200;
sdNow = 0.2;
four_pi = 4 * constant("pi");
call streaminit(3331333);
y = 0;
id = 0;
do theta = 0 to 12;
r = 1.0 - theta / four_pi;
mu1 = cos(theta);
mu2 = sin(theta);
ss = r*r / (mu1*mu1 + mu2*mu2);
s = sqrt(ss);
mu1 = s*mu1;
mu2 = s*mu2;
nNow = nNow * 0.7;
sdNow = sdNow * 0.75;
do i = 1 to nNow;
x1 = rand('normal', mu1, sdNow);
x2 = rand('normal', mu2, sdNow);
y = 1 - y;
id = id + 1;
output;
end;
end;
run;
These statements assume that your CAS engine libref is named mylib, as in the section Using CAS Sessions and CAS Engine Librefs, but you can substitute any appropriately defined CAS engine libref.
The ISOLATION option in the PROC FOREST statement of the following code requests that the procedure create an isolation forest. The OUTPUT statement scores the training data and saves the results to a new table named score, which contains the anomaly score and the predictions for each observation. The predictions are unreliable because the trees are generally shallow and are generated by using random splits.
The following code runs the FOREST procedure with the ISOLATION option to generate an isolation forest. The results are saved in the table mylib.score, and all of the input variables are copied to the output table. Finally, the table is returned from CAS to the SAS client when the SORT procedure is used. In general, you might not want to return data from CAS to the SAS client, especially in the case of large data tables.
proc forest data=mylib.spiral isolation seed=12345;
input x1 x2 /level=interval;
id id;
output out=mylib.score copyvars=(_ALL_);
run;
proc sort data=mylib.score out=score;
by id;
run;
The following code plots the data with a color ramp from cyan to red, based on the anomaly score. The most anomalous observations are colored red.
proc template;
define statgraph anomalyPlot;
begingraph;
layout overlay;
scatterplot y=x2 x=x1 /
name='color'
markerattrs=(symbol=circlefilled)
colormodel=(cyan ligr red)
colorresponse=_Anomaly_;
continuouslegend 'color'/ title='_Anomaly_';
endlayout;
endgraph;
end;
run;
proc sgrender data=score template=anomalyPlot;
run;
Output 11.2.1 shows that the most anomalous points are either near the exterior boundary of the data or in a small clump in the center.
Output 11.2.1: Spiral Data Colored by Anomaly Score
