Repository navigation
Expand file tree
/
Copy pathNdArray.Mean.cs
More file actions
53 lines (49 loc) · 1.6 KB
/
Copy pathNdArray.Mean.cs
File metadata and controls
53 lines (49 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumSharp.Extensions
{
public static partial class NDArrayExtensions
{
public static NDArray_Legacy<double> Mean(this NDArray_Legacy<NDArray_Legacy<double>> np, int axis = -1)
{
var mean = new NDArray_Legacy<double>();
// axis == -1: DEFAULT; to compute the mean of the flattened array.
if (axis == -1)
{
var sum = np.Data.Select(d => d.Data.Sum()).Sum();
mean.Data.Add(sum / np.Size);
}
// to compute mean by compressing row and row
else if (axis == 0)
{
double[] sumVec = new double[np.Data[0].Length];
for (int d = 0; d < np.Length; d++)
{
for (int p = 0; p < np.Data[0].Length; p++)
{
sumVec[p] += np.Data[d][p];
}
}
for (int d = 0; d < np.Data[0].Length; d++)
{
mean.Data.Add(sumVec[d] / np.Length);
}
}
else if (axis == 1)
{
for (int d = 0; d < np.Length; d++)
{
double rowSum = 0;
for (int p = 0; p < np.Data[0].Length; p++)
{
rowSum += np.Data[d][p];
}
mean.Data.Add(rowSum / np.Data[0].Length);
}
}
return mean;
}
}
}