public static WeightedMean ( double matrix, double weights, int dimension ) : double[] | ||
matrix | double | A matrix whose means will be calculated. |
weights | double | A vector containing the importance of each sample in the matrix. |
dimension | int | /// The dimension along which the means will be calculated. Pass /// 0 to compute a row vector containing the mean of each column, /// or 1 to compute a column vector containing the mean of each row. /// Default value is 0. /// |
return | double[] |
public static double[] WeightedMean(double[][] matrix, double[] weights, int dimension = 0)
{
int rows = matrix.Length;
if (rows == 0)
return new double[0];
int cols = matrix[0].Length;
double[] mean;
if (dimension == 0)
{
mean = new double[cols];
if (rows != weights.Length)
{
throw new DimensionMismatchException("weights",
"The number of rows and weights must match.");
}
// for each row
for (int i = 0; i < rows; i++)
{
double[] row = matrix[i];
double w = weights[i];
// for each column
for (int j = 0; j < cols; j++)
mean[j] += row[j] * w;
}
}
else if (dimension == 1)
{
mean = new double[rows];
if (cols != weights.Length)
{
throw new DimensionMismatchException("weights",
"The number of columns and weights must match.");
}
// for each row
for (int j = 0; j < rows; j++)
{
double[] row = matrix[j];
double w = weights[j];
// for each column
for (int i = 0; i < cols; i++)
mean[j] += row[i] * w;
}
}
else
{
throw new ArgumentException("Invalid dimension.", "dimension");
}
double weightSum = weights.Sum();
if (weightSum != 0)
for (int i = 0; i < mean.Length; i++)
mean[i] /= weightSum;
return mean;
}
Measures::WeightedMean ( double matrix, int weights, int dimension ) : double[] | |
Measures::WeightedMean ( this matrix, double weights ) : double[] | |
Measures::WeightedMean ( this matrix, int weights ) : double[] |