Aggregating vectors and matrices
The same methods that are available for aggregating over the columns of data frames are also available for vectors and matrices.
Aggregating over matrices
For matrices, it is possible to aggregate over both the rows and columns. The AggregateRows method aggregates the rows of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateRowsBy method aggregates groups within each row. In the following example, we create a matrix of random numbers and compute the means of its rows. We then create a moving window of length 5, and construct a matrix that contains the moving average of each row:
var m1 = Matrix.CreateRandom(10, 100);
var rowMeans = m1.AggregateRows(Aggregators.Mean);
var window = Grouping.Window(m1.ColumnCount, 5);
var rowMAs = m1.AggregateRowsBy(window, Aggregators.Mean);
The AggregateColumns method aggregates the columns of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateColumnsBy method aggregates groups within each column. The same example as above, but aggregating over columns instead of rows, becomes:
var m2 = Matrix.CreateRandom(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);
There is one more method for aggregating over columns: AggregateColumnsListwise<U>. This method is the same as the regular AggregateColumns. method with the following exception: whenever a row contains a missing value, the entire row is treated as missing. This effectively means that the aggregation is performed after first performing listwise deletion of rows with missing values. In the next example, we first set some missing values and then compute the listwise column means of the matrix:
var m2 = Matrix.CreateRandom(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);
Aggregating over vectors
Similarly, vectors have Aggregate and AggregateBy methods that aggregate over the entire vector or each group, respectively. In the next example, we create a vector of random values and compute its kurtosis. We then compute the kurtosis of each chunk of length 10:
var v = Vector.CreateRandom(100);
var K = v.Aggregate(Aggregators.Kurtosis);
var partition = Grouping.Partition(v.Length, 10);
var Ks = v.AggregateBy(partition, Aggregators.Kurtosis);
The AggregateColumns method aggregates the columns of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateColumnsBy method aggregates groups within each column. The same example as above, but aggregating over columns instead of rows, becomes:
var m2 = Matrix.CreateRandom(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);
There is one more method for aggregating over columns: AggregateColumnsListwise<U>. This method is the same as the regular AggregateColumns. method with the following exception: whenever a row contains a missing value, the entire row is treated as missing. This effectively means that the aggregation is performed after first performing listwise deletion of rows with missing values. In the next example, we first set some missing values and then compute the listwise column means of the matrix:
var m2 = Matrix.CreateRandom(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);