Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions NumSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/NumSharp.Core/Math/NdArray.Mean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>().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});
}
Expand All @@ -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<double>().ToList();
var puffer = mean.Storage.CloneData<double>().ToList();

for (int d = 0; d < np.shape[1]; d++)
{
Expand Down
81 changes: 81 additions & 0 deletions src/NumSharp.Core/Math/NdArray.Std.cs
Original file line number Diff line number Diff line change
@@ -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<double>()[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<double>();

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<double>();

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;
}
}
}
2 changes: 0 additions & 2 deletions src/NumSharp.Core/NumSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ Add axis arg in ndarray.roll.</PackageReleaseNotes>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<ItemGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d'))">
Expand Down
4 changes: 2 additions & 2 deletions test/NumSharp.UnitTest/Creation/np.random.normal.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>()[0] ) < 0.01);
}
}
}
8 changes: 4 additions & 4 deletions test/NumSharp.UnitTest/Extensions/NDArray.Std.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public class NDArrayStdTest
[TestMethod]
public void StdTest()
{
var np = new NDArray(typeof(double)).arange(4).reshape(2,2).MakeGeneric<double>();
var nd1 = new NDArray(typeof(double)).arange(4).reshape(2,2).MakeGeneric<double>();

//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<double>(), new double[] { 1.1180339887498949 }));
Assert.IsTrue(Enumerable.SequenceEqual(nd1.std(0).Data<double>(), new double[] { 1, 1 }));
Assert.IsTrue(Enumerable.SequenceEqual(nd1.std(1).Data<double>(), new double[] { 0.5, 0.5 }));
}
}
}
2 changes: 0 additions & 2 deletions test/NumSharp.UnitTest/NumSharp.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

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