Published on :
Predictive Modeling CREATION_INTERNE

Scoring New Data with an Existing Forest Model

This code is also available in: Deutsch Español Français
Awaiting validation
The example demonstrates how the FOREST procedure can be used with the OUTMODEL= option to save a trained model. This model can then be loaded via the INMODEL= option to score new observations. It is crucial not to modify the saved model table to ensure the validity of the scores. The example uses the JunkMail dataset from the Sashelp library to classify emails as spam (1) or not (0) based on 57 predictive variables. Compatibility with SAS© Viya 4 is ensured by using CAS libraries.
Data Analysis

Type : CREATION_INTERNE


The examples use the Sashelp.JunkMail dataset, which is loaded into a temporary CAS table named mycas.junkmail for procedure execution.

1 Code Block
PROC FOREST Data
Explanation :
This example initializes a CAS session and loads the 'JunkMail' dataset from Sashelp into a CAS table named 'mycas.junkmail'. It then trains a basic random forest model using the FOREST procedure with default parameters, except for a seed for reproducibility. The model's fit statistics are saved and displayed.
Copied!
1caslib _all_ assign;
2 
3/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4DATA mycas.junkmail;
5 SET sashelp.junkmail;
6RUN;
7 
8/* Entraîner un modèle Forest de base */
9PROC FOREST DATA=mycas.junkmail seed=54321;
10 INPUT Address Addresses All Bracket Business CS CapAvg CapLong
11 CapTotal Conference Credit DATA Direct Dollar Edu Email
12 Exclamation Font Free George HP HPL Internet Lab Labs
13 Mail Make Meeting Money Order Original Our Over PM Paren
14 Parts People Pound Project RE Receive Remove Semicolon
15 TABLE Technology Telnet Will You Your _000 _85 _415 _650
16 _857 _1999 _3D / level = interval;
17 target class /level=nominal;
18 ods OUTPUT FitStatistics=basic_fit_stats;
19RUN;
20 
21/* Afficher les statistiques d'ajustement */
22PROC PRINT DATA=basic_fit_stats;
23RUN;
2 Code Block
PROC FOREST
Explanation :
This example extends the basic use case by incorporating tree growth options (NODESIZE and MAXDEPTH) to control tree complexity. It also uses cross-validation (CROSSVALIDATION CV=5) to more robustly evaluate model performance. Predicted scores are saved, and the first observations are displayed.
Copied!
1caslib _all_ assign;
2 
3/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4DATA mycas.junkmail;
5 SET sashelp.junkmail;
6RUN;
7 
8/* Entraîner un modèle Forest avec validation croisée et options d'arbre */
9PROC FOREST DATA=mycas.junkmail seed=67890;
10 INPUT Address Addresses All Bracket Business CS CapAvg CapLong
11 CapTotal Conference Credit DATA Direct Dollar Edu Email
12 Exclamation Font Free George HP HPL Internet Lab Labs
13 Mail Make Meeting Money Order Original Our Over PM Paren
14 Parts People Pound Project RE Receive Remove Semicolon
15 TABLE Technology Telnet Will You Your _000 _85 _415 _650
16 _857 _1999 _3D / level = interval;
17 target class /level=nominal;
18 grow nodesize=5 maxdepth=10; /* Options courantes pour la croissance des arbres */
19 crossvalidation cv=5; /* Validation croisée à 5 plis */
20 OUTPUT out=mycas.forest_cv_scores predicted_class;
21 ods OUTPUT FitStatistics=cv_fit_stats;
22RUN;
23 
24/* Afficher les premières observations des scores de validation croisée */
25PROC PRINT DATA=mycas.forest_cv_scores (obs=10);
26RUN;
3 Code Block
PROC FOREST
Explanation :
This advanced example uses the FOREST procedure to train a model while performing variable selection based on their importance (SELECTION METHOD=VARIABLEIMPORTANCE). The IMPORTANCE statement is added to display the contribution of each variable to the model. Predicted scores are saved, and variable importance is displayed in a separate table.
Copied!
1caslib _all_ assign;
2 
3/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4DATA mycas.junkmail;
5 SET sashelp.junkmail;
6RUN;
7 
8/* Entraîner un modèle Forest avec sélection de variables et importance */
9PROC FOREST DATA=mycas.junkmail seed=98765;
10 INPUT Address Addresses All Bracket Business CS CapAvg CapLong
11 CapTotal Conference Credit DATA Direct Dollar Edu Email
12 Exclamation Font Free George HP HPL Internet Lab Labs
13 Mail Make Meeting Money Order Original Our Over PM Paren
14 Parts People Pound Project RE Receive Remove Semicolon
15 TABLE Technology Telnet Will You Your _000 _85 _415 _650
16 _857 _1999 _3D / level = interval;
17 target class /level=nominal;
18 selection method=variableimportance; /* Sélection de variables basée sur l'importance */
19 importance; /* Demander l'affichage de l'importance des variables */
20 OUTPUT out=mycas.forest_importance_scores predicted_class;
21 ods OUTPUT FitStatistics=importance_fit_stats VariableImportance=var_importance_table;
22RUN;
23 
24/* Afficher l'importance des variables */
25PROC PRINT DATA=var_importance_table;
26RUN;
27 
28/* Afficher les premières observations des scores */
29PROC PRINT DATA=mycas.forest_importance_scores (obs=10);
30RUN;
4 Code Block
PROC FOREST
Explanation :
This example, as presented in the documentation, demonstrates the complete process of saving and reusing a model. First, a model is trained on 'mycas.junkmail' and saved using the OUTMODEL= option into 'mycas.forest_model'. Then, this saved model is loaded via the INMODEL= option to score 'new' data (in this example, the same data is used for demonstration purposes). Predicted scores are saved in 'mycas.score_later', and the first observations are displayed, illustrating the ability to apply a pre-trained model.
Copied!
1caslib _all_ assign;
2 
3/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4DATA mycas.junkmail;
5 SET sashelp.junkmail;
6RUN;
7 
8/* Entraîner un modèle Forest et le sauvegarder (première exécution) */
9PROC FOREST DATA=mycas.junkmail outmodel=mycas.forest_model seed=12345;
10 INPUT Address Addresses All Bracket Business CS CapAvg CapLong
11 CapTotal Conference Credit DATA Direct Dollar Edu Email
12 Exclamation Font Free George HP HPL Internet Lab Labs
13 Mail Make Meeting Money Order Original Our Over PM Paren
14 Parts People Pound Project RE Receive Remove Semicolon
15 TABLE Technology Telnet Will You Your _000 _85 _415 _650
16 _857 _1999 _3D / level = interval;
17 target class /level=nominal;
18 OUTPUT out=mycas.score_at_runtime predicted_class;
19RUN;
20 
21/* Utiliser le modèle sauvegardé pour scorer de nouvelles données (ici, les mêmes données pour démonstration) */
22/* (Simule le scoring de 'nouvelles' données à un moment ultérieur ou sur un autre jeu de données) */
23PROC FOREST DATA=mycas.junkmail inmodel=mycas.forest_model;
24 OUTPUT out=mycas.score_later predicted_class;
25RUN;
26 
27/* Afficher les premières observations des données scorées */
28PROC PRINT DATA=mycas.score_later (obs=10);
29RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright © SAS Institute Inc. All rights reserved.


Banner
Expert Advice
Expert
Stéphanie
Spécialiste Machine Learning et IA.
« In the high-performance environment of SAS Viya, efficiently managing the transition from model training to production scoring is a technical priority. The OUTMODEL= and INMODEL= options in PROC FOREST provide a native, table-based mechanism to persist and reuse your Random Forest logic across different sessions or datasets. »