Skip to content

Commit bef90b5

Browse files
authored
Merge pull request #181 from dotChris90/master
Add Std api as method again
2 parents 8cc334f + 3186738 commit bef90b5

File tree

7 files changed

+94
-18
lines changed

7 files changed

+94
-18
lines changed

NumSharp.sln

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Python", "src\NumS
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "src\NumSharp.Core\NumSharp.Core.csproj", "{190A2514-31CD-4738-AF20-3492DD47DE8C}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NumSharp.Examples", "test\NumSharp.Examples\NumSharp.Examples.csproj", "{D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}"
17-
EndProject
1816
Global
1917
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2018
Debug|Any CPU = Debug|Any CPU
@@ -41,10 +39,6 @@ Global
4139
{190A2514-31CD-4738-AF20-3492DD47DE8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
4240
{190A2514-31CD-4738-AF20-3492DD47DE8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
4341
{190A2514-31CD-4738-AF20-3492DD47DE8C}.Release|Any CPU.Build.0 = Release|Any CPU
44-
{D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45-
{D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Debug|Any CPU.Build.0 = Debug|Any CPU
46-
{D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Release|Any CPU.ActiveCfg = Release|Any CPU
47-
{D8CE7D31-B5F7-4286-9D5D-3CA7781A9522}.Release|Any CPU.Build.0 = Release|Any CPU
4842
EndGlobalSection
4943
GlobalSection(SolutionProperties) = preSolution
5044
HideSolutionNode = FALSE

src/NumSharp.Core/Math/NdArray.Mean.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ public static NDArray mean(this NDArray np, int axis = -1)
1616
// axis == -1: DEFAULT; to compute the mean of the flattened array.
1717
if (axis == -1)
1818
{
19-
var sum = np.Storage.GetData<double>().Sum();
19+
var data = np.Storage.GetData();
20+
21+
double sum = 0;
22+
23+
for (int idx =0; idx < data.Length;idx++)
24+
sum += Convert.ToDouble(data.GetValue(idx));
2025

2126
mean.Storage.SetData(new double[] { sum / np.size});
2227
}
@@ -32,7 +37,7 @@ public static NDArray mean(this NDArray np, int axis = -1)
3237
sumVec[p] += Convert.ToDouble(np[d,p]);
3338
}
3439
}
35-
var puffer = mean.Storage.GetData<double>().ToList();
40+
var puffer = mean.Storage.CloneData<double>().ToList();
3641

3742
for (int d = 0; d < np.shape[1]; d++)
3843
{
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NumSharp.Core.Extensions;
6+
7+
namespace NumSharp.Core
8+
{
9+
public partial class NDArray
10+
{
11+
public NDArray std(int axis = -1, Type dtype = null)
12+
{
13+
dtype = (dtype == null) ? typeof(double) : dtype;
14+
15+
// in case have 1D array but user still using axis 0 ... can be used like -1
16+
axis = (axis == 0 && this.ndim == 1) ? -1 : axis;
17+
18+
Array data = this.Storage.GetData();
19+
20+
NDArray stdArr = new NDArray(dtype);
21+
22+
if (axis == -1)
23+
{
24+
double mean = this.mean(axis).MakeGeneric<double>()[0];
25+
double sum = 0;
26+
for(int idx = 0; idx < data.Length;idx++)
27+
sum += Math.Pow(Convert.ToDouble(data.GetValue(idx)) - mean,2);
28+
29+
double stdValue = Math.Sqrt(sum / this.size);
30+
stdArr.Storage.Allocate(dtype,new Shape(1),1);
31+
var puffer = Array.CreateInstance(dtype,1);
32+
puffer.SetValue(stdValue,0);
33+
stdArr.Storage.SetData(puffer);
34+
}
35+
else
36+
{
37+
double[] stdValue = null;
38+
if (axis == 0)
39+
{
40+
double[] sum = new double[this.shape[1]];
41+
stdValue = new double[sum.Length];
42+
43+
double[] mean = this.mean(axis).Storage.GetData<double>();
44+
45+
for (int idx = 0; idx < sum.Length;idx++)
46+
{
47+
for(int jdx =0; jdx < this.shape[0];jdx++)
48+
{
49+
sum[idx] += Math.Pow(Convert.ToDouble(this[jdx,idx]) - mean[idx],2);
50+
}
51+
stdValue[idx] = Math.Sqrt(sum[idx] / this.shape[0]);
52+
}
53+
54+
}
55+
else if (axis == 1)
56+
{
57+
double[] sum = new double[this.shape[0]];
58+
stdValue = new double[sum.Length];
59+
60+
double[] mean = this.mean(axis).Storage.GetData<double>();
61+
62+
for (int idx = 0; idx < sum.Length;idx++)
63+
{
64+
for(int jdx =0; jdx < this.shape[1];jdx++)
65+
{
66+
sum[idx] += Math.Pow(Convert.ToDouble(this[idx,jdx]) - mean[idx],2);
67+
}
68+
stdValue[idx] = Math.Sqrt(sum[idx] / this.shape[1]);
69+
}
70+
}
71+
else
72+
{
73+
throw new NotImplementedException();
74+
}
75+
stdArr.Storage.Allocate(dtype,new Shape(stdValue.Length),1);
76+
stdArr.Storage.SetData(stdValue);
77+
}
78+
return stdArr;
79+
}
80+
}
81+
}

src/NumSharp.Core/NumSharp.Core.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ Add axis arg in ndarray.roll.</PackageReleaseNotes>
4343

4444
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
4545
<DefineConstants>DEBUG;TRACE</DefineConstants>
46-
<DebugType>full</DebugType>
47-
<DebugSymbols>true</DebugSymbols>
4846
</PropertyGroup>
4947

5048
<ItemGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d'))">

test/NumSharp.UnitTest/Creation/np.random.normal.Test.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public void NormalDistributionTest()
2424
Assert.IsTrue(s.shape[0] == 10);
2525
Assert.IsTrue(s.shape[1] == 100);
2626

27-
// var std = np.std(s, ddof = 1);
28-
// Assert.IsTrue(Math.Abs(sigma - std)) < 0.01;
27+
var std = s.std();
28+
Assert.IsTrue(Math.Abs(sigma - std.Storage.GetData<double>()[0] ) < 0.01);
2929
}
3030
}
3131
}

test/NumSharp.UnitTest/Extensions/NDArray.Std.Test.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public class NDArrayStdTest
1313
[TestMethod]
1414
public void StdTest()
1515
{
16-
var np = new NDArray(typeof(double)).arange(4).reshape(2,2).MakeGeneric<double>();
16+
var nd1 = new NDArray(typeof(double)).arange(4).reshape(2,2).MakeGeneric<double>();
1717

18-
//Assert.IsTrue(Enumerable.SequenceEqual(np.s .Data, new double[] { 1.1180339887498949 }));
19-
// Assert.IsTrue(Enumerable.SequenceEqual(np.Std(0).Data, new double[] { 1, 1 }));
20-
// Assert.IsTrue(Enumerable.SequenceEqual(np.Std(1).Data, new double[] { 0.5, 3.5 }));
18+
Assert.IsTrue(Enumerable.SequenceEqual(nd1.std().Data<double>(), new double[] { 1.1180339887498949 }));
19+
Assert.IsTrue(Enumerable.SequenceEqual(nd1.std(0).Data<double>(), new double[] { 1, 1 }));
20+
Assert.IsTrue(Enumerable.SequenceEqual(nd1.std(1).Data<double>(), new double[] { 0.5, 0.5 }));
2121
}
2222
}
2323
}

test/NumSharp.UnitTest/NumSharp.UnitTest.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
2424
<DefineConstants>DEBUG;TRACE</DefineConstants>
25-
<DebugType>full</DebugType>
26-
<DebugSymbols>true</DebugSymbols>
2725
</PropertyGroup>
2826

2927
<ItemGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d'))">

0 commit comments

Comments
 (0)