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 Size of Direct Forecasting Model
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: {[1x1 classreg.learning.regr.CompactRegressionTree]} MaxLag: 7 NumObservations: 465
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: {[1x1 classreg.learning.regr.CompactRegressionTree]} MaxLag: 7
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
Mdl
— Direct forecasting model
DirectForecaster
model object
Direct forecasting model, specified as a DirectForecaster
model object.
Output Arguments
CompactMdl
— Compact direct forecasting model
CompactDirectForecaster
model object
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)