Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

This example shows how to perform statistical analysis and machine learning on out-of-memory data with MATLAB® and Statistics and Machine Learning Toolbox™.

Tall arrays and tables are designed for working with out-of-memory data. This type of data consists of a very large number of rows (observations) compared to a smaller number of columns (variables). Instead of writing specialized code that takes into account the huge size of the data, such as with MapReduce, tall arrays let you work with large data sets in a manner similar to in-memory MATLAB arrays. The fundamental difference is that tall arrays typically remain unevaluated until you request that the calculations be performed.

This example works with a subset of data on a single computer to develop a linear regression model, and then it scales up to analyze all of the data set. You can scale up this analysis even further to:

Work with data that cannot be read into memory

Work with data distributed across clusters using MATLAB Distributed Computing Server™

Integrate with big data systems like Hadoop® and Spark®

Several unsupervised and supervised learning algorithms in Statistics and Machine Learning Toolbox are available to work with tall arrays to perform data mining and predictive modeling with out-of-memory data. These algorithms are appropriate for out-of-memory data and can include slight variations from the in-memory algorithms. Capabilities include:

k-Means clustering

Linear regression

Generalized linear regression

Logistic regression

Discriminant analysis

The machine learning workflow for out-of-memory data in MATLAB is similar to in-memory data:

Preprocess

Explore

Develop model

Validate model

Scale up to larger data

This example follows a similar structure in developing a predictive model for airline delays. The data includes a large file of airline flight information from 1987 through 2008. The example goal is to predict the departure delay based on a number of variables.

Details on the fundamental aspects of tall arrays are included in the example Analyze Big Data in MATLAB Using Tall Arrays (MATLAB). This example extends the analysis to include machine learning with tall arrays.

A datastore is a repository for collections of data that are too large to fit in memory. You can create a datastore from a number of different file formats as the first step to create a tall array from an external data source.

Create a datastore for the sample file `airlinesmall.csv`

. Select the variables of interest, treat `'NA'`

values as missing data, and generate a preview table of the data.

ds = datastore(fullfile(matlabroot,'toolbox','matlab','demos','airlinesmall.csv')); ds.SelectedVariableNames = {'Year','Month','DayofMonth','DayOfWeek',... 'DepTime','ArrDelay','DepDelay','Distance'}; ds.TreatAsMissing = 'NA'; pre = preview(ds)

pre = 8x8 table Year Month DayofMonth DayOfWeek DepTime ArrDelay DepDelay Distance ____ _____ __________ _________ _______ ________ ________ ________ 1987 10 21 3 642 8 12 308 1987 10 26 1 1021 8 1 296 1987 10 23 5 2055 21 20 480 1987 10 23 5 1332 13 12 296 1987 10 22 4 629 4 -1 373 1987 10 28 3 1446 59 63 308 1987 10 8 4 928 3 -2 447 1987 10 10 6 859 11 -1 954

Create a tall table backed by the datastore to facilitate working with the data. The underlying data type of a tall array depends on the type of datastore. In this case, the datastore is tabular text and returns a tall table. The display includes a preview of the data, with indication that the size is unknown.

tt = tall(ds)

Starting parallel pool (parpool) using the 'local' profile ... connected to 12 workers. tt = Mx8 tall table Year Month DayofMonth DayOfWeek DepTime ArrDelay DepDelay Distance ____ _____ __________ _________ _______ ________ ________ ________ 1987 10 21 3 642 8 12 308 1987 10 26 1 1021 8 1 296 1987 10 23 5 2055 21 20 480 1987 10 23 5 1332 13 12 296 1987 10 22 4 629 4 -1 373 1987 10 28 3 1446 59 63 308 1987 10 8 4 928 3 -2 447 1987 10 10 6 859 11 -1 954 : : : : : : : : : : : : : : : :

This example aims to explore the time of day and day of week in more detail. Convert the day of week to categorical data with labels and determine the hour of day from the numeric departure time variable.

tt.DayOfWeek = categorical(tt.DayOfWeek,1:7,{'Sun','Mon','Tues',... 'Wed','Thu','Fri','Sat'}); tt.Hr = discretize(tt.DepTime,0:100:2400,0:23)

tt = Mx9 tall table Year Month DayofMonth DayOfWeek DepTime ArrDelay DepDelay Distance Hr ____ _____ __________ _________ _______ ________ ________ ________ __ 1987 10 21 Tues 642 8 12 308 6 1987 10 26 Sun 1021 8 1 296 10 1987 10 23 Thu 2055 21 20 480 20 1987 10 23 Thu 1332 13 12 296 13 1987 10 22 Wed 629 4 -1 373 6 1987 10 28 Tues 1446 59 63 308 14 1987 10 8 Wed 928 3 -2 447 9 1987 10 10 Fri 859 11 -1 954 8 : : : : : : : : : : : : : : : : : :

Include only years after 2000 and ignore rows with missing data. Identify data of interest by logical condition.

```
idx = tt.Year >= 2000 & ...
~any(ismissing(tt),2);
tt = tt(idx,:);
```

A number of exploratory functions are available for tall arrays. For a list of supported statistics functions, see Tall Array Support, Usage Notes, and Limitations.

The `grpstats`

function calculates grouped statistics of tall arrays. Explore the data by determining the centrality and spread of the data with summary statistics grouped by day of week. Also, explore the correlation between the departure delay and arrival delay.

g = grpstats(tt(:,{'ArrDelay','DepDelay','DayOfWeek'}),'DayOfWeek',... {'mean','std','skewness','kurtosis'})

g = Mx11 tall table GroupLabel DayOfWeek GroupCount mean_ArrDelay std_ArrDelay skewness_ArrDelay kurtosis_ArrDelay mean_DepDelay std_DepDelay skewness_DepDelay kurtosis_DepDelay __________ _________ __________ _____________ ____________ _________________ _________________ _____________ ____________ _________________ _________________ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? : : : : : : : : : : : : : : : : : : : : : :

C = corr(tt.DepDelay,tt.ArrDelay)

C = MxNx... tall array ? ? ? ... ? ? ? ... ? ? ? ... : : : : : :

These commands produce more tall arrays. The commands are not executed until you explicitly gather the results into the workspace. The `gather`

command triggers execution and attempts to minimize the number of passes required through the data to perform the calculations. `gather`

requires that the resulting variables fit into memory.

[statsByDay,C] = gather(g,C)

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 11 sec Evaluation completed in 30 sec statsByDay = 7x11 table GroupLabel DayOfWeek GroupCount mean_ArrDelay std_ArrDelay skewness_ArrDelay kurtosis_ArrDelay mean_DepDelay std_DepDelay skewness_DepDelay kurtosis_DepDelay __________ _________ __________ _____________ ____________ _________________ _________________ _____________ ____________ _________________ _________________ 'Wed' Wed 8489 9.3324 37.406 5.1638 57.479 10 33.426 6.4336 85.426 'Tues' Tues 8381 6.4786 32.322 4.374 38.694 7.6083 28.394 5.2012 46.249 'Fri' Fri 7339 4.1512 32.1 7.082 120.53 7.0857 29.339 8.9387 168.37 'Sat' Sat 8045 7.132 33.108 3.6457 22.991 9.1557 29.731 4.5135 31.228 'Mon' Mon 8443 5.2487 32.453 4.5811 37.175 6.8319 28.573 5.6468 50.271 'Sun' Sun 8570 7.7515 36.003 5.7943 80.91 9.3324 32.516 7.2146 118.25 'Thu' Thu 8601 10.053 36.18 4.1381 37.051 10.923 34.708 1.1414 138.38 C = 0.8966

The variables containing the results are now in-memory variables in the Workspace. Based on these calculations, variation occurs in the data and there is correlation between the delays that you can investigate further.

Explore the effect of day of week and hour of day and gain additional statistical information such as the standard error of the mean and the 95% confidence interval for the mean. You can pass the entire tall table and specify which variables to perform calculations on.

byDayHr = grpstats(tt,{'Hr','DayOfWeek'},... {'mean','sem','meanci'},'DataVar','DepDelay'); byDayHr = gather(byDayHr);

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 5 sec Evaluation completed in 13 sec

Due to the data partitioning of the tall array, the output might be unordered. Rearrange the data in memory for further exploration.

x = unstack(byDayHr(:,{'Hr','DayOfWeek','mean_DepDelay'}),... 'mean_DepDelay','DayOfWeek'); x = sortrows(x)

x = 24x8 table Hr Sun Mon Tues Wed Thu Fri Sat __ _______ ________ ________ _______ _______ _______ _______ 0 38.519 71.914 39.656 34.667 90 25.536 65.579 1 45.846 27.875 93.6 125.23 52.765 38.091 29.182 2 NaN 39 102 NaN 78.25 -1.5 NaN 3 NaN NaN NaN NaN -377.5 53.5 NaN 4 -7 -6.2857 -7 -7.3333 -10.5 -5 NaN 5 -2.2409 -3.7099 -4.0146 -3.9565 -3.5897 -3.5766 -4.1474 6 0.4 -1.8909 -1.9802 -1.8304 -1.3578 0.84161 -2.2537 7 3.4173 -0.47222 -0.18893 0.71546 0.08 1.069 -1.3221 8 2.3759 1.4054 1.6745 2.2345 2.9668 1.6727 0.88213 9 2.5325 1.6805 2.7656 2.683 5.6138 3.4838 2.5011 10 6.37 5.2868 3.6822 7.5773 5.3372 6.9391 4.9979 11 6.9946 4.9165 5.5639 5.5936 7.0435 4.8989 5.2839 12 5.673 5.1193 5.7081 7.9178 7.5269 8.0625 7.4686 13 8.0879 7.1017 5.0857 8.8082 8.2878 8.0675 6.2107 14 9.5164 5.8343 7.416 9.5954 8.6667 6.0677 8.444 15 8.1257 4.8802 7.4726 9.8674 10.235 7.167 8.6219 16 12.302 7.4968 11.406 12.413 12.874 10.962 12.908 17 11.47 8.9495 10.658 12.961 13.487 7.9034 8.9327 18 15.148 13.849 11.266 15.406 16.706 11.022 13.042 19 14.77 11.618 15.053 17.561 21.032 12.644 16.404 20 17.711 13.942 17.105 22.382 25.945 11.223 22.152 21 23.727 17.276 23.092 25.794 28.828 14.011 22.682 22 29.383 24.949 28.265 30.649 37.38 24.328 36.272 23 38.296 33.966 34.904 47.592 49.523 29.5 44.122

Currently, you can visualize tall array data using `histogram`

, `histogram2`

, `binScatterPlot`

, and `ksdensity`

. The visualizations all trigger execution, similar to calling the `gather`

function.

Use `binScatterPlot`

to examine the relationship between the `Hr`

and `DepDelay`

variables.

binScatterPlot(tt.Hr,tt.DepDelay,'Gamma',0.25) ylim([0 500]) xlabel('Time of Day') ylabel('Delay (Minutes)')

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 3 sec Evaluation completed in 5 sec Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 3 sec Evaluation completed in 4 sec

As noted in the output display, the visualizations often take two passes through the data: one to perform the binning, and one to perform the binned calculation and produce the visualization.

To develop a machine learning model, it is useful to reserve part of the data to train and develop the model and another part of the data to test the model. A number of ways exist for you to split the data into training and validation sets.

Use `datasample`

to obtain a random sampling of the data. Then use `cvpartition`

to partition the data into test and training sets. To obtain nonstratified partitions, set a uniform grouping variable by multiplying the data samples by zero.

rng(1234) data = datasample(tt,25000,'Replace',false); groups = 0*data.DepDelay; y = cvpartition(groups,'HoldOut',1/3);

dataTrain = data(training(y),:); dataTest = data(test(y),:);

Build a model to predict the departure delay based on several variables. The linear regression model function `fitlm`

behaves similarly to the in-memory function. However, calculations with tall arrays result in a `CompactLinearModel`

, which is more efficient for large data sets. Model fitting triggers execution because it is an iterative process.

model = fitlm(dataTrain,'ResponseVar','DepDelay')

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 2: Completed in 3 sec - Pass 2 of 2: Completed in 6 sec Evaluation completed in 12 sec model = Compact linear regression model: DepDelay ~ [Linear formula with 9 terms in 8 predictors] Estimated Coefficients: Estimate SE tStat pValue __________ __________ ________ __________ (Intercept) -109.17 101.06 -1.0803 0.28003 Year 0.053138 0.050412 1.0541 0.29187 Month 0.07556 0.037513 2.0142 0.044002 DayofMonth 0.013388 0.014639 0.91452 0.36046 DayOfWeek_Mon -0.37364 0.46724 -0.79969 0.4239 DayOfWeek_Tues -0.80986 0.47137 -1.7181 0.0858 DayOfWeek_Wed -0.63797 0.47164 -1.3527 0.17618 DayOfWeek_Thu -0.70947 0.46599 -1.5225 0.1279 DayOfWeek_Fri 0.71784 0.48925 1.4672 0.14233 DayOfWeek_Sat -0.04999 0.48035 -0.10407 0.91712 DepTime 0.013196 0.0072274 1.8258 0.067895 ArrDelay 0.80774 0.0036434 221.7 0 Distance 0.00082956 0.00022675 3.6585 0.00025452 Hr -1.0154 0.7215 -1.4073 0.15935 Number of observations: 16668, Error degrees of freedom: 16654 Root Mean Squared Error: 16.5 R-squared: 0.757, Adjusted R-Squared 0.757 F-statistic vs. constant model: 4e+03, p-value = 0

The display indicates fit information, as well as coefficients and associated coefficient statistics.

The `model`

variable contains information about the fitted model as properties, which you can access using dot notation. Alternatively, double click the variable in the Workspace to explore the properties interactively.

model.Rsquared

ans = struct with fields: Ordinary: 0.7574 Adjusted: 0.7572

Predict new values based on the model, calculate the residuals, and visualize using a histogram. The `predict`

function predicts new values for both tall and in-memory data.

pred = predict(model,dataTest); err = pred - dataTest.DepDelay; figure histogram(err,'BinLimits',[-100 100],'Normalization','pdf') title('Histogram of Residuals')

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 2: Completed in 5 sec - Pass 2 of 2: Completed in 3 sec Evaluation completed in 10 sec

Looking at the output p-values in the display, some variables might be unnecessary in the model. You can reduce the complexity of the model by removing these variables.

Examine the significance of the variables in the model more closely using `anova`

.

a = anova(model)

a = 9x5 table SumSq DF MeanSq F pValue __________ _____ __________ _______ __________ Year 303.85 1 303.85 1.1111 0.29187 Month 1109.5 1 1109.5 4.0571 0.044002 DayofMonth 228.72 1 228.72 0.83635 0.36046 DayOfWeek 3858.4 6 643.07 2.3515 0.028491 DepTime 911.68 1 911.68 3.3336 0.067895 ArrDelay 1.3441e+07 1 1.3441e+07 49150 0 Distance 3660.3 1 3660.3 13.384 0.00025452 Hr 541.64 1 541.64 1.9806 0.15935 Error 4.5545e+06 16654 273.48

Based on the p-values, the variables `Year`

, `Month`

, and `DayOfMonth`

are not significant to this model, so you can remove them without negatively affecting the model quality.

To explore these model parameters further, use interactive visualizations such as `plotSlice`

, `plotInterations`

, and `plotEffects`

. For example, use `plotEffects`

to examine the estimated effect that each predictor variable has on the departure delay.

plotEffects(model)

Based on these calculations, `ArrDelay`

is the main effect in the model (it is highly correlated to `DepDelay`

). The other effects are observable, but have much less impact. In addition, `Hr`

was determined from `DepTime`

, so only one of these variables is necessary to the model.

Reduce the number of variables to exclude all date components, and then fit a new model.

```
model2 = fitlm(dataTrain,'DepDelay ~ DepTime + ArrDelay + Distance')
```

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 3 sec Evaluation completed in 4 sec model2 = Compact linear regression model: DepDelay ~ 1 + DepTime + ArrDelay + Distance Estimated Coefficients: Estimate SE tStat pValue __________ __________ _______ __________ (Intercept) -1.9093 0.42096 -4.5355 5.7857e-06 DepTime 0.0030033 0.00027088 11.087 1.8177e-28 ArrDelay 0.80732 0.0036359 222.04 0 Distance 0.00084625 0.00022666 3.7336 0.0001894 Number of observations: 16668, Error degrees of freedom: 16664 Root Mean Squared Error: 16.5 R-squared: 0.757, Adjusted R-Squared 0.757 F-statistic vs. constant model: 1.73e+04, p-value = 0

Even with the model simplified, it can be useful to further adjust the relationships between the variables and include specific interactions. To experiment further, repeat this workflow with smaller tall arrays. For performance while tuning the model, you can consider working with a small extraction of in-memory data before scaling up to the entire tall array.

In this example, you can use functionality like stepwise regression, which is suited for iterative, in-memory model development. After tuning the model, you can scale up to use tall arrays.

Gather a subset of the data into the workspace and use `stepwiselm`

to iteratively develop the model in memory.

subset = gather(dataTest); sModel = stepwiselm(subset,'ResponseVar','DepDelay')

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 2 sec Evaluation completed in 2 sec 1. Adding ArrDelay, FStat = 40563.0642, pValue = 0 2. Adding DepTime, FStat = 54.9857, pValue = 1.33322e-13 3. Adding DepTime:ArrDelay, FStat = 38.8048, pValue = 4.91088e-10 4. Adding Distance, FStat = 26.2606, pValue = 3.05021e-07 5. Adding ArrDelay:Distance, FStat = 72.619, pValue = 1.84869e-17 sModel = Linear regression model: DepDelay ~ 1 + DepTime*ArrDelay + ArrDelay*Distance Estimated Coefficients: Estimate SE tStat pValue ___________ __________ _______ __________ (Intercept) -0.75349 0.4493 -1.677 0.093574 DepTime 0.0017591 0.00029405 5.9822 2.2923e-09 ArrDelay 0.77392 0.012848 60.239 0 Distance 0.0015654 0.00024075 6.5021 8.3786e-11 DepTime:ArrDelay 4.9203e-05 7.4005e-06 6.6487 3.1436e-11 ArrDelay:Distance -5.6492e-05 6.6292e-06 -8.5217 1.8487e-17 Number of observations: 8332, Error degrees of freedom: 8326 Root Mean Squared Error: 12.4 R-squared: 0.834, Adjusted R-Squared 0.833 F-statistic vs. constant model: 8.34e+03, p-value = 0

The model that results from the stepwise fit includes interaction terms.

Now try to fit a model for the tall data by using `fitlm`

with the formula returned by `stepwiselm`

.

model3 = fitlm(dataTrain,sModel.Formula)

Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 3 sec Evaluation completed in 4 sec model3 = Compact linear regression model: DepDelay ~ 1 + DepTime*ArrDelay + ArrDelay*Distance Estimated Coefficients: Estimate SE tStat pValue ___________ __________ _______ ___________ (Intercept) -0.95294 0.41598 -2.2908 0.021985 DepTime 0.0016495 0.00026962 6.1179 9.6928e-10 ArrDelay 0.676 0.010768 62.778 0 Distance 0.0016168 0.00022463 7.1974 6.3967e-13 DepTime:ArrDelay 0.00014251 6.0575e-06 23.526 2.0204e-120 ArrDelay:Distance -0.00011011 5.9044e-06 -18.648 7.8936e-77 Number of observations: 16668, Error degrees of freedom: 16662 Root Mean Squared Error: 16.1 R-squared: 0.77, Adjusted R-Squared 0.77 F-statistic vs. constant model: 1.11e+04, p-value = 0

You can repeat this process to continue to adjust the linear model. However, in this case, you should explore different types of regression that might be more appropriate for this data. For example, if you do not want to include the arrival delay, then this type of linear model is no longer appropriate. See Logistic Regression with Tall Arrays for more information.

A key capability of tall arrays in MATLAB and Statistics and Machine Learning Toolbox is the connectivity to platforms such as Hadoop and Spark. You can even compile the code and run it on Spark using MATLAB Compiler™. See Extend Tall Arrays with Other Products (MATLAB) for more information about using these products:

Database Toolbox™

Parallel Computing Toolbox™

MATLAB® Distributed Computing Server™

MATLAB Compiler™

Was this topic helpful?