public static NDArrayGeneric <double> Std(this NDArrayGeneric <double> np, int axis = -1) { var std = new NDArrayGeneric <double>(); var mean = np.Mean(axis); // axis == -1: DEFAULT; to compute the standard deviation of the flattened array. if (axis == -1) { var sum = np.Data.Select(d => Math.Pow(Math.Abs(d - mean.Data[0]), 2)).Sum(); std.Data = new double[] { Math.Sqrt(sum / np.Size) }; } // to compute mean by compressing row and row else if (axis == 0) { double[] sumVec = new double[np.Shape.Shapes[1]]; for (int d = 0; d < np.Shape.Shapes[0]; d++) { for (int p = 0; p < np.Shape.Shapes[1]; p++) { sumVec[p] += np[d, p]; } } var puffer = mean.Data.ToList(); for (int d = 0; d < np.Shape.Shapes[1]; d++) { puffer.Add(sumVec[d] / np.Shape.Shapes[0]); } mean.Data = puffer.ToArray(); } else if (axis == 1) { var puffer = mean.Data.ToList(); for (int d = 0; d < np.Shape.Shapes[0]; d++) { double rowSum = 0; for (int p = 0; p < np.Shape.Shapes[1]; p++) { rowSum += np[d, p]; } puffer.Add(rowSum / np.Shape.Shapes[1]); } mean.Data = puffer.ToArray(); } return(std); }
public void MeanAxisMinus1() { var np = new NDArrayGeneric <double>().arange(5, 1).reshape(2, 2); Assert.IsTrue(Enumerable.SequenceEqual(np.Mean().Data, new double[] { 2.5 })); }