forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdArray.Mean.cs
More file actions
73 lines (59 loc) · 2.19 KB
/
NdArray.Mean.cs
File metadata and controls
73 lines (59 loc) · 2.19 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumSharp.Core.Extensions
{
public static partial class NDArrayExtensions
{
public static NDArray mean(this NDArray np, int axis = -1)
{
var mean = new NDArray(typeof(double));
mean.Storage.SetData(new double[0]);
// axis == -1: DEFAULT; to compute the mean of the flattened array.
if (axis == -1)
{
var data = np.Storage.GetData();
double sum = 0;
for (int idx =0; idx < data.Length;idx++)
sum += Convert.ToDouble(data.GetValue(idx));
mean.Storage.SetData(new double[] { sum / np.size});
}
// to compute mean by compressing row and row
else if (axis == 0)
{
double[] sumVec = new double[np.shape[0]];
for (int d = 0; d < sumVec.Length; d++)
{
for (int p = 0; p < np.shape[1]; p++)
{
sumVec[p] += Convert.ToDouble(np[d,p]);
}
}
var puffer = mean.Storage.CloneData<double>().ToList();
for (int d = 0; d < np.shape[1]; d++)
{
puffer.Add(sumVec[d] / np.shape[0]);
}
mean.Storage.SetData(puffer.ToArray());
mean.Storage.Reshape(mean.Storage.GetData().Length);
}
else if (axis == 1)
{
var puffer = mean.Storage.GetData<double>().ToList();
for (int d = 0; d < np.shape[0]; d++)
{
double rowSum = 0;
for (int p = 0; p < np.shape[1]; p++)
{
rowSum += Convert.ToDouble(np[d,p]);
}
puffer.Add(rowSum / np.shape[1]);
}
mean.Storage.SetData(puffer.ToArray());
mean.Storage.Reshape(mean.Storage.GetData().Length);
}
return mean;
}
}
}