compact
Description
returns a compact model (CompactMdl = compact(Mdl)compactMdl), the compact version of the
trained direct forecasting model Mdl.
CompactMdl does not contain the training data, whereas
Mdl contains the training data in its X and
Y properties. Therefore, although you can predict and forecast using
CompactMdl, you cannot perform tasks such as cross-validation with
the compact model.
Examples
Reduce the size of a full direct forecasting model by removing the training data from the model. You can use a compact model to improve memory efficiency.
Load the sample file TemperatureData.csv, which contains average daily temperatures from January 2015 through July 2016. Read the file into a table. Observe the first eight observations in the table.
temperatures = readtable("TemperatureData.csv");
head(temperatures) Year Month Day TemperatureF
____ ___________ ___ ____________
2015 {'January'} 1 23
2015 {'January'} 2 31
2015 {'January'} 3 25
2015 {'January'} 4 39
2015 {'January'} 5 29
2015 {'January'} 6 12
2015 {'January'} 7 10
2015 {'January'} 8 4
For this example, use a subset of the temperature data that omits the first 100 observations.
Tbl = temperatures(101:end,:);
Create a datetime variable t that contains the year, month, and day information for each observation in Tbl. Then, use t to convert Tbl into a timetable.
numericMonth = month(datetime(Tbl.Month, ... InputFormat="MMMM",Locale="en_US")); t = datetime(Tbl.Year,numericMonth,Tbl.Day); Tbl.Time = t; Tbl = table2timetable(Tbl);
Plot the temperature values in Tbl over time.
plot(Tbl.Time,Tbl.TemperatureF) xlabel("Date") ylabel("Temperature in Fahrenheit")

Create a full direct forecasting model by using the data in Tbl. Train the model using a decision tree learner. All three of the predictors (Year, Month, and Day) are leading predictors because their future values are known. To create new predictors by shifting the leading predictor and response variables backward in time, specify the leading predictor lags and the response variable lags.
Mdl = directforecaster(Tbl,"TemperatureF", ... Learner="tree", ... LeadingPredictors="all",LeadingPredictorLags={0:1,0:1,0:7}, ... ResponseLags=1:7)
Mdl =
DirectForecaster
Horizon: 1
ResponseLags: [1 2 3 4 5 6 7]
LeadingPredictors: [1 2 3]
LeadingPredictorLags: {[0 1] [0 1] [0 1 2 3 4 5 6 7]}
ResponseName: 'TemperatureF'
PredictorNames: {'Year' 'Month' 'Day'}
CategoricalPredictors: 2
Learners: {[1×1 classreg.learning.regr.CompactRegressionTree]}
MaxLag: 7
NumObservations: 465
Properties, Methods
Mdl is a DirectForecaster object. By default, the horizon is one step ahead. That is, Mdl predicts a value that is one step into the future.
Reduce the size of the model by using the compact object function.
compactMdl = compact(Mdl)
compactMdl =
CompactDirectForecaster
Horizon: 1
ResponseLags: [1 2 3 4 5 6 7]
LeadingPredictors: [1 2 3]
LeadingPredictorLags: {[0 1] [0 1] [0 1 2 3 4 5 6 7]}
ResponseName: 'TemperatureF'
PredictorNames: {'Year' 'Month' 'Day'}
CategoricalPredictors: 2
Learners: {[1×1 classreg.learning.regr.CompactRegressionTree]}
MaxLag: 7
Properties, Methods
compactMdl is a CompactDirectForecaster model object. compactMdl contains fewer properties than the full model Mdl.
Display the amount of memory used by each direct forecasting model.
whos("Mdl","compactMdl")
Name Size Bytes Class Attributes Mdl 1x1 119523 timeseries.forecaster.DirectForecaster compactMdl 1x1 43983 timeseries.forecaster.CompactDirectForecaster
The full model is larger than the compact model.
Input Arguments
Direct forecasting model, specified as a DirectForecaster model object.
Output Arguments
Compact direct forecasting model, returned as a CompactDirectForecaster model object.
Version History
Introduced in R2023b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)