Comments (8)
I added more overloads for linalg. Check it out, this passes now:
using LA = Numpy.np.linalg;
[TestMethod]
public void normTest()
{
// >>> from numpy import linalg as LA
// >>> a = np.arange(9) - 4
// >>> a
// array([-4, -3, -2, -1, 0, 1, 2, 3, 4])
// >>> b = a.reshape((3, 3))
// >>> b
// array([[-4, -3, -2],
// [-1, 0, 1],
// [ 2, 3, 4]])
//
var a = np.arange(9) - 4;
NDarray given = a;
var expected =
"array([-4, -3, -2, -1, 0, 1, 2, 3, 4])";
Assert.AreEqual(expected, given.repr);
var b = a.reshape(3, 3);
given = b;
expected =
"array([[-4, -3, -2],\n" +
" [-1, 0, 1],\n" +
" [ 2, 3, 4]])";
Assert.AreEqual(expected, given.repr);
// >>> LA.norm(a)
// 7.745966692414834
// >>> LA.norm(b)
// 7.745966692414834
// >>> LA.norm(b, 'fro')
// 7.745966692414834
// >>> LA.norm(a, np.inf)
// 4.0
// >>> LA.norm(b, np.inf)
// 9.0
// >>> LA.norm(a, -np.inf)
// 0.0
// >>> LA.norm(b, -np.inf)
// 2.0
//
Assert.GreaterOrEqual(7.74596669f, (float)LA.norm(a));
Assert.GreaterOrEqual(7.74596669f, (float)LA.norm(b));
Assert.GreaterOrEqual(7.74596669f, LA.norm(b, "fro"));
Assert.AreEqual(4, LA.norm(a, Constants.inf));
Assert.AreEqual(9, LA.norm(b, Constants.inf));
Assert.AreEqual(0, LA.norm(a, Constants.neg_inf));
Assert.AreEqual(2, LA.norm(b, Constants.neg_inf));
// >>> LA.norm(a, 1)
// 20.0
// >>> LA.norm(b, 1)
// 7.0
// >>> LA.norm(a, -1)
// -4.6566128774142013e-010
// >>> LA.norm(b, -1)
// 6.0
// >>> LA.norm(a, 2)
// 7.745966692414834
// >>> LA.norm(b, 2)
// 7.3484692283495345
//
Assert.AreEqual(20f, (float)LA.norm(a, 1));
Assert.AreEqual(7f, (float)LA.norm(b, 1));
Assert.GreaterOrEqual(0f, (float)LA.norm(a, -1));
Assert.GreaterOrEqual(6, (float)LA.norm(b, -1));
Assert.GreaterOrEqual(7.74596669f, (float)LA.norm(a, 2));
Assert.GreaterOrEqual(7.34846922f, (float)LA.norm(b, 2));
// >>> LA.norm(a, -2)
// 0.0
// >>> LA.norm(b, -2)
// 1.8570331885190563e-016
// >>> LA.norm(a, 3)
// 5.8480354764257312
// >>> LA.norm(a, -3)
// 0.0
//
Assert.AreEqual(0f, (float)LA.norm(a, -2));
Assert.AreEqual(1.8570331885190563e-016f, (float)LA.norm(b, -2));
Assert.AreEqual(5.8480354764257312f, (float)LA.norm(a, 3));
Assert.AreEqual(0f, (float)LA.norm(a, -3));
// Using the axis argument to compute vector norms:
// >>> c = np.array([[ 1, 2, 3],
// ... [-1, 1, 4]])
// >>> LA.norm(c, axis=0)
// array([ 1.41421356, 2.23606798, 5. ])
// >>> LA.norm(c, axis=1)
// array([ 3.74165739, 4.24264069])
// >>> LA.norm(c, ord=1, axis=1)
// array([ 6., 6.])
//
var c = np.array(new[,]{{ 1, 2, 3},{-1, 1, 4}});
given= LA.norm(c, axis:0);
expected=
"array([1.41421356, 2.23606798, 5. ])";
Assert.AreEqual(expected, given.repr);
given= LA.norm(c, axis:1);
expected=
"array([3.74165739, 4.24264069])";
Assert.AreEqual(expected, given.repr);
given= LA.norm(c, ord:1, axis:1);
expected=
"array([6., 6.])";
Assert.AreEqual(expected, given.repr);
// Using the axis argument to compute matrix norms:
// >>> m = np.arange(8).reshape(2,2,2)
// >>> LA.norm(m, axis=(1,2))
// array([ 3.74165739, 11.22497216])
// >>> LA.norm(m[0, :, :]), LA.norm(m[1, :, :])
// (3.7416573867739413, 11.224972160321824)
//
var m = np.arange(8).reshape(2,2,2);
given= LA.norm(m, axis: new[]{1,2});
expected=
"array([ 3.74165739, 11.22497216])";
Assert.AreEqual(expected, given.repr);
var given1= new[]{ LA.norm(m["0, :, :"]), LA.norm(m["1, :, :"])};
expected=
"(3.7416573867739413, 11.224972160321824)";
Assert.AreEqual(expected, given1.repr());
}
from numpy.net.
released as v19 on nuget
from numpy.net.
Hi Chris,
thanks for the high praise, I do appreciate it.
The reason for the first two problems (missing overloads) is that the whole API is automatically generated by parsing the numpy documentation which doesn't specify exactly what kinds of values can be passed in. So as users find such missing overloads I gradually add them. For instance, it would say a parameter is "array_like" and I am now slowly finding out that that doesn't only mean ndarrays but also tuples of ndarrays in many cases.
I'll add overloads for np.linalg.norm
and np.random.normal
, no problem.
As for the probelm with GetData, I haven't fully understood it yet. Could you open a separate issue about it and provide a code sample so I can reproduce it?
Thanks for contributing back!
PS: What is your project and can I add it to the list of projects using Numpy.NET?
from numpy.net.
Oh, I get your GetData point now. GetData returns the underlying data as a 1D array always regardless of the shape of the NDarray. Numpy's data representation at the low level is always a 1D array. Copying that data all in one piece is just the most performant way of getting data from Python to C#. But we could of course implement a function to copy data into a multi-dimensional C# array using slicing and copying slices one by one. Would you like to implement it?
from numpy.net.
Or you could just use my library SliceAndDice to access the data multi-dimensionally.
from numpy.net.
also added overloads for np.random.normal
. this testcase passes:
[TestMethod]
public void normalTest()
{
// Draw samples from the distribution:
// >>> mu, sigma = 0, 0.1 # mean and standard deviation
// >>> s = np.random.normal(mu, sigma, 1000)
//
var (mu, sigma) = (0.0f, 0.1f); // mean and standard deviation;
var s = np.random.normal(mu, sigma, 1000);
// Verify the mean and the variance:
// >>> abs(mu - np.mean(s)) < 0.01
// True
//
Assert.IsTrue( Math.Abs(mu - np.mean(s)) < 0.01);
// >>> abs(sigma - np.std(s, ddof=1)) < 0.01
// True
//
Assert.IsTrue(Math.Abs(sigma - np.std(s, ddof: 1)) < 0.01);
// Two-by-four array of samples from N(3, 6.25):
// >>> np.random.normal(3, 2.5, size = (2, 4))
// array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random
// [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random
Assert.AreEqual(new Shape(2,4), np.random.normal(3, 2.5f, new []{2, 4}).shape );
}
from numpy.net.
Hey @henon ,
Thanks for adding the overloads, that is great! Just FYI I also noticed a similar behavior with np.mean
( have to write np.mean(x, new[] { 0 })
instead of np.mean(x, axis:0)
)
Regarding GetData
, I will try to find some time this or next week to make a branch and implement it.
Unfortunately the project I am working on is one related to a private company, so I am not sure whether I can disclose any information about it. If I can though (for this or future projects) I will let you know so you can add it to the list :)
C.
from numpy.net.
This same problem affects almost all statistics functions and many others. I found a solution for all by changing the parameter axis
to type Axis. Parameter axis
can by assigned null, int or int[] due to implicit cast operators, thus allowing all possibilities without overloads.
This test passes now:
[TestMethod]
public void meanTest()
{
// >>> a = np.array([[1, 2], [3, 4]])
// >>> np.mean(a)
// 2.5
// >>> np.mean(a, axis=0)
// array([ 2., 3.])
// >>> np.mean(a, axis=1)
// array([ 1.5, 3.5])
//
NDarray a = np.array(new [,]{{1, 2}, {3, 4}});
var given_scalar= np.mean(a);
Assert.AreEqual(2.5, given_scalar);
var given= np.mean(a, axis:0);
var expected=
"array([2., 3.])";
Assert.AreEqual(expected, given.repr);
given= np.mean(a, axis:1);
expected=
"array([1.5, 3.5])";
Assert.AreEqual(expected, given.repr);
// In single precision, mean can be inaccurate:
// >>> a = np.zeros((2, 512*512), dtype=np.float32)
// >>> a[0, :] = 1.0
// >>> a[1, :] = 0.1
// >>> np.mean(a)
// 0.54999924
//
a = np.zeros(new Shape(2, 512*512), dtype: np.float32);
a["0, :"] = (NDarray)1.0;
a["1, :"] = (NDarray)0.1;
given_scalar= Math.Round( np.mean(a), 8);
var expected_scalar=
0.54999924;
Assert.AreEqual(expected_scalar, given_scalar);
// Computing the mean in float64 is more accurate:
// >>> np.mean(a, dtype=np.float64)
// 0.55000000074505806
//
given_scalar= np.mean(a, dtype: np.float64);
expected_scalar=
0.55000000074505806;
Assert.AreEqual(expected_scalar, given_scalar);
}
from numpy.net.
Related Issues (20)
- Cannot slice by passing either a string or a Numpy.Models.Slice as an index. HOT 6
- Slow Conversion for 3D arrays HOT 3
- np.load of np.savez impossible to get values back out? HOT 3
- np.array leaks memory HOT 12
- Numpy hangs on exit since version 3.10.1.29 (last stable 3.7.1.28) HOT 7
- Support for CuPy HOT 1
- Please Sign and assign StrongName for Numpy Nuget package
- np.split HOT 12
- `Half` type arrays HOT 7
- Set conditions as NDArray Type HOT 3
- fft() failed: βindex -2 is out of bounds for axis 0 with size 1β HOT 9
- Problem with passing imaginery numbers HOT 3
- NDarray of DateTime HOT 3
- Azure AppService and System.DllNotFoundException: Could not load python311.dll with flags RTLD_NOW | RTLD_GLOBAL: python311.dll: cannot open shared object file: No such file or directory HOT 5
- Could not find platform dependent libraries <exec_prefix> HOT 6
- System.TypeLoadException: Could not load type 'Python.Runtime.PyLong' from assembly 'Python.Runtime, Version=3.0.1.0, Culture=neutral, PublicKeyToken=5000fea6cba702dd'. HOT 5
- Numpy.Bare build for Python 3.11 HOT 3
- np.sum(NDArray, axis:1) not supported? HOT 5
- Unable to use Numpy.NET from linux environment HOT 1
- Hello, has SciSharp/NumSharp stopped development and maintenance? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from numpy.net.