diff --git a/NumSharp.sln b/NumSharp.sln index 95260747..0e8a9e9b 100644 --- a/NumSharp.sln +++ b/NumSharp.sln @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Python", "src\NumS EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "src\NumSharp.Core\NumSharp.Core.csproj", "{190A2514-31CD-4738-AF20-3492DD47DE8C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NumSharp.Examples", "test\NumSharp.Examples\NumSharp.Examples.csproj", "{D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,10 +39,6 @@ Global {190A2514-31CD-4738-AF20-3492DD47DE8C}.Debug|Any CPU.Build.0 = Debug|Any CPU {190A2514-31CD-4738-AF20-3492DD47DE8C}.Release|Any CPU.ActiveCfg = Release|Any CPU {190A2514-31CD-4738-AF20-3492DD47DE8C}.Release|Any CPU.Build.0 = Release|Any CPU - {D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/NumSharp.Core/Math/NdArray.Mean.cs b/src/NumSharp.Core/Math/NdArray.Mean.cs index cf9e69c0..c8977c53 100644 --- a/src/NumSharp.Core/Math/NdArray.Mean.cs +++ b/src/NumSharp.Core/Math/NdArray.Mean.cs @@ -16,7 +16,12 @@ public static NDArray mean(this NDArray np, int axis = -1) // axis == -1: DEFAULT; to compute the mean of the flattened array. if (axis == -1) { - var sum = np.Storage.GetData().Sum(); + 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}); } @@ -32,7 +37,7 @@ public static NDArray mean(this NDArray np, int axis = -1) sumVec[p] += Convert.ToDouble(np[d,p]); } } - var puffer = mean.Storage.GetData().ToList(); + var puffer = mean.Storage.CloneData().ToList(); for (int d = 0; d < np.shape[1]; d++) { diff --git a/src/NumSharp.Core/Math/NdArray.Std.cs b/src/NumSharp.Core/Math/NdArray.Std.cs new file mode 100644 index 00000000..350e7444 --- /dev/null +++ b/src/NumSharp.Core/Math/NdArray.Std.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NumSharp.Core.Extensions; + +namespace NumSharp.Core +{ + public partial class NDArray + { + public NDArray std(int axis = -1, Type dtype = null) + { + dtype = (dtype == null) ? typeof(double) : dtype; + + // in case have 1D array but user still using axis 0 ... can be used like -1 + axis = (axis == 0 && this.ndim == 1) ? -1 : axis; + + Array data = this.Storage.GetData(); + + NDArray stdArr = new NDArray(dtype); + + if (axis == -1) + { + double mean = this.mean(axis).MakeGeneric()[0]; + double sum = 0; + for(int idx = 0; idx < data.Length;idx++) + sum += Math.Pow(Convert.ToDouble(data.GetValue(idx)) - mean,2); + + double stdValue = Math.Sqrt(sum / this.size); + stdArr.Storage.Allocate(dtype,new Shape(1),1); + var puffer = Array.CreateInstance(dtype,1); + puffer.SetValue(stdValue,0); + stdArr.Storage.SetData(puffer); + } + else + { + double[] stdValue = null; + if (axis == 0) + { + double[] sum = new double[this.shape[1]]; + stdValue = new double[sum.Length]; + + double[] mean = this.mean(axis).Storage.GetData(); + + for (int idx = 0; idx < sum.Length;idx++) + { + for(int jdx =0; jdx < this.shape[0];jdx++) + { + sum[idx] += Math.Pow(Convert.ToDouble(this[jdx,idx]) - mean[idx],2); + } + stdValue[idx] = Math.Sqrt(sum[idx] / this.shape[0]); + } + + } + else if (axis == 1) + { + double[] sum = new double[this.shape[0]]; + stdValue = new double[sum.Length]; + + double[] mean = this.mean(axis).Storage.GetData(); + + for (int idx = 0; idx < sum.Length;idx++) + { + for(int jdx =0; jdx < this.shape[1];jdx++) + { + sum[idx] += Math.Pow(Convert.ToDouble(this[idx,jdx]) - mean[idx],2); + } + stdValue[idx] = Math.Sqrt(sum[idx] / this.shape[1]); + } + } + else + { + throw new NotImplementedException(); + } + stdArr.Storage.Allocate(dtype,new Shape(stdValue.Length),1); + stdArr.Storage.SetData(stdValue); + } + return stdArr; + } + } +} \ No newline at end of file diff --git a/src/NumSharp.Core/NumSharp.Core.csproj b/src/NumSharp.Core/NumSharp.Core.csproj index 02b6d105..f262a7e8 100644 --- a/src/NumSharp.Core/NumSharp.Core.csproj +++ b/src/NumSharp.Core/NumSharp.Core.csproj @@ -43,8 +43,6 @@ Add axis arg in ndarray.roll. DEBUG;TRACE - full - true diff --git a/test/NumSharp.UnitTest/Creation/np.random.normal.Test.cs b/test/NumSharp.UnitTest/Creation/np.random.normal.Test.cs index 4fa443ea..e2e02c43 100644 --- a/test/NumSharp.UnitTest/Creation/np.random.normal.Test.cs +++ b/test/NumSharp.UnitTest/Creation/np.random.normal.Test.cs @@ -24,8 +24,8 @@ public void NormalDistributionTest() Assert.IsTrue(s.shape[0] == 10); Assert.IsTrue(s.shape[1] == 100); - // var std = np.std(s, ddof = 1); - // Assert.IsTrue(Math.Abs(sigma - std)) < 0.01; + var std = s.std(); + Assert.IsTrue(Math.Abs(sigma - std.Storage.GetData()[0] ) < 0.01); } } } diff --git a/test/NumSharp.UnitTest/Extensions/NDArray.Std.Test.cs b/test/NumSharp.UnitTest/Extensions/NDArray.Std.Test.cs index 2ed39c6f..6e484e2c 100644 --- a/test/NumSharp.UnitTest/Extensions/NDArray.Std.Test.cs +++ b/test/NumSharp.UnitTest/Extensions/NDArray.Std.Test.cs @@ -13,11 +13,11 @@ public class NDArrayStdTest [TestMethod] public void StdTest() { - var np = new NDArray(typeof(double)).arange(4).reshape(2,2).MakeGeneric(); + var nd1 = new NDArray(typeof(double)).arange(4).reshape(2,2).MakeGeneric(); - //Assert.IsTrue(Enumerable.SequenceEqual(np.s .Data, new double[] { 1.1180339887498949 })); - // Assert.IsTrue(Enumerable.SequenceEqual(np.Std(0).Data, new double[] { 1, 1 })); - // Assert.IsTrue(Enumerable.SequenceEqual(np.Std(1).Data, new double[] { 0.5, 3.5 })); + Assert.IsTrue(Enumerable.SequenceEqual(nd1.std().Data(), new double[] { 1.1180339887498949 })); + Assert.IsTrue(Enumerable.SequenceEqual(nd1.std(0).Data(), new double[] { 1, 1 })); + Assert.IsTrue(Enumerable.SequenceEqual(nd1.std(1).Data(), new double[] { 0.5, 0.5 })); } } } diff --git a/test/NumSharp.UnitTest/NumSharp.UnitTest.csproj b/test/NumSharp.UnitTest/NumSharp.UnitTest.csproj index 70f0f706..4fcea96a 100644 --- a/test/NumSharp.UnitTest/NumSharp.UnitTest.csproj +++ b/test/NumSharp.UnitTest/NumSharp.UnitTest.csproj @@ -22,8 +22,6 @@ DEBUG;TRACE - full - true